Skip to content

Commit f9416db

Browse files
committed
Implement day 8 2024 in Terraform
1 parent 6941f2b commit f9416db

File tree

5 files changed

+176
-0
lines changed

5 files changed

+176
-0
lines changed

2024/bonus/day08/freq/main.tf

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
variable "width" {
2+
type = number
3+
}
4+
5+
variable "height" {
6+
type = number
7+
}
8+
9+
variable "antennae" {
10+
type = list(tuple([number, number]))
11+
}
12+
13+
locals {
14+
pairs = concat([
15+
for i in range(length(var.antennae)) :
16+
[
17+
for j in range(i + 1, length(var.antennae)) : [var.antennae[i], var.antennae[j]]
18+
]
19+
]...)
20+
}
21+
22+
module "pair" {
23+
source = "./pair"
24+
count = length(local.pairs)
25+
26+
first = local.pairs[count.index][0]
27+
second = local.pairs[count.index][1]
28+
width = var.width
29+
height = var.height
30+
}
31+
32+
output "nodes1" {
33+
value = setunion([
34+
for i in range(length(local.pairs)) :
35+
[
36+
for v in module.pair[i].nodes1 :
37+
v
38+
if v[0] >= 0 && v[0] < var.width && v[1] >= 0 && v[1] < var.height
39+
]
40+
]...)
41+
}
42+
43+
output "nodes2" {
44+
value = setunion([
45+
for i in range(length(local.pairs)) :
46+
[
47+
for v in module.pair[i].nodes2 :
48+
v
49+
if v[0] >= 0 && v[0] < var.width && v[1] >= 0 && v[1] < var.height
50+
]
51+
]...)
52+
}

2024/bonus/day08/freq/pair/main.tf

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
variable "first" {
2+
type = tuple([number, number])
3+
}
4+
5+
variable "second" {
6+
type = tuple([number, number])
7+
}
8+
9+
variable "width" {
10+
type = number
11+
}
12+
13+
variable "height" {
14+
type = number
15+
}
16+
17+
locals {
18+
dx = var.second[0] - var.first[0]
19+
dy = var.second[1] - var.first[1]
20+
}
21+
22+
output "nodes1" {
23+
value = [
24+
[var.first[0] - local.dx, var.first[1] - local.dy],
25+
[var.second[0] + local.dx, var.second[1] + local.dy],
26+
]
27+
}
28+
29+
output "nodes2" {
30+
value = concat(
31+
[for i in range(max(var.width, var.height)) : [var.first[0] - i * local.dx, var.first[1] - i * local.dy]],
32+
[for i in range(max(var.width, var.height)) : [var.second[0] + i * local.dx, var.second[1] + i * local.dy]]
33+
)
34+
}

2024/bonus/day08/main.tf

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
variable "input" {
2+
type = string
3+
default = <<-EOT
4+
............
5+
........0...
6+
.....0......
7+
.......0....
8+
....0.......
9+
......A.....
10+
............
11+
............
12+
........A...
13+
.........A..
14+
............
15+
............
16+
EOT
17+
18+
}
19+
20+
locals {
21+
lines = split("\n", chomp(var.input))
22+
height = length(local.lines)
23+
width = length(local.lines[0])
24+
25+
antennae = concat([
26+
for y in range(local.height) :
27+
[
28+
for x in range(local.width) :
29+
[substr(local.lines[y], x, 1), x, y]
30+
if substr(local.lines[y], x, 1) != "."
31+
]
32+
]...)
33+
34+
by_freq = {
35+
for antenna in local.antennae :
36+
antenna[0] => [antenna[1], antenna[2]]...
37+
}
38+
}
39+
40+
module "freq" {
41+
source = "./freq"
42+
for_each = local.by_freq
43+
width = local.width
44+
height = local.height
45+
antennae = each.value
46+
}
47+
48+
output "part1" {
49+
value = length(setunion([for _, v in module.freq : v.nodes1]...))
50+
}
51+
52+
output "part2" {
53+
value = length(setunion([for _, v in module.freq : v.nodes2]...))
54+
}

2024/bonus/main.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ output "day05_1" {
6666
value = module.day05.part1
6767
}
6868

69+
module "day08" {
70+
source = "./day08"
71+
input = file("../inputs/08.txt")
72+
}
73+
74+
output "day08_1" {
75+
value = module.day08.part1
76+
}
77+
78+
output "day08_2" {
79+
value = module.day08.part2
80+
}
81+
6982
module "day11" {
7083
source = "./day11"
7184
input = file("../inputs/11.txt")

2024/bonus/tests.tftest.hcl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,29 @@ run "day5_1" {
131131
}
132132
}
133133

134+
run "day08" {
135+
command = plan
136+
137+
module {
138+
source = "./day08"
139+
}
140+
141+
variables {
142+
input = file("../tests/samples/08.txt")
143+
}
144+
145+
assert {
146+
condition = output.part1 == 14
147+
error_message = "Part1 output is wrong"
148+
}
149+
150+
151+
assert {
152+
condition = output.part2 == 34
153+
error_message = "Part1 output is wrong"
154+
}
155+
}
156+
134157
run "day11" {
135158
command = plan
136159

0 commit comments

Comments
 (0)