mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
2025 day 2 part 1
This commit is contained in:
7
2025/day02/range/item/main.tf
Normal file
7
2025/day02/range/item/main.tf
Normal file
@@ -0,0 +1,7 @@
|
||||
variable "half" {
|
||||
type = number
|
||||
}
|
||||
|
||||
output "full" {
|
||||
value = tonumber("${var.half}${var.half}")
|
||||
}
|
||||
36
2025/day02/range/main.tf
Normal file
36
2025/day02/range/main.tf
Normal file
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user