Skip to content

Commit 6dbff9c

Browse files
committed
Last minute tweaks to Day 5, and completed Day 6
1 parent f76add4 commit 6dbff9c

File tree

4 files changed

+68
-7
lines changed

4 files changed

+68
-7
lines changed

Day5/almanac.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ def generator() -> Generator[int, int, None]:
7070
# Stay here while i is in the current range
7171
while i in cur_range:
7272
i = yield i + offset
73-
elif i < start(cur_range) and i >= end(prev):
74-
while i < start(cur_ranges) and i >= end(prev):
73+
elif i < start(cur_range) and (prev is None or i >= end(prev)):
74+
while i < start(cur_range) and (prev is None or i >= end(prev)):
7575
i = yield i
7676
elif i >= end(cur_range):
7777
if cur_range == ranges.end[0]:
@@ -80,9 +80,6 @@ def generator() -> Generator[int, int, None]:
8080
else:
8181
prev = cur_range
8282
cur_range, offset = ranges.next()
83-
elif prev is None:
84-
while i < start(cur_range):
85-
i = yield i
8683
elif i < end(prev):
8784
cur_range, offset = ranges.prev()
8885
prev = None if ranges.last() is None else ranges.last()[0]
@@ -251,7 +248,7 @@ def part_two(source:str) -> int:
251248

252249
total = sum(len(ran) for ran in ranges)
253250
result = min(converter.send(i)
254-
for i in tqdm(chain(*ranges), total=total, desc="Maps"))
251+
for i in tqdm(chain(*ranges), total=total, desc="Maps", leave=False))
255252

256253
# Return the minimum value
257254
return result

Day6/input.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Time: 63 78 94 68
2+
Distance: 411 1274 2047 1035

Day6/race.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import re
2+
from sys import argv, stdin
3+
from math import prod
4+
5+
def win_races(time:int, record:int) -> int:
6+
# charge = time spent charging = speed of the boat
7+
# charge - time = remaining time
8+
distances = [ (time - charge) * charge for charge in range(0, time) ]
9+
10+
# collect elements that pass the record and return the number
11+
return sum(x > record for x in distances)
12+
13+
def parse_config(source) -> list[tuple[int, int]]:
14+
re_time = re.compile(r"^Time:\s+((?:\d+\s*)+)", re.MULTILINE)
15+
re_distance = re.compile(r"^Distance:\s+((?:\d+\s*)+)", re.MULTILINE)
16+
re_int = re.compile(r"(\d+)")
17+
18+
times = re.findall(re_int, ' '.join(re.findall(re_time, source)))
19+
distances = re.findall(re_int, ' '.join(re.findall(re_distance, source)))
20+
21+
races = zip(times, distances)
22+
return list(races)
23+
24+
def part_one(races: list[tuple[int, int]]) -> int:
25+
return prod(win_races(int(time), int(record)) for time, record in races)
26+
27+
def part_two(races: list[tuple[int, int]]) -> int:
28+
# Unpack tuples in the list, assign them to variables,
29+
# concatenate into a single string, then convert to an int
30+
time, record = zip(*races)
31+
time = int(''.join(time))
32+
record = int(''.join(record))
33+
34+
return win_races(time, record)
35+
36+
if __name__ == "__main__":
37+
if len(argv) < 2 or argv[1] == '-':
38+
print(f"Reading from stdin")
39+
text = stdin.read()
40+
else:
41+
try:
42+
with open(argv[1]) as f:
43+
print(f"Reading from {argv[1]}")
44+
text = f.read()
45+
except FileNotFoundError:
46+
print(f"Unable to find file: {argv[1]}")
47+
exit(1)
48+
49+
races = parse_config(text)
50+
51+
print(f"Part 1: {part_one(races)}")
52+
print(f"Part 2: {part_two(races)}")

README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,15 @@ Language: **Rust**
3636
---
3737
#### [Day 5](https://adventofcode.com/2023/day/5)
3838
Language: **Python**
39+
40+
[almanac.py](Day5/almanac.py) < [input.txt](Day5/input.txt)
3941
- [x] Part 1: 1181555926
40-
- [x] Part 2: 37806486 (function runtime was about 4 hours ;-;)
42+
- [x] Part 2: 37806486 (function runtime was about 4 hours ;-;)
43+
44+
---
45+
#### [Day 6](https://adventofcode.com/2023/day/6)
46+
Language: **Python**
47+
48+
[race.py](Day6/race.py) < [input.txt](Day6/input.txt)
49+
- [x] Part 1: 781200
50+
- [x] Part 2: 49240091

0 commit comments

Comments
 (0)