From 7e23cf94a648ea769e46b3eb9e9e7fa7a39ac983 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Tue, 2 Dec 2025 13:35:53 +0100 Subject: [PATCH] 2025 day 2 part 2 in Terraform --- 2025/day02/README.md | 29 +++++++++++ 2025/day02/range/main.tf | 4 +- 2025/day02/range2/main.tf | 4 +- 2025/day02/tests.tftest.hcl | 101 +++++++++++++++++++++++++++++++++++- 4 files changed, 132 insertions(+), 6 deletions(-) create mode 100644 2025/day02/README.md diff --git a/2025/day02/README.md b/2025/day02/README.md new file mode 100644 index 0000000..5c438d6 --- /dev/null +++ b/2025/day02/README.md @@ -0,0 +1,29 @@ +## Day 02: Terraform + +The code assumes an input file at `../inputs/02.txt`. Other than that, simply try to run as follows: + +```console +$ terraform init +… +Initializing the backend... +Initializing modules... +Initializing provider plugins... + +Terraform has been successfully initialized! + +You may now begin working with Terraform. Try running "terraform plan" to see +any changes that are required for your infrastructure. All Terraform commands +should now work. + +If you ever set or change modules or backend configuration for Terraform, +rerun this command to reinitialize your working directory. If you forget, other +commands will detect it and remind you to do so if necessary. +$ terraform plan +…. + +Changes to Outputs: + + part1 = secret + + part2 = also secret + +You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure. +``` diff --git a/2025/day02/range/main.tf b/2025/day02/range/main.tf index 7d3f86f..b8a4a06 100644 --- a/2025/day02/range/main.tf +++ b/2025/day02/range/main.tf @@ -16,8 +16,8 @@ locals { maximum = substr(tostring(var.max), 0, local.digits) real_maximum = length(tostring(var.max)) % var.repetitions == 0 ? tonumber(local.maximum) : pow(10, local.digits) - min_digits = max(floor(length(tostring(var.min)) / var.repetitions), 1) - minimum = tonumber(substr(tostring(var.min), 0, local.min_digits)) + minimum_prefix = substr(tostring(var.min), 0, length(tostring(var.min)) - local.digits * (var.repetitions - 1)) + minimum = local.minimum_prefix == "" ? 1 : tonumber(local.minimum_prefix) count = max(local.real_maximum - local.minimum + 1, 1) diff --git a/2025/day02/range2/main.tf b/2025/day02/range2/main.tf index 722f109..68a4faa 100644 --- a/2025/day02/range2/main.tf +++ b/2025/day02/range2/main.tf @@ -12,10 +12,10 @@ locals { module "range" { source = "../range" - count = local.digits + count = local.digits - 1 max = var.max min = var.min - repetitions = count.index + 1 + repetitions = count.index + 2 } locals { diff --git a/2025/day02/tests.tftest.hcl b/2025/day02/tests.tftest.hcl index d2fee17..f755873 100644 --- a/2025/day02/tests.tftest.hcl +++ b/2025/day02/tests.tftest.hcl @@ -1,4 +1,4 @@ -run "sample-1" { +run "sample-1-1" { module { source = "./range" } @@ -14,7 +14,7 @@ run "sample-1" { } } -run "sample-2" { +run "sample-1-2" { module { source = "./range" } @@ -29,3 +29,100 @@ run "sample-2" { error_message = "Incorrect result" } } + +run "sample-2-1" { + module { + source = "./range2" + } + + variables { + min = 11 + max = 22 + } + + assert { + condition = output.invalid_sum == 33 + error_message = "Incorrect result" + } +} + +run "sample-2-2" { + module { + source = "./range2" + } + + variables { + min = 95 + max = 115 + } + + assert { + condition = output.invalid_sum == 210 + error_message = "Incorrect result" + } +} + +run "sample-2-3" { + module { + source = "./range2" + } + + variables { + min = 998 + max = 1012 + } + + assert { + condition = output.invalid_sum == 2009 + error_message = "Incorrect result" + } +} + +run "sample-2-2-detail" { + module { + source = "./range" + } + + variables { + min = 95 + max = 115 + repetitions = 3 + } + + assert { + condition = output.invalid_sum == 111 + error_message = "Incorrect result" + } +} + +run "sample-2-10" { + module { + source = "./range2" + } + + variables { + min = 824824821 + max = 824824827 + } + + assert { + condition = output.invalid_sum == 824824824 + error_message = "Incorrect result" + } +} + +run "sample-2-11" { + module { + source = "./range2" + } + + variables { + min = 2121212118 + max = 2121212124 + } + + assert { + condition = output.invalid_sum == 2121212121 + error_message = "Incorrect result" + } +}