2025 day 2 part 2 in Terraform

This commit is contained in:
2025-12-02 13:35:53 +01:00
parent f132842b5c
commit 7e23cf94a6
4 changed files with 132 additions and 6 deletions

29
2025/day02/README.md Normal file
View File

@@ -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.
```

View File

@@ -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)

View File

@@ -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 {

View File

@@ -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"
}
}