mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Compare commits
3 Commits
5e9a24c8d7
...
f132842b5c
| Author | SHA1 | Date | |
|---|---|---|---|
| f132842b5c | |||
| 027a7bdde6 | |||
| 2277721010 |
29
2025/day02/main.tf
Normal file
29
2025/day02/main.tf
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
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]
|
||||||
|
}
|
||||||
|
|
||||||
|
module "check_range2" {
|
||||||
|
source = "./range2"
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
output "part2" {
|
||||||
|
value = sum(module.check_range2[*].invalid_sum)
|
||||||
|
}
|
||||||
15
2025/day02/range/item/main.tf
Normal file
15
2025/day02/range/item/main.tf
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
variable "part" {
|
||||||
|
type = number
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "repetitions" {
|
||||||
|
type = number
|
||||||
|
}
|
||||||
|
|
||||||
|
locals {
|
||||||
|
repeated = [for _ in range(var.repetitions) : tostring(var.part)]
|
||||||
|
}
|
||||||
|
|
||||||
|
output "full" {
|
||||||
|
value = join("", local.repeated)
|
||||||
|
}
|
||||||
46
2025/day02/range/main.tf
Normal file
46
2025/day02/range/main.tf
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
variable "min" {
|
||||||
|
type = number
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "max" {
|
||||||
|
type = number
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "repetitions" {
|
||||||
|
type = number
|
||||||
|
default = 2
|
||||||
|
}
|
||||||
|
|
||||||
|
locals {
|
||||||
|
digits = floor(length(tostring(var.max)) / var.repetitions)
|
||||||
|
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))
|
||||||
|
|
||||||
|
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 % var.repetitions == 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
|
||||||
|
part = count.index + local.minimum
|
||||||
|
repetitions = var.repetitions
|
||||||
|
}
|
||||||
|
|
||||||
|
locals {
|
||||||
|
invalid = [for n in module.candidates[*].full : n if n >= var.min && n <= var.max]
|
||||||
|
}
|
||||||
|
|
||||||
|
output "invalid_sum" {
|
||||||
|
value = length(local.invalid) > 0 ? sum(local.invalid) : 0
|
||||||
|
}
|
||||||
|
|
||||||
|
output "invalid" {
|
||||||
|
value = toset(local.invalid)
|
||||||
|
}
|
||||||
27
2025/day02/range2/main.tf
Normal file
27
2025/day02/range2/main.tf
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
variable "min" {
|
||||||
|
type = number
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "max" {
|
||||||
|
type = number
|
||||||
|
}
|
||||||
|
|
||||||
|
locals {
|
||||||
|
digits = length(tostring(var.max))
|
||||||
|
}
|
||||||
|
|
||||||
|
module "range" {
|
||||||
|
source = "../range"
|
||||||
|
count = local.digits
|
||||||
|
max = var.max
|
||||||
|
min = var.min
|
||||||
|
repetitions = count.index + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
locals {
|
||||||
|
results = setunion(module.range[*].invalid...)
|
||||||
|
}
|
||||||
|
|
||||||
|
output "invalid_sum" {
|
||||||
|
value = length(local.results) > 0 ? sum(local.results) : 0
|
||||||
|
}
|
||||||
31
2025/day02/tests.tftest.hcl
Normal file
31
2025/day02/tests.tftest.hcl
Normal file
@@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user