From 227772101067c82cdad140e21642e576878f3cf7 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Tue, 2 Dec 2025 09:36:44 +0100 Subject: [PATCH] 2025 day 2 part 1 --- 2025/day02/main.tf | 17 +++++++++++++++++ 2025/day02/range/item/main.tf | 7 +++++++ 2025/day02/range/main.tf | 36 +++++++++++++++++++++++++++++++++++ 2025/day02/tests.tftest.hcl | 31 ++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+) create mode 100644 2025/day02/main.tf create mode 100644 2025/day02/range/item/main.tf create mode 100644 2025/day02/range/main.tf create mode 100644 2025/day02/tests.tftest.hcl diff --git a/2025/day02/main.tf b/2025/day02/main.tf new file mode 100644 index 0000000..fdf4685 --- /dev/null +++ b/2025/day02/main.tf @@ -0,0 +1,17 @@ +locals { + input = file("../inputs/02.txt") + ranges = split(",", chomp(local.input)) + min_max = [for r in local.ranges : split("-", r)] +} + +module "check_range" { + source = "./range" + count = length(local.min_max) + + min = local.min_max[count.index][0] + max = local.min_max[count.index][1] +} + +output "part1" { + value = sum(module.check_range[*].invalid_sum) +} diff --git a/2025/day02/range/item/main.tf b/2025/day02/range/item/main.tf new file mode 100644 index 0000000..3e8864c --- /dev/null +++ b/2025/day02/range/item/main.tf @@ -0,0 +1,7 @@ +variable "half" { + type = number +} + +output "full" { + value = tonumber("${var.half}${var.half}") +} diff --git a/2025/day02/range/main.tf b/2025/day02/range/main.tf new file mode 100644 index 0000000..f6440ba --- /dev/null +++ b/2025/day02/range/main.tf @@ -0,0 +1,36 @@ +variable "min" { + type = number +} + +variable "max" { + type = number +} + +locals { + digits = floor(length(tostring(var.max)) / 2) + maximum = substr(tostring(var.max), 0, local.digits) + real_maximum = length(tostring(var.max)) % 2 == 0 ? tonumber(local.maximum) : pow(10, local.digits) + + min_digits = max(floor(length(tostring(var.min)) / 2), 1) + minimum = tonumber(substr(tostring(var.min), 0, local.min_digits)) + + count = max(local.real_maximum - local.minimum + 1, 1) + + can_work = anytrue([for n in range(length(tostring(var.min)), length(tostring(var.max)) + 1) : n % 2 == 0]) +} + +// This "candidates" module ought really be a list comprehension from range, but Terraform does not +// allow you to create ranges longer than 1024. +module "candidates" { + source = "./item" + count = local.can_work ? local.count : 0 + half = count.index + local.minimum +} + +locals { + valid = [for n in module.candidates[*].full : n if n >= var.min && n <= var.max] +} + +output "invalid_sum" { + value = length(local.valid) > 0 ? sum(local.valid) : 0 +} diff --git a/2025/day02/tests.tftest.hcl b/2025/day02/tests.tftest.hcl new file mode 100644 index 0000000..d2fee17 --- /dev/null +++ b/2025/day02/tests.tftest.hcl @@ -0,0 +1,31 @@ +run "sample-1" { + module { + source = "./range" + } + + variables { + min = 11 + max = 22 + } + + assert { + condition = output.invalid_sum == 33 + error_message = "Incorrect result" + } +} + +run "sample-2" { + module { + source = "./range" + } + + variables { + min = 95 + max = 115 + } + + assert { + condition = output.invalid_sum == 99 + error_message = "Incorrect result" + } +}