From 073b576fd854a798ba7732819a805b0b91f9c3d2 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Thu, 26 Dec 2024 15:13:46 +0100 Subject: [PATCH] Implement 2024 day 25 in Terraform --- 2024/bonus/day25/main.tf | 23 +++++++++++++++++++++++ 2024/bonus/main.tf | 9 +++++++++ 2024/bonus/tests.tftest.hcl | 17 +++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 2024/bonus/day25/main.tf diff --git a/2024/bonus/day25/main.tf b/2024/bonus/day25/main.tf new file mode 100644 index 0000000..a54b198 --- /dev/null +++ b/2024/bonus/day25/main.tf @@ -0,0 +1,23 @@ +variable "input" { + type = string +} + +locals { + blocks = split("\n\n", chomp(var.input)) + heights = [ + for block in local.blocks : [ + for i in range(5) : length([ + for line in split("\n", block) : line if substr(line, i, 1) == "#" + ]) + ] + ] + + locks = [for i in range(length(local.blocks)) : local.heights[i] if startswith(local.blocks[i], "#####")] + keys = [for i in range(length(local.blocks)) : local.heights[i] if !startswith(local.blocks[i], "#####")] + + combined = concat([for lock in local.locks : [for key in local.keys : [for i in range(5) : lock[i] + key[i] <= 7]]]...) +} + +output "part1" { + value = length([for combination in local.combined : combination if alltrue(combination)]) +} diff --git a/2024/bonus/main.tf b/2024/bonus/main.tf index 8149729..ed96c52 100644 --- a/2024/bonus/main.tf +++ b/2024/bonus/main.tf @@ -87,3 +87,12 @@ module "day19" { output "day19_1" { value = module.day19.part1 } + +module "day25" { + source = "./day25" + input = file("../inputs/25.txt") +} + +output "day25_1" { + value = module.day25.part1 +} diff --git a/2024/bonus/tests.tftest.hcl b/2024/bonus/tests.tftest.hcl index 1fc06f1..3fc450b 100644 --- a/2024/bonus/tests.tftest.hcl +++ b/2024/bonus/tests.tftest.hcl @@ -164,3 +164,20 @@ run "day19" { error_message = "Part1 output is wrong" } } + +run "day25" { + command = plan + + module { + source = "./day25" + } + + variables { + input = file("../tests/samples/25.txt") + } + + assert { + condition = output.part1 == 3 + error_message = "Part1 output is wrong" + } +}