Skip to content

Commit c3357d9

Browse files
committed
Implement day 5 part 1 in terraform
I'm pretty sure part 2 is impossible
1 parent edb0767 commit c3357d9

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
variable "update" {
2+
type = list(number)
3+
}
4+
5+
variable "disallow_rules" {
6+
type = map(list(number))
7+
}
8+
9+
locals {
10+
not_disallowed = alltrue([
11+
for i in range(1, length(var.update)) :
12+
!contains(
13+
flatten([for j in range(i) : lookup(var.disallow_rules, var.update[j], [])]),
14+
var.update[i]
15+
)
16+
])
17+
}
18+
19+
output "valid" {
20+
value = local.not_disallowed ? var.update[floor(length(var.update) / 2)] : 0
21+
}

2024/bonus/day05/main.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
variable "input" {
2+
type = string
3+
}
4+
5+
locals {
6+
parts = split("\n\n", chomp(var.input))
7+
rules = [for rule_line in split("\n", local.parts[0]) : [for v in split("|", rule_line) : tonumber(v)]]
8+
disallow_rules = { for rule in local.rules : rule[1] => rule[0]... }
9+
10+
updates = [for update_line in split("\n", local.parts[1]) : [for v in split(",", update_line) : tonumber(v)]]
11+
}
12+
13+
module "is_valid" {
14+
source = "./is_correct"
15+
count = length(local.updates)
16+
17+
update = local.updates[count.index]
18+
disallow_rules = local.disallow_rules
19+
}
20+
21+
output "part1" {
22+
value = sum(module.is_valid[*].valid)
23+
}

2024/bonus/tests.tftest.hcl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,20 @@ run "day4" {
113113
error_message = "Part2 output is wrong"
114114
}
115115
}
116+
117+
run "day5_1" {
118+
command = plan
119+
120+
module {
121+
source = "./day05"
122+
}
123+
124+
variables {
125+
input = file("../tests/samples/05.txt")
126+
}
127+
128+
assert {
129+
condition = output.part1 == 143
130+
error_message = "Part1 output is wrong"
131+
}
132+
}

0 commit comments

Comments
 (0)