Skip to content

Commit d07bb92

Browse files
committed
Implement 2024 day 13 in terraform
1 parent b23676b commit d07bb92

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed

2024/bonus/day13/main.tf

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
variable "input" {
2+
type = string
3+
default = <<-EOT
4+
Button A: X+94, Y+34
5+
Button B: X+22, Y+67
6+
Prize: X=8400, Y=5400
7+
8+
Button A: X+26, Y+66
9+
Button B: X+67, Y+21
10+
Prize: X=12748, Y=12176
11+
12+
Button A: X+17, Y+86
13+
Button B: X+84, Y+37
14+
Prize: X=7870, Y=6450
15+
16+
Button A: X+69, Y+23
17+
Button B: X+27, Y+71
18+
Prize: X=18641, Y=10279
19+
EOT
20+
}
21+
22+
locals {
23+
machines = regexall(
24+
"Button A: X\\+(\\d+), Y\\+(\\d+)\nButton B: X\\+(\\d+), Y\\+(\\d+)\nPrize: X=(\\d+), Y=(\\d+)",
25+
var.input
26+
)
27+
}
28+
29+
module "solve1" {
30+
source = "./solve"
31+
machines = local.machines
32+
}
33+
34+
module "solve2" {
35+
source = "./solve"
36+
machines = [
37+
for machine in local.machines :
38+
[machine[0], machine[1], machine[2], machine[3], 10000000000000 + tonumber(machine[4]), 10000000000000 + tonumber(machine[5])]
39+
]
40+
}
41+
42+
output "part1" {
43+
value = module.solve1.solutions
44+
}
45+
46+
output "part2" {
47+
value = module.solve2.solutions
48+
}

2024/bonus/day13/solve/main.tf

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
variable "machines" {
2+
type = list(list(number))
3+
}
4+
5+
locals {
6+
a_substitutions = [
7+
for machine in var.machines :
8+
[-machine[2] / machine[0], machine[4] / machine[0]]
9+
]
10+
11+
b_equations = [
12+
for i in range(length(var.machines)) :
13+
[
14+
var.machines[i][3] + local.a_substitutions[i][0] * var.machines[i][1],
15+
var.machines[i][5] - local.a_substitutions[i][1] * var.machines[i][1]
16+
]
17+
]
18+
19+
b = [for eq in local.b_equations : floor(eq[1] / eq[0] + 0.5)]
20+
21+
a = [
22+
for i in range(length(var.machines)) :
23+
floor((var.machines[i][4] - local.b[i] * var.machines[i][2]) / var.machines[i][0] + 0.5)
24+
]
25+
}
26+
27+
output "solutions" {
28+
value = sum([
29+
for i in range(length(var.machines)) :
30+
3 * local.a[i] + local.b[i]
31+
if var.machines[i][0] * local.a[i] + var.machines[i][2] * local.b[i] == var.machines[i][4]
32+
&& var.machines[i][1] * local.a[i] + var.machines[i][3] * local.b[i] == var.machines[i][5]
33+
])
34+
}

2024/bonus/main.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,19 @@ output "day11_2" {
7979
value = module.day11.part2
8080
}
8181

82+
module "day13" {
83+
source = "./day13"
84+
input = file("../inputs/13.txt")
85+
}
86+
87+
output "day13_1" {
88+
value = module.day13.part1
89+
}
90+
91+
output "day13_2" {
92+
value = module.day13.part2
93+
}
94+
8295
module "day19" {
8396
source = "./day19"
8497
input = file("../inputs/19.txt")

2024/bonus/tests.tftest.hcl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,23 @@ run "day11" {
148148
}
149149
}
150150

151+
run "day13" {
152+
command = plan
153+
154+
module {
155+
source = "./day13"
156+
}
157+
158+
variables {
159+
input = file("../tests/samples/13.txt")
160+
}
161+
162+
assert {
163+
condition = output.part1 == 480
164+
error_message = "Part1 output is wrong"
165+
}
166+
}
167+
151168
run "day19" {
152169
command = plan
153170

0 commit comments

Comments
 (0)