2023.06 (naive)
This commit is contained in:
parent
a5250cc4e9
commit
2f374d1cad
4 changed files with 68 additions and 0 deletions
1
challenges/2023/06-waitForIt/README.md
Normal file
1
challenges/2023/06-waitForIt/README.md
Normal file
|
@ -0,0 +1 @@
|
|||
# [Day 6: Wait For It](https://adventofcode.com/2023/day/6)
|
58
challenges/2023/06-waitForIt/main.py
Normal file
58
challenges/2023/06-waitForIt/main.py
Normal 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))
|
8
challenges/2023/06-waitForIt/tests.json
Normal file
8
challenges/2023/06-waitForIt/tests.json
Normal 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"}
|
||||
]
|
||||
}
|
|
@ -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 :) |
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue