2 Commits

Author SHA1 Message Date
044c971d00 Implement 2024 day 12 2024-12-12 22:18:32 +01:00
8308580284 Actually run part 2 in Terraform, madness 2024-12-11 23:18:56 +01:00
10 changed files with 595 additions and 156 deletions

View File

@@ -15,150 +15,10 @@ module "step1" {
prev = local.total
}
module "step2" {
source = "./step"
prev = module.step1.next
}
module "step3" {
source = "./step"
prev = module.step2.next
}
module "step4" {
source = "./step"
prev = module.step3.next
}
module "step5" {
source = "./step"
prev = module.step4.next
}
module "step6" {
source = "./step"
prev = module.step5.next
}
module "step7" {
source = "./step"
prev = module.step6.next
}
module "step8" {
source = "./step"
prev = module.step7.next
}
module "step9" {
source = "./step"
prev = module.step8.next
}
module "step10" {
source = "./step"
prev = module.step9.next
}
module "step11" {
source = "./step"
prev = module.step10.next
}
module "step12" {
source = "./step"
prev = module.step11.next
}
module "step13" {
source = "./step"
prev = module.step12.next
}
module "step14" {
source = "./step"
prev = module.step13.next
}
module "step15" {
source = "./step"
prev = module.step14.next
}
module "step16" {
source = "./step"
prev = module.step15.next
}
module "step17" {
source = "./step"
prev = module.step16.next
}
module "step18" {
source = "./step"
prev = module.step17.next
}
module "step19" {
source = "./step"
prev = module.step18.next
}
module "step20" {
source = "./step"
prev = module.step19.next
}
module "step21" {
source = "./step"
prev = module.step20.next
}
module "step22" {
source = "./step"
prev = module.step21.next
}
module "step23" {
source = "./step"
prev = module.step22.next
}
module "step24" {
source = "./step"
prev = module.step23.next
}
module "step25" {
source = "./step"
prev = module.step24.next
}
output "part1" {
value = sum(values(module.step25.next))
}
output "part2" {
value = sum(values(module.step75.next))
}

View File

@@ -18,17 +18,6 @@ locals {
grouped = { for kv in local.by_value : kv.num => kv.count... }
}
# module "transform" {
# source = "../transform"
# count = length(var.prev)
# num = var.prev[count.index]
# }
# output "next" {
# value = flatten(module.transform[*].result)
# }
output "next" {
value = { for num, groups in local.grouped : num => sum(groups) }
}

446
2024/bonus/day11/steps.tf Normal file
View File

@@ -0,0 +1,446 @@
# This file was generated using a nasty bash for loop. Edit at your own peril.
module "step2" {
source = "./step"
prev = module.step1.next
}
module "step3" {
source = "./step"
prev = module.step2.next
}
module "step4" {
source = "./step"
prev = module.step3.next
}
module "step5" {
source = "./step"
prev = module.step4.next
}
module "step6" {
source = "./step"
prev = module.step5.next
}
module "step7" {
source = "./step"
prev = module.step6.next
}
module "step8" {
source = "./step"
prev = module.step7.next
}
module "step9" {
source = "./step"
prev = module.step8.next
}
module "step10" {
source = "./step"
prev = module.step9.next
}
module "step11" {
source = "./step"
prev = module.step10.next
}
module "step12" {
source = "./step"
prev = module.step11.next
}
module "step13" {
source = "./step"
prev = module.step12.next
}
module "step14" {
source = "./step"
prev = module.step13.next
}
module "step15" {
source = "./step"
prev = module.step14.next
}
module "step16" {
source = "./step"
prev = module.step15.next
}
module "step17" {
source = "./step"
prev = module.step16.next
}
module "step18" {
source = "./step"
prev = module.step17.next
}
module "step19" {
source = "./step"
prev = module.step18.next
}
module "step20" {
source = "./step"
prev = module.step19.next
}
module "step21" {
source = "./step"
prev = module.step20.next
}
module "step22" {
source = "./step"
prev = module.step21.next
}
module "step23" {
source = "./step"
prev = module.step22.next
}
module "step24" {
source = "./step"
prev = module.step23.next
}
module "step25" {
source = "./step"
prev = module.step24.next
}
module "step26" {
source = "./step"
prev = module.step25.next
}
module "step27" {
source = "./step"
prev = module.step26.next
}
module "step28" {
source = "./step"
prev = module.step27.next
}
module "step29" {
source = "./step"
prev = module.step28.next
}
module "step30" {
source = "./step"
prev = module.step29.next
}
module "step31" {
source = "./step"
prev = module.step30.next
}
module "step32" {
source = "./step"
prev = module.step31.next
}
module "step33" {
source = "./step"
prev = module.step32.next
}
module "step34" {
source = "./step"
prev = module.step33.next
}
module "step35" {
source = "./step"
prev = module.step34.next
}
module "step36" {
source = "./step"
prev = module.step35.next
}
module "step37" {
source = "./step"
prev = module.step36.next
}
module "step38" {
source = "./step"
prev = module.step37.next
}
module "step39" {
source = "./step"
prev = module.step38.next
}
module "step40" {
source = "./step"
prev = module.step39.next
}
module "step41" {
source = "./step"
prev = module.step40.next
}
module "step42" {
source = "./step"
prev = module.step41.next
}
module "step43" {
source = "./step"
prev = module.step42.next
}
module "step44" {
source = "./step"
prev = module.step43.next
}
module "step45" {
source = "./step"
prev = module.step44.next
}
module "step46" {
source = "./step"
prev = module.step45.next
}
module "step47" {
source = "./step"
prev = module.step46.next
}
module "step48" {
source = "./step"
prev = module.step47.next
}
module "step49" {
source = "./step"
prev = module.step48.next
}
module "step50" {
source = "./step"
prev = module.step49.next
}
module "step51" {
source = "./step"
prev = module.step50.next
}
module "step52" {
source = "./step"
prev = module.step51.next
}
module "step53" {
source = "./step"
prev = module.step52.next
}
module "step54" {
source = "./step"
prev = module.step53.next
}
module "step55" {
source = "./step"
prev = module.step54.next
}
module "step56" {
source = "./step"
prev = module.step55.next
}
module "step57" {
source = "./step"
prev = module.step56.next
}
module "step58" {
source = "./step"
prev = module.step57.next
}
module "step59" {
source = "./step"
prev = module.step58.next
}
module "step60" {
source = "./step"
prev = module.step59.next
}
module "step61" {
source = "./step"
prev = module.step60.next
}
module "step62" {
source = "./step"
prev = module.step61.next
}
module "step63" {
source = "./step"
prev = module.step62.next
}
module "step64" {
source = "./step"
prev = module.step63.next
}
module "step65" {
source = "./step"
prev = module.step64.next
}
module "step66" {
source = "./step"
prev = module.step65.next
}
module "step67" {
source = "./step"
prev = module.step66.next
}
module "step68" {
source = "./step"
prev = module.step67.next
}
module "step69" {
source = "./step"
prev = module.step68.next
}
module "step70" {
source = "./step"
prev = module.step69.next
}
module "step71" {
source = "./step"
prev = module.step70.next
}
module "step72" {
source = "./step"
prev = module.step71.next
}
module "step73" {
source = "./step"
prev = module.step72.next
}
module "step74" {
source = "./step"
prev = module.step73.next
}
module "step75" {
source = "./step"
prev = module.step74.next
}

View File

@@ -74,3 +74,7 @@ module "day11" {
output "day11_1" {
value = module.day11.part1
}
output "day11_2" {
value = module.day11.part2
}

View File

@@ -146,5 +146,4 @@ run "day11" {
condition = output.part1 == 55312
error_message = "Part1 output is wrong"
}
}

101
2024/src/aoc/days/day12.py Normal file
View File

@@ -0,0 +1,101 @@
import numpy
from . import CombinedRunner
DIRECTIONS = [
(-1, 0),
(1, 0),
(0, -1),
(0, 1),
]
class DayRunner(CombinedRunner):
@classmethod
def run_both(cls, input: str) -> tuple[int, int]:
grid = numpy.array(list(map(list, input.strip().split("\n"))))
score = 0
score2 = 0
for y in range(grid.shape[0]):
for x in range(grid.shape[1]):
if grid[y, x] == ".":
continue
search = grid[y, x]
grid[y, x] = "."
todo = [(y, x)]
cluster = {(y, x)}
def enqueue(y, x):
if grid[y, x] == search:
grid[y, x] = "."
todo.append((y, x))
cluster.add((y, x))
while todo:
cy, cx = todo.pop()
if cx > 0:
enqueue(cy, cx - 1)
if cy > 0:
enqueue(cy - 1, cx)
if cx < grid.shape[1] - 1:
enqueue(cy, cx + 1)
if cy < grid.shape[0] - 1:
enqueue(cy + 1, cx)
side_length = sum(
sum((cy + dy, cx + dx) not in cluster for dy, dx in DIRECTIONS)
for cy, cx in cluster
)
corners = 0
for cy, cx in cluster:
# Outer corners
corners += (cy, cx - 1) not in cluster and (
cy - 1,
cx,
) not in cluster
corners += (cy, cx + 1) not in cluster and (
cy - 1,
cx,
) not in cluster
corners += (cy, cx - 1) not in cluster and (
cy + 1,
cx,
) not in cluster
corners += (cy, cx + 1) not in cluster and (
cy + 1,
cx,
) not in cluster
# Inner corners
corners += (
(cy, cx - 1) in cluster
and (cy - 1, cx) in cluster
and (cy - 1, cx - 1) not in cluster
)
corners += (
(cy, cx + 1) in cluster
and (cy - 1, cx) in cluster
and (cy - 1, cx + 1) not in cluster
)
corners += (
(cy, cx - 1) in cluster
and (cy + 1, cx) in cluster
and (cy + 1, cx - 1) not in cluster
)
corners += (
(cy, cx + 1) in cluster
and (cy + 1, cx) in cluster
and (cy + 1, cx + 1) not in cluster
)
score += side_length * len(cluster)
score2 += corners * len(cluster)
return (score, score2)

View File

@@ -0,0 +1,4 @@
AAAA
BBCD
BBCC
EEEC

View File

@@ -0,0 +1,5 @@
OOOOO
OXOXO
OOOOO
OXOXO
OOOOO

View File

@@ -0,0 +1,10 @@
RRRRIICCFF
RRRRIICCCF
VVRRRCCFFF
VVRCCCJFFF
VVVVCJJCFE
VVIVCCJJEE
VVIIICJJEE
MIIIIIJJEE
MIIISIJEEE
MMMISSJEEE

21
2024/tests/test_day12.py Normal file
View File

@@ -0,0 +1,21 @@
import pytest
from aoc.days.day12 import DayRunner
from . import get_data
@pytest.mark.parametrize(
"data,result",
[(get_data(12, 1), 140), (get_data(12, 2), 772), (get_data(12, 3), 1930)],
)
def test_sample_part1(data: str, result: int) -> None:
assert DayRunner.part1(data) == result
@pytest.mark.parametrize(
"data,result",
[(get_data(12, 1), 80), (get_data(12, 2), 436), (get_data(12, 3), 1206)],
)
def test_sample_part2(data: str, result: int) -> None:
assert DayRunner.part2(data) == result