Created
December 13, 2022 16:06
-
-
Save halitanildonmez/c79ad45478e8efc9acd31d5331ebaf5f to your computer and use it in GitHub Desktop.
Advent of Code 2020 day 15
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import collections | |
import itertools | |
import re | |
import ast | |
import functools | |
T = "bb.txt" | |
M = "aa.txt" | |
file1 = open(M, 'r') | |
Lines = file1.read().splitlines() | |
main_input = [0, 1, 4, 13, 15, 12, 16] | |
test_input = [0, 3, 6] | |
def part1(): | |
test = main_input | |
max_turn = 30000000 | |
turn = len(test) + 1 | |
last_spoken = test[-1] | |
ledger = {} | |
for i, e in enumerate(test): | |
ledger[e] = [i+1] | |
while turn <= max_turn: | |
if last_spoken in ledger and len(ledger[last_spoken]) == 1: | |
last_spoken = 0 | |
elif last_spoken in ledger and len(ledger[last_spoken]) > 1: | |
vals = ledger[last_spoken] | |
last_spoken = vals[-1] - vals[-2] | |
if last_spoken in ledger: | |
ledger[last_spoken].append(turn) | |
else: | |
ledger[last_spoken] = [turn] | |
turn += 1 | |
return last_spoken | |
print(part1()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment