mirror of
https://github.com/Noettore/AdventOfCode.git
synced 2025-10-15 03:36:39 +02:00
AoC 2020: day15, refactored without using dict
Signed-off-by: Ettore Dreucci <ettore.dreucci@gmail.com>
This commit is contained in:
@@ -13,42 +13,35 @@ def read_input(input_path: str) -> str:
|
|||||||
|
|
||||||
def extract(input_data: str) -> list:
|
def extract(input_data: str) -> list:
|
||||||
"""take input data and return the appropriate data structure"""
|
"""take input data and return the appropriate data structure"""
|
||||||
entries = input_data.split(',')
|
entries = list(map(int, input_data.split(',')))
|
||||||
return entries
|
return entries
|
||||||
|
|
||||||
|
def calculate_last_spoken(numbers: list, turns: int) -> int:
|
||||||
|
"""calculate the last spoken number at specified turn"""
|
||||||
|
spoken = [0]*turns
|
||||||
|
last_spoken = -1
|
||||||
|
|
||||||
|
for turn, number in enumerate(numbers, 1):
|
||||||
|
spoken[number] = turn
|
||||||
|
last_spoken = number
|
||||||
|
|
||||||
|
for prev_turn in range(len(numbers), turns):
|
||||||
|
if spoken[last_spoken] != 0:
|
||||||
|
current_spoken = prev_turn - spoken[last_spoken]
|
||||||
|
else:
|
||||||
|
current_spoken = 0
|
||||||
|
spoken[last_spoken] = prev_turn
|
||||||
|
last_spoken = current_spoken
|
||||||
|
|
||||||
|
return last_spoken
|
||||||
|
|
||||||
def part1(entries: dict) -> int:
|
def part1(entries: dict) -> int:
|
||||||
"""part1 solver"""
|
"""part1 solver"""
|
||||||
spoken = dict()
|
return calculate_last_spoken(entries, 2020)
|
||||||
last_spoken = '-1'
|
|
||||||
for turn, number in enumerate(entries, 1):
|
|
||||||
spoken[number] = [turn]
|
|
||||||
last_spoken = number
|
|
||||||
for i in range(len(entries)+1, 2020+1):
|
|
||||||
if len(spoken.get(last_spoken, [])) < 2:
|
|
||||||
spoken.setdefault('0', []).append(i)
|
|
||||||
last_spoken = '0'
|
|
||||||
else:
|
|
||||||
number = str(int(spoken[last_spoken][-1]) - int(spoken[last_spoken][-2]))
|
|
||||||
spoken.setdefault(number, []).append(i)
|
|
||||||
last_spoken = number
|
|
||||||
return int(last_spoken)
|
|
||||||
|
|
||||||
def part2(entries: tuple) -> int:
|
def part2(entries: tuple) -> int:
|
||||||
"""part2 solver"""
|
"""part2 solver"""
|
||||||
spoken = dict()
|
return calculate_last_spoken(entries, 30000000)
|
||||||
last_spoken = '-1'
|
|
||||||
for turn, number in enumerate(entries, 1):
|
|
||||||
spoken[number] = [turn]
|
|
||||||
last_spoken = number
|
|
||||||
for i in range(len(entries)+1, 30000000+1):
|
|
||||||
if len(spoken.get(last_spoken, [])) < 2:
|
|
||||||
spoken.setdefault('0', []).append(i)
|
|
||||||
last_spoken = '0'
|
|
||||||
else:
|
|
||||||
number = str(int(spoken[last_spoken][-1]) - int(spoken[last_spoken][-2]))
|
|
||||||
spoken.setdefault(number, []).append(i)
|
|
||||||
last_spoken = number
|
|
||||||
return int(last_spoken)
|
|
||||||
|
|
||||||
def test_input_day_15():
|
def test_input_day_15():
|
||||||
"""pytest testing function"""
|
"""pytest testing function"""
|
||||||
|
@@ -24,3 +24,4 @@
|
|||||||
| [Day 12](https://adventofcode.com/2020/day/12) | [746.181µs](./2020-python/solutions/day_12.py) | [211.216µs](./2020-python/solutions/day_12.py) |
|
| [Day 12](https://adventofcode.com/2020/day/12) | [746.181µs](./2020-python/solutions/day_12.py) | [211.216µs](./2020-python/solutions/day_12.py) |
|
||||||
| [Day 13](https://adventofcode.com/2020/day/13) | [122.107µs](./2020-python/solutions/day_13.py) | [100.671µs](./2020-python/solutions/day_13.py) |
|
| [Day 13](https://adventofcode.com/2020/day/13) | [122.107µs](./2020-python/solutions/day_13.py) | [100.671µs](./2020-python/solutions/day_13.py) |
|
||||||
| [Day 14](https://adventofcode.com/2020/day/14) | [64.058ms](./2020-python/solutions/day_14.py) | [56.398ms](./2020-python/solutions/day_14.py) |
|
| [Day 14](https://adventofcode.com/2020/day/14) | [64.058ms](./2020-python/solutions/day_14.py) | [56.398ms](./2020-python/solutions/day_14.py) |
|
||||||
|
| [Day 15](https://adventofcode.com/2020/day/15) | [4.463s](./2020-python/solutions/day_15.py) | [718.712ms](./2020-python/solutions/day_15.py) |
|
||||||
|
Reference in New Issue
Block a user