From 5c030d52728bb0cdadc685584e0930114584a045 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Fri, 27 Dec 2024 21:14:28 +0100 Subject: [PATCH] Implement 2024 day 14 part 1 in Terraform --- 2024/bonus/day14/main.tf | 33 +++++++++++++++++++++++++++++++++ 2024/bonus/main.tf | 9 +++++++++ 2024/bonus/tests.tftest.hcl | 19 +++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 2024/bonus/day14/main.tf diff --git a/2024/bonus/day14/main.tf b/2024/bonus/day14/main.tf new file mode 100644 index 0000000..1ba438b --- /dev/null +++ b/2024/bonus/day14/main.tf @@ -0,0 +1,33 @@ +variable "input" { + type = string +} + +variable "width" { + type = number + default = 101 +} + +variable "height" { + type = number + default = 103 +} + +locals { + lines = regexall("p=(-?\\d+),(-?\\d+) v=(-?\\d+),(-?\\d+)", var.input) + positions = [ + for line in local.lines : + [ + ((line[0] + 100 * line[2]) % var.width + var.width) % var.width, + ((line[1] + 100 * line[3]) % var.height + var.height) % var.height, + ] + ] + + q1 = length([for pos in local.positions : pos if pos[0] < floor(var.width / 2) && pos[1] < floor(var.height / 2)]) + q2 = length([for pos in local.positions : pos if pos[0] > floor(var.width / 2) && pos[1] < floor(var.height / 2)]) + q3 = length([for pos in local.positions : pos if pos[0] < floor(var.width / 2) && pos[1] > floor(var.height / 2)]) + q4 = length([for pos in local.positions : pos if pos[0] > floor(var.width / 2) && pos[1] > floor(var.height / 2)]) +} + +output "part1" { + value = local.q1 * local.q2 * local.q3 * local.q4 +} diff --git a/2024/bonus/main.tf b/2024/bonus/main.tf index 1e3ca93..868efc8 100644 --- a/2024/bonus/main.tf +++ b/2024/bonus/main.tf @@ -92,6 +92,15 @@ output "day13_2" { value = module.day13.part2 } +module "day14" { + source = "./day14" + input = file("../inputs/14.txt") +} + +output "day14_1" { + value = module.day14.part1 +} + module "day19" { source = "./day19" input = file("../inputs/19.txt") diff --git a/2024/bonus/tests.tftest.hcl b/2024/bonus/tests.tftest.hcl index 8ec0ea1..95c715a 100644 --- a/2024/bonus/tests.tftest.hcl +++ b/2024/bonus/tests.tftest.hcl @@ -165,6 +165,25 @@ run "day13" { } } +run "day14" { + command = plan + + module { + source = "./day14" + } + + variables { + input = file("../tests/samples/14.txt") + height = 7 + width = 11 + } + + assert { + condition = output.part1 == 12 + error_message = "Part1 output is wrong" + } +} + run "day19" { command = plan