Add 2022 solutions
This commit is contained in:
73
2022/d20.py
Normal file
73
2022/d20.py
Normal file
@@ -0,0 +1,73 @@
|
||||
from lib import *
|
||||
|
||||
EXAMPLE = """1
|
||||
2
|
||||
-3
|
||||
3
|
||||
-2
|
||||
0
|
||||
4
|
||||
"""
|
||||
|
||||
|
||||
def move(xs, i, move):
|
||||
move = move % (len(xs) - 1)
|
||||
ni = (i + move + 1) % len(xs)
|
||||
xsi, xs[i] = xs[i], None
|
||||
xs.insert(ni, xsi)
|
||||
xs.remove(None)
|
||||
return xs
|
||||
|
||||
|
||||
def asserts():
|
||||
assert move([1, 2, 3], 0, 0) == [1, 2, 3]
|
||||
assert move([1, 2, 3], 0, 1) == [2, 1, 3]
|
||||
assert move([1, 2, 3], 0, 2) == [1, 2, 3]
|
||||
assert move([1, 2, 3], 0, 3) == [2, 1, 3]
|
||||
assert move([1, 2, 3], 0, 4) == [1, 2, 3]
|
||||
assert move([1, 2, 3], 0, 5) == [2, 1, 3]
|
||||
assert move([1, 2, 3], 0, 2) == [1, 2, 3]
|
||||
assert move([3, 2, 1], 2, 1) == [3, 1, 2]
|
||||
assert move([3, 2, 1], 2, 2) == [1, 3, 2]
|
||||
|
||||
assert move([4, -2, 5, 6, 7, 8, 9,], 1, -2) == [4, 5, 6, 7, 8, -2, 9]
|
||||
assert move([1, 2, -2, -3, 0, 3, 4], 2, -2) == [-2, 1, 2, -3, 0, 3, 4]
|
||||
|
||||
|
||||
def solve(input: Input, second=False):
|
||||
xs = list(map(int, input.lines()))
|
||||
if second:
|
||||
xs = list(map(lambda n: n * 811589153, xs))
|
||||
ys = list(range(len(xs)))
|
||||
oys = ys[:]
|
||||
|
||||
repeat = 1
|
||||
if second:
|
||||
repeat = 10
|
||||
for _ in range(repeat):
|
||||
for o in oys:
|
||||
i = ys.index(o)
|
||||
movev = xs[o]
|
||||
move(ys, i, movev)
|
||||
|
||||
xs = [xs[i] for i in ys]
|
||||
s = 0
|
||||
i0 = xs.index(0)
|
||||
for i in range(1, 4):
|
||||
ni = (i0 + i * 1000) % len(ys)
|
||||
s += xs[ni]
|
||||
return s
|
||||
|
||||
|
||||
def main():
|
||||
DAY_INPUT = "d20.txt"
|
||||
print("Example 1:", solve(Input(EXAMPLE)))
|
||||
print("Solution 1:", solve(Input(DAY_INPUT)))
|
||||
print("Example 2:", solve(Input(EXAMPLE), True))
|
||||
print("Solution 2:", solve(Input(DAY_INPUT), True))
|
||||
assert solve(Input(DAY_INPUT), True) == 6704537992933
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asserts()
|
||||
main()
|
||||
Reference in New Issue
Block a user