Skip to content

Commit a69e56a

Browse files
author
Jan Verner
committed
AoC23 - Day 7
1 parent ec9c89f commit a69e56a

File tree

5 files changed

+68
-19
lines changed

5 files changed

+68
-19
lines changed

src/main/python/aoc23/day05/day05.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def lookup_min_location(seed_range):
5252
seed = seed_range[0]
5353
min_location = sys.maxsize
5454
while seed < seed_range[0] + seed_range[1]:
55-
log.debug(f"Checking seed {seed}")
55+
log.info(f"Checking seed {seed}")
5656
n = seed # seed -> lookups -> location
5757
max_jump = sys.maxsize
5858
for map_name, ranges in mappings.items():
@@ -81,7 +81,7 @@ def lookup_min_location(seed_range):
8181

8282
if __name__ == "__main__":
8383
log.setLevel(logging.INFO)
84-
timed_run("Star 1", lambda: star1(read_input(__file__)))
84+
# timed_run("Star 1", lambda: star1(read_input(__file__)))
8585
timed_run("Star 2", lambda: star2(read_input(__file__)))
8686

8787
# Star 1: 457535844

src/main/python/aoc23/day06/day06.py

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,49 @@
11
import logging
2+
import math
3+
from typing import List, Tuple
24

3-
from util.data_io import read_input, read_test_input, timed_run
5+
from util.data_io import timed_run
46
from util.log import log
57

68

7-
def star1(lines: list[str]):
9+
def star1(puzzle_input: List[Tuple[int, int]]):
810
"""
9-
>>> star1(read_test_input(__file__))
10-
11+
>>> star1([(7, 9), (15, 40), (30, 200)])
12+
288
1113
"""
12-
for line in lines:
13-
pass
14-
15-
16-
def star2(lines: list[str]):
14+
total = 1
15+
for time, distance in puzzle_input:
16+
log.debug(f"Solving time: {time}, current record: {distance}")
17+
boundary = _find_boundary(distance, time)
18+
subtotal = (math.ceil((time / 2)) - boundary) * 2
19+
if time % 2 == 0:
20+
subtotal += 1
21+
log.debug(f"Solution is {subtotal}")
22+
total *= subtotal
23+
return total
24+
25+
26+
def _find_boundary(distance, time):
27+
start_from = distance // time # low hanging optimization
28+
for i in range(start_from, time):
29+
if i * (time - i) > distance:
30+
log.debug(f"Boundary found {i} -> {i * (time - i)}")
31+
return i
32+
33+
34+
def star2(puzzle_input: Tuple[int, int]):
1735
"""
18-
>>> star2(read_test_input(__file__))
19-
36+
>>> star2((71530, 940200))
37+
71503
2038
"""
21-
for line in lines:
22-
pass
39+
return star1([puzzle_input])
2340

2441

2542
if __name__ == "__main__":
2643
log.setLevel(logging.INFO)
27-
timed_run("Star 1", lambda: star1(read_input(__file__)))
28-
timed_run("Star 2", lambda: star2(read_input(__file__)))
2944

30-
# Star 1:
31-
# Star 2:
45+
timed_run("Star 1", lambda: star1([(62, 644), (73, 1023), (75, 1240), (65, 1023)]))
46+
timed_run("Star 2", lambda: star2((62737565, 644102312401023)))
47+
48+
# Star 1: 393120
49+
# Star 2: 36872656

src/main/python/aoc23/day07/day07.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import logging
2+
3+
from util.data_io import read_input, read_test_input, timed_run
4+
from util.log import log
5+
6+
7+
def star1(lines: list[str]):
8+
"""
9+
>>> star1(read_test_input(__file__))
10+
11+
"""
12+
for line in lines:
13+
pass
14+
15+
16+
def star2(lines: list[str]):
17+
"""
18+
>>> star2(read_test_input(__file__))
19+
20+
"""
21+
for line in lines:
22+
pass
23+
24+
25+
if __name__ == "__main__":
26+
log.setLevel(logging.INFO)
27+
timed_run("Star 1", lambda: star1(read_input(__file__)))
28+
timed_run("Star 2", lambda: star2(read_input(__file__)))
29+
30+
# Star 1:
31+
# Star 2:

0 commit comments

Comments
 (0)