From be2244eca9dccae7db0887baa2b96ac780500ee9 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Fri, 20 Dec 2024 20:15:30 +0100 Subject: [PATCH] Inline module Because this is Terraform, and it's the ultimate optimisation technique --- 2024/bonus/day11/step/main.tf | 25 ++++++++++++++++--------- 2024/bonus/day11/transform/main.tf | 15 --------------- 2 files changed, 16 insertions(+), 24 deletions(-) delete mode 100644 2024/bonus/day11/transform/main.tf diff --git a/2024/bonus/day11/step/main.tf b/2024/bonus/day11/step/main.tf index 5e0d9d9..204d670 100644 --- a/2024/bonus/day11/step/main.tf +++ b/2024/bonus/day11/step/main.tf @@ -2,17 +2,24 @@ variable "prev" { type = map(number) } -module "transform" { - source = "../transform" - for_each = var.prev - - num = each.key -} - locals { + not_a_module = { + for num, _ in var.prev : num => ( + tonumber(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, length(tostring(num)) / 2)), + ] + : [num * 2024] + ) + ) + } by_value = flatten([ - for key, value in module.transform : - [for result in value.result : { num = result, count = var.prev[key] }] + for key, value in local.not_a_module : + [for result in value : { num = result, count = var.prev[key] }] ]) grouped = { for kv in local.by_value : kv.num => kv.count... } diff --git a/2024/bonus/day11/transform/main.tf b/2024/bonus/day11/transform/main.tf deleted file mode 100644 index 46c5c83..0000000 --- a/2024/bonus/day11/transform/main.tf +++ /dev/null @@ -1,15 +0,0 @@ -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] -}