2 Commits

Author SHA1 Message Date
2beccdd0e6 day 11 part 1 in terraform 2024-12-11 18:53:06 +01:00
023807a701 Implement 2024 day 11 2024-12-11 08:51:32 +01:00
7 changed files with 287 additions and 0 deletions

161
2024/bonus/day11/main.tf Normal file
View File

@@ -0,0 +1,161 @@
variable "input" {
type = string
}
locals {
nums = [for s in split(" ", chomp(var.input)) : tonumber(s)]
}
module "step1" {
source = "./step"
prev = local.nums
}
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 = length(flatten(module.step25.next))
}

View File

@@ -0,0 +1,27 @@
variable "prev" {
type = list(number)
}
locals {
values = [
for num in var.prev : num == 0 ? [1]
: length(tostring(num)) % 2 == 0
? [tonumber(substr(tostring(num), 0, length(tostring(num)) / 2)), tonumber(substr(tostring(num), length(tostring(num)) / 2, 10))]
: [num * 2024]
]
}
# module "transform" {
# source = "../transform"
# count = length(var.prev)
# num = var.prev[count.index]
# }
# output "next" {
# value = flatten(module.transform[*].result)
# }
output "next" {
value = flatten(local.values)
}

View File

@@ -0,0 +1,15 @@
variable "num" {
type = number
}
locals {
as_str = tostring(var.num)
len = length(local.as_str)
half = floor(length(local.as_str) / 2)
first = try(tonumber(substr(local.as_str, 0, local.half)), -1)
second = try(tonumber(substr(local.as_str, local.half, local.half)), -1)
}
output "result" {
value = var.num == 0 ? [1] : local.len % 2 == 0 ? [local.first, local.second] : [var.num * 2024]
}

View File

@@ -65,3 +65,12 @@ module "day05" {
output "day05_1" {
value = module.day05.part1
}
module "day11" {
source = "./day11"
input = file("../inputs/11.txt")
}
output "day11_1" {
value = module.day11.part1
}

View File

@@ -130,3 +130,21 @@ run "day5_1" {
error_message = "Part1 output is wrong"
}
}
run "day11" {
command = plan
module {
source = "./day11"
}
variables {
input = "125 17"
}
assert {
condition = output.part1 == 55312
error_message = "Part1 output is wrong"
}
}

View File

@@ -0,0 +1,50 @@
import functools
from collections import defaultdict
from . import CombinedRunner
@functools.cache
def blink_num(num: int) -> tuple[int, ...]:
if num == 0:
return (1,)
num_str = str(num)
num_len = len(num_str)
if num_len % 2 == 0:
half = num_len // 2
return (int(num_str[:half]), int(num_str[half:]))
return (num * 2024,)
def step(nums: dict[int, int]) -> dict[int, int]:
result = defaultdict(int)
for num, count in nums.items():
for transformed in blink_num(num):
result[transformed] += count
return result
class DayRunner(CombinedRunner):
@classmethod
def run_both(cls, input: str) -> tuple[int, int]:
nums = [int(val) for val in input.strip().split(" ")]
counts = defaultdict(int)
for num in nums:
counts[num] += 1
for _ in range(25):
counts = step(counts)
part1 = sum(counts.values())
for _ in range(50):
counts = step(counts)
return part1, sum(counts.values())

7
2024/tests/test_day11.py Normal file
View File

@@ -0,0 +1,7 @@
from aoc.days.day11 import DayRunner
SAMPLE = "125 17"
def test_sample_part1() -> None:
assert DayRunner.part1(SAMPLE) == 55312