From 0a4b8a70a944592db49da2e7f7d89b07726f69f5 Mon Sep 17 00:00:00 2001 From: setop Date: Thu, 22 Dec 2022 02:13:03 +0100 Subject: [PATCH] day 20, double linked list, pypy to make it fast --- d20/part1.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ d20/part2.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 d20/part1.py create mode 100644 d20/part2.py diff --git a/d20/part1.py b/d20/part1.py new file mode 100644 index 0000000..4b4455a --- /dev/null +++ b/d20/part1.py @@ -0,0 +1,49 @@ +from __future__ import annotations +import sys +from dataclasses import dataclass + +@dataclass +class E: + v: int + p: E + n: E + +V0 = None +p = None +L = [] +for l in sys.stdin.read().splitlines(): + e = E(int(l),p,None) + L.append(e) + if l == "0": + V0 = e + if p is not None: + p.n = e + p = e +L[-1].n = L[0] +L[0].p = L[-1] + +def swap(l,m): + k = l.p + n = m.n + k.n = m + n.p = l + l.n = n + m.p = k + l.p = m + m.n = l + +for l in L: + if l.v > 0: + for _ in range(l.v): + swap(l,l.n) + elif l.v < 0: + for _ in range(-l.v): + swap(l.p,l) + +S = 0 +s = V0 +for i in range(3000+1): + if i in [1000,2000,3000]: + S += s.v + s = s.n +print(S) diff --git a/d20/part2.py b/d20/part2.py new file mode 100644 index 0000000..e33aa6d --- /dev/null +++ b/d20/part2.py @@ -0,0 +1,51 @@ +from __future__ import annotations +import sys +from dataclasses import dataclass + +@dataclass +class E: + v: int + p: E + n: E + +DEK = 811589153 +V0 = None +p = None +L = [] +for l in sys.stdin.read().splitlines(): + e = E(int(l)*DEK,p,None) + L.append(e) + if l == "0": + V0 = e + if p is not None: + p.n = e + p = e +L[-1].n = L[0] +L[0].p = L[-1] + +def swap(l,m): + k = l.p + n = m.n + k.n = m + n.p = l + l.n = n + m.p = k + l.p = m + m.n = l + +for _ in range(10): + for l in L: + if l.v > 0: + for _ in range(l.v%(len(L)-1)): + swap(l,l.n) + elif l.v < 0: + for _ in range(-l.v%(len(L)-1)): + swap(l.p,l) + +S = 0 +s = V0 +for i in range(3000+1): + if i in [1000,2000,3000]: + S += s.v + s = s.n +print(S)