mirror of
https://github.com/Crocmagnon/advent-of-code.git
synced 2024-11-22 06:28:11 +01:00
Solve day 15 (part 2 bruteforce)
This commit is contained in:
parent
cd02ef3595
commit
3fe94bbb02
7 changed files with 109 additions and 0 deletions
70
2020/day15_memory.py
Normal file
70
2020/day15_memory.py
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
import functools
|
||||||
|
import re
|
||||||
|
from typing import Dict, Iterable, List, Tuple, Union
|
||||||
|
|
||||||
|
|
||||||
|
def main(filename: str, expected_part_1: int = None, expected_part_2: int = None):
|
||||||
|
print(f"\n+ Running on {filename}")
|
||||||
|
with open(filename) as f:
|
||||||
|
starting_numbers = list(map(int, f.read().strip().split("\n")[0].split(",")))
|
||||||
|
|
||||||
|
counter_part_1 = solve_part_1(starting_numbers)
|
||||||
|
|
||||||
|
print(f"1. Found {counter_part_1}")
|
||||||
|
if expected_part_1:
|
||||||
|
assert expected_part_1 == counter_part_1
|
||||||
|
|
||||||
|
counter_part_2 = solve_part_2(starting_numbers)
|
||||||
|
print(f"2. Found {counter_part_2}")
|
||||||
|
if expected_part_2:
|
||||||
|
assert expected_part_2 == counter_part_2
|
||||||
|
|
||||||
|
|
||||||
|
SeenTuple = Union[Tuple[int], Tuple[int, int]]
|
||||||
|
Seen = Dict[int, SeenTuple]
|
||||||
|
|
||||||
|
|
||||||
|
def solve_part_1(starting_numbers):
|
||||||
|
return solve(starting_numbers, 2020)
|
||||||
|
|
||||||
|
|
||||||
|
def solve(starting_numbers, limit):
|
||||||
|
seen = dict() # type: Seen
|
||||||
|
previous = 0
|
||||||
|
for turn, current in enumerate(starting_numbers):
|
||||||
|
seen[current] = (turn,)
|
||||||
|
previous = current
|
||||||
|
previous_first_time = True
|
||||||
|
for turn in range(len(starting_numbers), limit):
|
||||||
|
if previous_first_time:
|
||||||
|
current = 0
|
||||||
|
else:
|
||||||
|
current = seen[previous][0] - seen[previous][1]
|
||||||
|
seen[current] = seen_tuple(seen, current, turn)
|
||||||
|
previous = current
|
||||||
|
previous_first_time = first_time_seen(seen, previous)
|
||||||
|
return previous
|
||||||
|
|
||||||
|
|
||||||
|
def first_time_seen(seen: Seen, previous: int) -> bool:
|
||||||
|
return len(seen[previous]) == 1
|
||||||
|
|
||||||
|
|
||||||
|
def seen_tuple(seen: Seen, current: int, turn: int) -> SeenTuple:
|
||||||
|
try:
|
||||||
|
existing = seen[current]
|
||||||
|
return turn, existing[0]
|
||||||
|
except KeyError:
|
||||||
|
return (turn,)
|
||||||
|
|
||||||
|
|
||||||
|
def solve_part_2(starting_numbers):
|
||||||
|
return solve(starting_numbers, 30_000_000)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main("inputs/day15-test1", 436, 175594)
|
||||||
|
main("inputs/day15-test2", 1, 2578)
|
||||||
|
main("inputs/day15-test6", 438, 18)
|
||||||
|
main("inputs/day15-test7", 1836, 362)
|
||||||
|
main("inputs/day15", 595, 1708310)
|
1
2020/inputs/day15
Normal file
1
2020/inputs/day15
Normal file
|
@ -0,0 +1 @@
|
||||||
|
1,17,0,10,18,11,6
|
1
2020/inputs/day15-test1
Normal file
1
2020/inputs/day15-test1
Normal file
|
@ -0,0 +1 @@
|
||||||
|
0,3,6
|
1
2020/inputs/day15-test2
Normal file
1
2020/inputs/day15-test2
Normal file
|
@ -0,0 +1 @@
|
||||||
|
1,3,2
|
1
2020/inputs/day15-test6
Normal file
1
2020/inputs/day15-test6
Normal file
|
@ -0,0 +1 @@
|
||||||
|
3,2,1
|
1
2020/inputs/day15-test7
Normal file
1
2020/inputs/day15-test7
Normal file
|
@ -0,0 +1 @@
|
||||||
|
3,1,2
|
34
2020/template.py
Normal file
34
2020/template.py
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
import functools
|
||||||
|
import re
|
||||||
|
from typing import Dict, Iterable, List, Union
|
||||||
|
|
||||||
|
|
||||||
|
def main(filename: str, expected_part_1: int = None, expected_part_2: int = None):
|
||||||
|
print(f"\n+ Running on {filename}")
|
||||||
|
with open(filename) as f:
|
||||||
|
data = f.read().strip().split("\n")
|
||||||
|
|
||||||
|
counter_part_1 = solve_part_1(data)
|
||||||
|
|
||||||
|
print(f"1. Found {counter_part_1}")
|
||||||
|
if expected_part_1:
|
||||||
|
assert expected_part_1 == counter_part_1
|
||||||
|
|
||||||
|
counter_part_2 = solve_part_2(data)
|
||||||
|
print(f"2. Found {counter_part_2}")
|
||||||
|
if expected_part_2:
|
||||||
|
assert expected_part_2 == counter_part_2
|
||||||
|
|
||||||
|
|
||||||
|
def solve_part_1(data):
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
def solve_part_2(data):
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main("inputs/day14-test1", 436)
|
||||||
|
main("inputs/day14-test2", 1)
|
||||||
|
main("inputs/day14")
|
Loading…
Reference in a new issue