2 Commits

Author SHA1 Message Date
5c030d5272 Implement 2024 day 14 part 1 in Terraform 2024-12-27 21:14:28 +01:00
d07bb9235b Implement 2024 day 13 in terraform 2024-12-27 21:08:47 +01:00
5 changed files with 173 additions and 0 deletions

48
2024/bonus/day13/main.tf Normal file
View File

@@ -0,0 +1,48 @@
variable "input" {
type = string
default = <<-EOT
Button A: X+94, Y+34
Button B: X+22, Y+67
Prize: X=8400, Y=5400
Button A: X+26, Y+66
Button B: X+67, Y+21
Prize: X=12748, Y=12176
Button A: X+17, Y+86
Button B: X+84, Y+37
Prize: X=7870, Y=6450
Button A: X+69, Y+23
Button B: X+27, Y+71
Prize: X=18641, Y=10279
EOT
}
locals {
machines = regexall(
"Button A: X\\+(\\d+), Y\\+(\\d+)\nButton B: X\\+(\\d+), Y\\+(\\d+)\nPrize: X=(\\d+), Y=(\\d+)",
var.input
)
}
module "solve1" {
source = "./solve"
machines = local.machines
}
module "solve2" {
source = "./solve"
machines = [
for machine in local.machines :
[machine[0], machine[1], machine[2], machine[3], 10000000000000 + tonumber(machine[4]), 10000000000000 + tonumber(machine[5])]
]
}
output "part1" {
value = module.solve1.solutions
}
output "part2" {
value = module.solve2.solutions
}

View File

@@ -0,0 +1,34 @@
variable "machines" {
type = list(list(number))
}
locals {
a_substitutions = [
for machine in var.machines :
[-machine[2] / machine[0], machine[4] / machine[0]]
]
b_equations = [
for i in range(length(var.machines)) :
[
var.machines[i][3] + local.a_substitutions[i][0] * var.machines[i][1],
var.machines[i][5] - local.a_substitutions[i][1] * var.machines[i][1]
]
]
b = [for eq in local.b_equations : floor(eq[1] / eq[0] + 0.5)]
a = [
for i in range(length(var.machines)) :
floor((var.machines[i][4] - local.b[i] * var.machines[i][2]) / var.machines[i][0] + 0.5)
]
}
output "solutions" {
value = sum([
for i in range(length(var.machines)) :
3 * local.a[i] + local.b[i]
if var.machines[i][0] * local.a[i] + var.machines[i][2] * local.b[i] == var.machines[i][4]
&& var.machines[i][1] * local.a[i] + var.machines[i][3] * local.b[i] == var.machines[i][5]
])
}

33
2024/bonus/day14/main.tf Normal file
View File

@@ -0,0 +1,33 @@
variable "input" {
type = string
}
variable "width" {
type = number
default = 101
}
variable "height" {
type = number
default = 103
}
locals {
lines = regexall("p=(-?\\d+),(-?\\d+) v=(-?\\d+),(-?\\d+)", var.input)
positions = [
for line in local.lines :
[
((line[0] + 100 * line[2]) % var.width + var.width) % var.width,
((line[1] + 100 * line[3]) % var.height + var.height) % var.height,
]
]
q1 = length([for pos in local.positions : pos if pos[0] < floor(var.width / 2) && pos[1] < floor(var.height / 2)])
q2 = length([for pos in local.positions : pos if pos[0] > floor(var.width / 2) && pos[1] < floor(var.height / 2)])
q3 = length([for pos in local.positions : pos if pos[0] < floor(var.width / 2) && pos[1] > floor(var.height / 2)])
q4 = length([for pos in local.positions : pos if pos[0] > floor(var.width / 2) && pos[1] > floor(var.height / 2)])
}
output "part1" {
value = local.q1 * local.q2 * local.q3 * local.q4
}

View File

@@ -79,6 +79,28 @@ output "day11_2" {
value = module.day11.part2
}
module "day13" {
source = "./day13"
input = file("../inputs/13.txt")
}
output "day13_1" {
value = module.day13.part1
}
output "day13_2" {
value = module.day13.part2
}
module "day14" {
source = "./day14"
input = file("../inputs/14.txt")
}
output "day14_1" {
value = module.day14.part1
}
module "day19" {
source = "./day19"
input = file("../inputs/19.txt")

View File

@@ -148,6 +148,42 @@ run "day11" {
}
}
run "day13" {
command = plan
module {
source = "./day13"
}
variables {
input = file("../tests/samples/13.txt")
}
assert {
condition = output.part1 == 480
error_message = "Part1 output is wrong"
}
}
run "day14" {
command = plan
module {
source = "./day14"
}
variables {
input = file("../tests/samples/14.txt")
height = 7
width = 11
}
assert {
condition = output.part1 == 12
error_message = "Part1 output is wrong"
}
}
run "day19" {
command = plan