Skip to content

Commit 5c030d5

Browse files
committed
Implement 2024 day 14 part 1 in Terraform
1 parent d07bb92 commit 5c030d5

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

2024/bonus/day14/main.tf

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
variable "input" {
2+
type = string
3+
}
4+
5+
variable "width" {
6+
type = number
7+
default = 101
8+
}
9+
10+
variable "height" {
11+
type = number
12+
default = 103
13+
}
14+
15+
locals {
16+
lines = regexall("p=(-?\\d+),(-?\\d+) v=(-?\\d+),(-?\\d+)", var.input)
17+
positions = [
18+
for line in local.lines :
19+
[
20+
((line[0] + 100 * line[2]) % var.width + var.width) % var.width,
21+
((line[1] + 100 * line[3]) % var.height + var.height) % var.height,
22+
]
23+
]
24+
25+
q1 = length([for pos in local.positions : pos if pos[0] < floor(var.width / 2) && pos[1] < floor(var.height / 2)])
26+
q2 = length([for pos in local.positions : pos if pos[0] > floor(var.width / 2) && pos[1] < floor(var.height / 2)])
27+
q3 = length([for pos in local.positions : pos if pos[0] < floor(var.width / 2) && pos[1] > floor(var.height / 2)])
28+
q4 = length([for pos in local.positions : pos if pos[0] > floor(var.width / 2) && pos[1] > floor(var.height / 2)])
29+
}
30+
31+
output "part1" {
32+
value = local.q1 * local.q2 * local.q3 * local.q4
33+
}

2024/bonus/main.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@ output "day13_2" {
9292
value = module.day13.part2
9393
}
9494

95+
module "day14" {
96+
source = "./day14"
97+
input = file("../inputs/14.txt")
98+
}
99+
100+
output "day14_1" {
101+
value = module.day14.part1
102+
}
103+
95104
module "day19" {
96105
source = "./day19"
97106
input = file("../inputs/19.txt")

2024/bonus/tests.tftest.hcl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,25 @@ run "day13" {
165165
}
166166
}
167167

168+
run "day14" {
169+
command = plan
170+
171+
module {
172+
source = "./day14"
173+
}
174+
175+
variables {
176+
input = file("../tests/samples/14.txt")
177+
height = 7
178+
width = 11
179+
}
180+
181+
assert {
182+
condition = output.part1 == 12
183+
error_message = "Part1 output is wrong"
184+
}
185+
}
186+
168187
run "day19" {
169188
command = plan
170189

0 commit comments

Comments
 (0)