mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Implement day 5 part 1 in terraform
I'm pretty sure part 2 is impossible
This commit is contained in:
21
2024/bonus/day05/is_correct/main.tf
Normal file
21
2024/bonus/day05/is_correct/main.tf
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
variable "update" {
|
||||||
|
type = list(number)
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "disallow_rules" {
|
||||||
|
type = map(list(number))
|
||||||
|
}
|
||||||
|
|
||||||
|
locals {
|
||||||
|
not_disallowed = alltrue([
|
||||||
|
for i in range(1, length(var.update)) :
|
||||||
|
!contains(
|
||||||
|
flatten([for j in range(i) : lookup(var.disallow_rules, var.update[j], [])]),
|
||||||
|
var.update[i]
|
||||||
|
)
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
|
output "valid" {
|
||||||
|
value = local.not_disallowed ? var.update[floor(length(var.update) / 2)] : 0
|
||||||
|
}
|
||||||
23
2024/bonus/day05/main.tf
Normal file
23
2024/bonus/day05/main.tf
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
variable "input" {
|
||||||
|
type = string
|
||||||
|
}
|
||||||
|
|
||||||
|
locals {
|
||||||
|
parts = split("\n\n", chomp(var.input))
|
||||||
|
rules = [for rule_line in split("\n", local.parts[0]) : [for v in split("|", rule_line) : tonumber(v)]]
|
||||||
|
disallow_rules = { for rule in local.rules : rule[1] => rule[0]... }
|
||||||
|
|
||||||
|
updates = [for update_line in split("\n", local.parts[1]) : [for v in split(",", update_line) : tonumber(v)]]
|
||||||
|
}
|
||||||
|
|
||||||
|
module "is_valid" {
|
||||||
|
source = "./is_correct"
|
||||||
|
count = length(local.updates)
|
||||||
|
|
||||||
|
update = local.updates[count.index]
|
||||||
|
disallow_rules = local.disallow_rules
|
||||||
|
}
|
||||||
|
|
||||||
|
output "part1" {
|
||||||
|
value = sum(module.is_valid[*].valid)
|
||||||
|
}
|
||||||
@@ -56,3 +56,12 @@ output "day03_2" {
|
|||||||
# output "day04_2" {
|
# output "day04_2" {
|
||||||
# value = module.day04.part2
|
# value = module.day04.part2
|
||||||
# }
|
# }
|
||||||
|
|
||||||
|
module "day05" {
|
||||||
|
source = "./day05"
|
||||||
|
input = file("../inputs/05.txt")
|
||||||
|
}
|
||||||
|
|
||||||
|
output "day05_1" {
|
||||||
|
value = module.day05.part1
|
||||||
|
}
|
||||||
|
|||||||
@@ -113,3 +113,20 @@ run "day4" {
|
|||||||
error_message = "Part2 output is wrong"
|
error_message = "Part2 output is wrong"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
run "day5_1" {
|
||||||
|
command = plan
|
||||||
|
|
||||||
|
module {
|
||||||
|
source = "./day05"
|
||||||
|
}
|
||||||
|
|
||||||
|
variables {
|
||||||
|
input = file("../tests/samples/05.txt")
|
||||||
|
}
|
||||||
|
|
||||||
|
assert {
|
||||||
|
condition = output.part1 == 143
|
||||||
|
error_message = "Part1 output is wrong"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user