4 Commits

Author SHA1 Message Date
e9a57701c9 Optimize day 4 enough to run 2024-12-05 22:52:57 +01:00
f7af07a631 Slightly better performance 2024-12-05 22:18:26 +01:00
ecfe5e9f20 Implement day 4 in Terraform 2024-12-05 22:14:35 +01:00
0967a3dfe3 Formatting 2024-12-05 19:36:09 +01:00
8 changed files with 206 additions and 2 deletions

View File

@@ -0,0 +1,30 @@
variable "grid" {
type = list(string)
}
variable "x" {
type = number
}
variable "y" {
type = number
}
locals {
found_a = substr(var.grid[var.y], var.x, 1) == "A"
c1 = substr(var.grid[var.y - 1], var.x - 1, 1)
c2 = substr(var.grid[var.y - 1], var.x + 1, 1)
c3 = substr(var.grid[var.y + 1], var.x + 1, 1)
c4 = substr(var.grid[var.y + 1], var.x - 1, 1)
d1 = "${local.c1}${local.c3}"
d2 = "${local.c2}${local.c4}"
found_d1 = contains(["SM", "MS"], local.d1)
found_d2 = contains(["SM", "MS"], local.d2)
}
output "found" {
value = local.found_a && local.found_d1 && local.found_d2
}

View File

@@ -0,0 +1,27 @@
variable "grid" {
type = list(string)
}
variable "x" {
type = number
}
variable "y" {
type = number
}
variable "dx" {
type = number
}
variable "dy" {
type = number
}
locals {
word = join("", [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") : "F"])
}
output "found" {
value = contains(["SAMX", "XMAS"], local.word) ? 1 : 0
}

View File

@@ -0,0 +1,59 @@
variable "grid" {
type = list(string)
}
variable "index" {
type = number
}
variable "width" {
type = number
}
variable "height" {
type = number
}
locals {
x = var.index % var.width
y = floor(var.index / var.width)
directions = {
"DL" = [-1, 1]
"D" = [0, 1]
"DR" = [1, 1]
}
should_check_x_mas = local.x >= 1 && local.y >= 1 && local.x < var.width - 1 && local.y < var.height - 1
}
module "check_xmas" {
source = "./check_xmas"
for_each = local.directions
grid = var.grid
x = local.x
y = local.y
dx = each.value[0]
dy = each.value[1]
}
module "check_x_mas" {
source = "./check_x_mas"
count = local.should_check_x_mas ? 1 : 0
grid = var.grid
y = local.y
x = local.x
}
output "xmas" {
value = sum([for _, v in module.check_xmas : v.found])
}
output "x_mas" {
value = try(module.check_x_mas[0].found, false) ? 1 : 0
}

31
2024/bonus/day04/main.tf Normal file
View File

@@ -0,0 +1,31 @@
variable "input" {
type = string
}
locals {
grid = split("\n", chomp(var.input))
height = length(local.grid)
width = length(local.grid[0])
lr = length(regexall("XMAS", var.input))
rl = length(regexall("SAMX", var.input))
}
module "check_point" {
source = "./check_point"
count = local.width * local.height
width = local.width
height = local.height
grid = local.grid
index = count.index
}
output "part1" {
value = sum(module.check_point[*].xmas) + local.lr + local.rl
}
output "part2" {
value = sum(module.check_point[*].x_mas)
}

View File

@@ -40,3 +40,19 @@ output "day03_1" {
output "day03_2" {
value = module.day03.part2
}
# Don't run this, it runs forever (6 minutes) and requires a lot of memory (~5.5GB) to execute to
# boot. Trust me, it works.
# module "day04" {
# source = "./day04"
# input = file("../inputs/04.txt")
# }
# output "day04_1" {
# value = module.day04.part1
# }
# output "day04_2" {
# value = module.day04.part2
# }

View File

@@ -74,3 +74,42 @@ run "day3_2" {
error_message = "Part2 output is wrong"
}
}
run "day4_small" {
command = plan
module {
source = "./day04"
}
variables {
input = file("../tests/samples/04.1.txt")
}
assert {
condition = output.part1 == 4
error_message = "Part1 output is wrong"
}
}
run "day4" {
command = plan
module {
source = "./day04"
}
variables {
input = file("../tests/samples/04.2.txt")
}
assert {
condition = output.part1 == 18
error_message = "Part1 output is wrong"
}
assert {
condition = output.part2 == 9
error_message = "Part2 output is wrong"
}
}

View File

@@ -1,5 +1,5 @@
from collections import defaultdict
import functools
from collections import defaultdict
from . import CombinedRunner
@@ -21,7 +21,7 @@ def is_correct(update: list[int], must_after: dict[int, set[int]]) -> bool:
return False
forbidden |= must_after.get(entry, set())
return True

View File

@@ -2,6 +2,7 @@ import os
from aoc.days.day5 import DayRunner
def get_data() -> str:
sample = os.path.dirname(__file__) + "/samples/05.txt"
with open(sample, mode="rt", encoding="utf-8") as f:
@@ -13,6 +14,7 @@ def test_sample_part1() -> None:
assert DayRunner.part1(data) == 143
def test_sample_part2() -> None:
data = get_data()