4 Commits

Author SHA1 Message Date
e0d458c972 Forgot to include real input 2024-12-03 08:20:53 +01:00
1c54a79d19 Implement 2024 day 3 part 1 in Terraform 2024-12-03 08:18:15 +01:00
c5cbd6d24f Restructure bonus tests 2024-12-03 08:12:45 +01:00
e36f7dd4f7 Implement 2024 day 3 2024-12-03 08:07:14 +01:00
7 changed files with 116 additions and 38 deletions

View File

@@ -1,19 +0,0 @@
variables {
input = file("../../tests/samples/01.txt")
}
run "run" {
command = plan
assert {
condition = output.part1 == 11
error_message = "Part1 output is wrong"
}
assert {
condition = output.part2 == 31
error_message = "Part2 output is wrong"
}
}

View File

@@ -1,19 +0,0 @@
variables {
input = file("../../tests/samples/02.txt")
}
run "run" {
command = plan
assert {
condition = output.part1 == 2
error_message = "Part1 output is wrong"
}
assert {
condition = output.part2 == 4
error_message = "Part2 output is wrong"
}
}

11
2024/bonus/day03/main.tf Normal file
View File

@@ -0,0 +1,11 @@
variable "input" {
type = string
}
locals {
muls = regexall("mul\\((\\d+),(\\d+)\\)", var.input)
}
output "part1" {
value = sum([for mul in local.muls : parseint(mul[1], 10) * parseint(mul[0], 10)])
}

View File

@@ -27,3 +27,12 @@ output "day02_1" {
output "day02_2" {
value = module.day02.part2
}
module "day03" {
source = "./day03"
input = file("../inputs/03.txt")
}
output "day03_1" {
value = module.day03.part1
}

View File

@@ -0,0 +1,59 @@
run "day1" {
command = plan
module {
source = "./day01"
}
variables {
input = file("../tests/samples/01.txt")
}
assert {
condition = output.part1 == 11
error_message = "Part1 output is wrong"
}
assert {
condition = output.part2 == 31
error_message = "Part2 output is wrong"
}
}
run "day2" {
command = plan
module {
source = "./day02"
}
variables {
input = file("../tests/samples/02.txt")
}
assert {
condition = output.part1 == 2
error_message = "Part1 output is wrong"
}
assert {
condition = output.part2 == 4
error_message = "Part2 output is wrong"
}
}
run "day3_1" {
command = plan
module {
source = "./day03"
}
variables {
input = "xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))"
}
assert {
condition = output.part1 == 161
error_message = "Part1 output is wrong"
}
}

25
2024/src/aoc/days/day3.py Normal file
View File

@@ -0,0 +1,25 @@
import re
from . import SeparateRunner
class DayRunner(SeparateRunner):
@classmethod
def part1(cls, data: str) -> int:
return sum(int(a) * int(b) for a, b in re.findall(r"mul\((\d+),(\d+)\)", data))
@classmethod
def part2(cls, data: str) -> int:
do = True
total = 0
for op, a, b in re.findall(r"(don't\(\)|do\(\)|mul\((\d+),(\d+)\))", data):
match op:
case "do()":
do = True
case "don't()":
do = False
case _:
if do:
total += int(a) * int(b)
return total

12
2024/tests/test_day3.py Normal file
View File

@@ -0,0 +1,12 @@
from aoc.days.day3 import DayRunner
SAMPLE_1 = "xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))"
SAMPLE_2 = "xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))"
def test_sample_part1() -> None:
assert DayRunner.part1(SAMPLE_1) == 161
def test_sample_part2() -> None:
assert DayRunner.part2(SAMPLE_2) == 48