From 2beccdd0e61d9d509611a2adeb68a278d9020936 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Wed, 11 Dec 2024 18:53:06 +0100 Subject: [PATCH] day 11 part 1 in terraform --- 2024/bonus/day11/main.tf | 161 +++++++++++++++++++++++++++++ 2024/bonus/day11/step/main.tf | 27 +++++ 2024/bonus/day11/transform/main.tf | 15 +++ 2024/bonus/main.tf | 9 ++ 2024/bonus/tests.tftest.hcl | 18 ++++ 5 files changed, 230 insertions(+) create mode 100644 2024/bonus/day11/main.tf create mode 100644 2024/bonus/day11/step/main.tf create mode 100644 2024/bonus/day11/transform/main.tf diff --git a/2024/bonus/day11/main.tf b/2024/bonus/day11/main.tf new file mode 100644 index 0000000..6ae9dc3 --- /dev/null +++ b/2024/bonus/day11/main.tf @@ -0,0 +1,161 @@ +variable "input" { + type = string +} + +locals { + nums = [for s in split(" ", chomp(var.input)) : tonumber(s)] +} + +module "step1" { + source = "./step" + + prev = local.nums +} + +module "step2" { + source = "./step" + + prev = module.step1.next +} + +module "step3" { + source = "./step" + + prev = module.step2.next +} + +module "step4" { + source = "./step" + + prev = module.step3.next +} + +module "step5" { + source = "./step" + + prev = module.step4.next +} + +module "step6" { + source = "./step" + + prev = module.step5.next +} + +module "step7" { + source = "./step" + + prev = module.step6.next +} + +module "step8" { + source = "./step" + + prev = module.step7.next +} + +module "step9" { + source = "./step" + + prev = module.step8.next +} + +module "step10" { + source = "./step" + + prev = module.step9.next +} + +module "step11" { + source = "./step" + + prev = module.step10.next +} + +module "step12" { + source = "./step" + + prev = module.step11.next +} + +module "step13" { + source = "./step" + + prev = module.step12.next +} + +module "step14" { + source = "./step" + + prev = module.step13.next +} + +module "step15" { + source = "./step" + + prev = module.step14.next +} + +module "step16" { + source = "./step" + + prev = module.step15.next +} + +module "step17" { + source = "./step" + + prev = module.step16.next +} + +module "step18" { + source = "./step" + + prev = module.step17.next +} + +module "step19" { + source = "./step" + + prev = module.step18.next +} + +module "step20" { + source = "./step" + + prev = module.step19.next +} + +module "step21" { + source = "./step" + + prev = module.step20.next +} + +module "step22" { + source = "./step" + + prev = module.step21.next +} + +module "step23" { + source = "./step" + + prev = module.step22.next +} + +module "step24" { + source = "./step" + + prev = module.step23.next +} + +module "step25" { + source = "./step" + + prev = module.step24.next +} + +output "part1" { + value = length(flatten(module.step25.next)) +} diff --git a/2024/bonus/day11/step/main.tf b/2024/bonus/day11/step/main.tf new file mode 100644 index 0000000..24a627b --- /dev/null +++ b/2024/bonus/day11/step/main.tf @@ -0,0 +1,27 @@ +variable "prev" { + type = list(number) +} + +locals { + values = [ + for num in var.prev : num == 0 ? [1] + : length(tostring(num)) % 2 == 0 + ? [tonumber(substr(tostring(num), 0, length(tostring(num)) / 2)), tonumber(substr(tostring(num), length(tostring(num)) / 2, 10))] + : [num * 2024] + ] +} + +# module "transform" { +# source = "../transform" + +# count = length(var.prev) +# num = var.prev[count.index] +# } + +# output "next" { +# value = flatten(module.transform[*].result) +# } + +output "next" { + value = flatten(local.values) +} diff --git a/2024/bonus/day11/transform/main.tf b/2024/bonus/day11/transform/main.tf new file mode 100644 index 0000000..46c5c83 --- /dev/null +++ b/2024/bonus/day11/transform/main.tf @@ -0,0 +1,15 @@ +variable "num" { + type = number +} + +locals { + as_str = tostring(var.num) + len = length(local.as_str) + half = floor(length(local.as_str) / 2) + first = try(tonumber(substr(local.as_str, 0, local.half)), -1) + second = try(tonumber(substr(local.as_str, local.half, local.half)), -1) +} + +output "result" { + value = var.num == 0 ? [1] : local.len % 2 == 0 ? [local.first, local.second] : [var.num * 2024] +} diff --git a/2024/bonus/main.tf b/2024/bonus/main.tf index ba870c4..1ce7d99 100644 --- a/2024/bonus/main.tf +++ b/2024/bonus/main.tf @@ -65,3 +65,12 @@ module "day05" { output "day05_1" { value = module.day05.part1 } + +module "day11" { + source = "./day11" + input = file("../inputs/11.txt") +} + +output "day11_1" { + value = module.day11.part1 +} diff --git a/2024/bonus/tests.tftest.hcl b/2024/bonus/tests.tftest.hcl index a4669de..6f9f16c 100644 --- a/2024/bonus/tests.tftest.hcl +++ b/2024/bonus/tests.tftest.hcl @@ -130,3 +130,21 @@ run "day5_1" { error_message = "Part1 output is wrong" } } + +run "day11" { + command = plan + + module { + source = "./day11" + } + + variables { + input = "125 17" + } + + assert { + condition = output.part1 == 55312 + error_message = "Part1 output is wrong" + } + +}