2023.06 (naive)

This commit is contained in:
akp 2023-12-06 11:53:56 +00:00
parent a5250cc4e9
commit 2f374d1cad
No known key found for this signature in database
GPG key ID: CF8D58F3DEB20755
4 changed files with 68 additions and 0 deletions

View file

@ -0,0 +1 @@
# [Day 6: Wait For It](https://adventofcode.com/2023/day/6)

View file

@ -0,0 +1,58 @@
import sys
from functools import reduce
import math
# time then record dist
Race = tuple[int, int]
def parse(instr: str) -> list[Race]:
times, distances = [[int(y) for y in x.split(":")[1].split(" ") if y != ""] for x in instr.splitlines()]
return list(zip(times, distances))
def solve_races(races: list[Race]) -> int:
acc = 1
for (duration, record) in races:
winning_plays = 0
for i in range(duration):
time_used = i
time_remaining = duration - time_used
speed = i
distance_travelled = speed * time_remaining
if distance_travelled > record:
winning_plays += 1
acc *= winning_plays
return acc
def one(instr: str):
return solve_races(parse(instr))
def two(instr: str):
races = parse(instr)
race = tuple(int(reduce(lambda x, y: x + str(y), [race[i] for race in races], "")) for i in range(2))
return solve_races([race])
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,8 @@
{
"1": [
{"is": "288", "input": "Time: 7 15 30\nDistance: 9 40 200\n"}
],
"2": [
{"is": "71503", "input": "Time: 7 15 30\nDistance: 9 40 200\n"}
]
}

View file

@ -15,3 +15,4 @@ Solutions to the [2023 Advent of Code](https://adventofcode.com/2023).
| 03 - Gear Ratios | ★ ★ | Python | First coordinate grid of the year! |
| 04 - Scratchcards | ★ ★ | Python | First flawed initial grok of the year |
| 05 - If You Give A Seed A Fertilizer | ★ ☆ | Python | This year is ridiculously unbalanced. Why is it so hard already. |
| 06 - Wait For It | ★ ★ | Python | Easy, GCSE-level maths :) |