Skip to content

Commit ecfe5e9

Browse files
committed
Implement day 4 in Terraform
1 parent 0967a3d commit ecfe5e9

File tree

6 files changed

+194
-0
lines changed

6 files changed

+194
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
variable "grid" {
2+
type = list(string)
3+
}
4+
5+
variable "x" {
6+
type = number
7+
}
8+
9+
variable "y" {
10+
type = number
11+
}
12+
13+
locals {
14+
found_a = substr(var.grid[var.y], var.x, 1) == "A"
15+
16+
c1 = substr(var.grid[var.y - 1], var.x - 1, 1)
17+
c2 = substr(var.grid[var.y - 1], var.x + 1, 1)
18+
c3 = substr(var.grid[var.y + 1], var.x + 1, 1)
19+
c4 = substr(var.grid[var.y + 1], var.x - 1, 1)
20+
21+
d1 = "${local.c1}${local.c3}"
22+
d2 = "${local.c2}${local.c4}"
23+
24+
found_d1 = contains(["SM", "MS"], local.d1)
25+
found_d2 = contains(["SM", "MS"], local.d2)
26+
}
27+
28+
output "found" {
29+
value = local.found_a && local.found_d1 && local.found_d2
30+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
variable "grid" {
2+
type = list(string)
3+
}
4+
5+
variable "x" {
6+
type = number
7+
}
8+
9+
variable "y" {
10+
type = number
11+
}
12+
13+
variable "dx" {
14+
type = number
15+
}
16+
17+
variable "dy" {
18+
type = number
19+
}
20+
21+
locals {
22+
match = [for i in range(4) : var.x + i * var.dx >= 0 && try(substr(var.grid[var.y + i * var.dy], var.x + i * var.dx, 1), "F") == substr("XMAS", i, 1)]
23+
}
24+
25+
output "found" {
26+
value = alltrue(local.match) ? 1 : 0
27+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
variable "grid" {
2+
type = list(string)
3+
}
4+
5+
variable "index" {
6+
type = number
7+
}
8+
9+
variable "width" {
10+
type = number
11+
}
12+
13+
variable "height" {
14+
type = number
15+
}
16+
17+
locals {
18+
x = var.index % var.width
19+
y = floor(var.index / var.width)
20+
21+
directions = {
22+
"UL" = [-1, -1]
23+
"U" = [0, -1]
24+
"UR" = [1, -1]
25+
"L" = [-1, 0]
26+
"R" = [1, 0]
27+
"DL" = [-1, 1]
28+
"D" = [0, 1]
29+
"DR" = [1, 1]
30+
}
31+
32+
should_check_x_mas = local.x >= 1 && local.y >= 1 && local.x < var.width - 1 && local.y < var.height - 1
33+
}
34+
35+
module "check_xmas" {
36+
source = "./check_xmas"
37+
for_each = local.directions
38+
39+
grid = var.grid
40+
41+
x = local.x
42+
y = local.y
43+
44+
dx = each.value[0]
45+
dy = each.value[1]
46+
}
47+
48+
module "check_x_mas" {
49+
source = "./check_x_mas"
50+
count = local.should_check_x_mas ? 1 : 0
51+
52+
grid = var.grid
53+
54+
y = local.y
55+
x = local.x
56+
}
57+
58+
output "xmas" {
59+
value = sum([for _, v in module.check_xmas : v.found])
60+
}
61+
62+
output "x_mas" {
63+
value = try(module.check_x_mas[0].found, false) ? 1 : 0
64+
}

2024/bonus/day04/main.tf

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
variable "input" {
2+
type = string
3+
}
4+
5+
locals {
6+
grid = split("\n", chomp(var.input))
7+
height = length(local.grid)
8+
width = length(local.grid[0])
9+
}
10+
11+
module "check_point" {
12+
source = "./check_point"
13+
14+
count = local.width * local.height
15+
16+
width = local.width
17+
height = local.height
18+
grid = local.grid
19+
index = count.index
20+
}
21+
22+
output "part1" {
23+
value = sum(module.check_point[*].xmas)
24+
}
25+
26+
output "part2" {
27+
value = sum(module.check_point[*].x_mas)
28+
}

2024/bonus/main.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,9 @@ output "day03_1" {
4040
output "day03_2" {
4141
value = module.day03.part2
4242
}
43+
44+
# Don't run this, it runs out of memory
45+
# module "day04" {
46+
# source = "./day04"
47+
# input = file("../inputs/04.txt")
48+
# }

2024/bonus/tests.tftest.hcl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,42 @@ run "day3_2" {
7474
error_message = "Part2 output is wrong"
7575
}
7676
}
77+
78+
run "day4_small" {
79+
command = plan
80+
81+
module {
82+
source = "./day04"
83+
}
84+
85+
variables {
86+
input = file("../tests/samples/04.1.txt")
87+
}
88+
89+
assert {
90+
condition = output.part1 == 4
91+
error_message = "Part1 output is wrong"
92+
}
93+
}
94+
95+
run "day4" {
96+
command = plan
97+
98+
module {
99+
source = "./day04"
100+
}
101+
102+
variables {
103+
input = file("../tests/samples/04.2.txt")
104+
}
105+
106+
assert {
107+
condition = output.part1 == 18
108+
error_message = "Part1 output is wrong"
109+
}
110+
111+
assert {
112+
condition = output.part2 == 9
113+
error_message = "Part2 output is wrong"
114+
}
115+
}

0 commit comments

Comments
 (0)