This commit is contained in:
akp 2023-12-09 17:19:21 +00:00
parent 7e8d195381
commit accf0a8850
No known key found for this signature in database
GPG key ID: CF8D58F3DEB20755
4 changed files with 62 additions and 0 deletions

View file

@ -0,0 +1 @@
# [Day 9: Mirage Maintenance](https://adventofcode.com/2023/day/9)

View file

@ -0,0 +1,50 @@
import sys
from functools import reduce
from typing import Callable
def parse(instr: str) -> list[list[int]]:
return [[int(x) for x in line.split(" ")] for line in instr.splitlines()]
def get_term(start: list[int], reduction_fn: Callable[[int, list[int]], int]) -> int:
seqs = [start]
while not reduce(lambda acc, v: acc and v == 0, seqs[-1], True):
cs = seqs[-1]
x = []
for i in range(len(cs) - 1):
x.append(cs[i+1] - cs[i])
seqs.append(x)
return reduce(reduction_fn, seqs[-2::-1], 0)
def run(instr: str, reduction_fn: Callable[[int, list[int]], int]):
sequences = parse(instr)
acc = 0
for sq in sequences:
acc += get_term(sq, reduction_fn)
return acc
def one(instr: str):
return run(instr, lambda acc, x: acc + x[-1])
def two(instr: str):
return run(instr, lambda acc, x: x[0] - acc)
def _debug(*args, **kwargs):
kwargs["file"] = sys.stderr
print(*args, **kwargs)
if __name__ == "__main__":
if len(sys.argv) < 2 or sys.argv[1] not in ["1", "2"]:
print("Missing day argument", file=sys.stderr)
sys.exit(1)
inp = sys.stdin.read().strip()
if sys.argv[1] == "1":
print(one(inp))
else:
print(two(inp))

View file

@ -0,0 +1,10 @@
{
"1": [
{"is": "114", "input": "0 3 6 9 12 15\n1 3 6 10 15 21\n10 13 16 21 30 45\n"},
{"is": "36", "input": "30 26 24 24 26 30"}
],
"2": [
{"is": "2", "input": "0 3 6 9 12 15\n1 3 6 10 15 21\n10 13 16 21 30 45\n"},
{"is": "36", "input": "30 26 24 24 26 30"}
]
}

View file

@ -18,3 +18,4 @@ Solutions to the [2023 Advent of Code](https://adventofcode.com/2023).
| 06 - Wait For It | ★ ★ | Python | Easy, GCSE-level maths :) |
| 07 - Camel Cards | ★ ★ | Python | Pedantic problem statements will be my downfall |
| 08 - Haunted Wasteland | ★ ★ | Python | I'm not sure any feasible generic solution exists - but I did work out the infeasible one! |
| 09 - Mirage Maintenance | ★ ★ | Python | GCSE maths and the n-th term coming in clutch right here |