Skip to content

Commit

Permalink
2018 day14
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasrenault committed Dec 20, 2024
1 parent 192b263 commit cd344cf
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions advent/advent2018/day14.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from advent.utils.utils import Advent

advent = Advent(14, 2018)


def main():
lines = advent.get_input_lines()

advent.submit(1, "".join(map(str, run(int(lines[0]))[-10:])))
advent.submit(2, find(int(lines[0])))


def find(recipes: int):
score = [3, 7]
e1, e2 = 0, 1
targetscores = tuple(map(int, str(recipes)))
targetlen = len(targetscores)
checked = 0
size = 0
while True:
s = str(score[e1] + score[e2])
size += len(s)
score.extend(int(c) for c in s)
e1 = (e1 + 1 + score[e1]) % len(score)
e2 = (e2 + 1 + score[e2]) % len(score)

while checked <= size - targetlen:
for t, r in zip(targetscores, score[checked:]):
if t != r:
break
else:
return checked

checked += 1


def run(recipes: int):
score = [3, 7]
e1, e2 = 0, 1
while len(score) < recipes + 10:
score.extend(int(c) for c in str(score[e1] + score[e2]))
e1 = (e1 + 1 + score[e1]) % len(score)
e2 = (e2 + 1 + score[e2]) % len(score)
return score


if __name__ == "__main__":
main()

0 comments on commit cd344cf

Please sign in to comment.