Created
December 22, 2020 05:21
-
-
Save bluepichu/bf3d6807e6d70630d9972e93f162490b to your computer and use it in GitHub Desktop.
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
from typing import Dict, List, Mapping, Optional, Sequence, Set, Tuple, Union | |
from advent import Input | |
import re | |
from collections import defaultdict | |
from math import ceil, floor | |
input = ( | |
Input( | |
day = 22, | |
# sample = True, | |
) | |
# .all() | |
# .ints() | |
# .lines() | |
# .line_tokens() | |
.line_tokens(sep = "\n", line_sep = "\n\n") | |
# .program() | |
) | |
a = [int(x) for x in input[0][1:]] | |
b = [int(x) for x in input[1][1:]] | |
print(a, b) | |
def score(deck: List[int]): | |
ans = 0 | |
n = len(deck) | |
for i in range(n): | |
ans += (n - i) * deck[i] | |
return ans | |
# part 1 here | |
# while len(a) > 0 and len(b) > 0: | |
# aa = a[0] | |
# bb = b[0] | |
# a = a[1:] | |
# b = b[1:] | |
# if aa > bb: | |
# a += [aa, bb] | |
# else: | |
# b += [bb, aa] | |
# print(score(a)) | |
# print(score(b)) | |
# part 2 here | |
def play_game(a: List[int], b: List[int]) -> Tuple[int, int]: | |
used: Set[str] = set() | |
while len(a) > 0 and len(b) > 0: | |
key = str(a) + "|" + str(b) | |
if key in used: | |
return (1, score(a)) | |
used.add(key) | |
aa = a[0] | |
bb = b[0] | |
a = a[1:] | |
b = b[1:] | |
if len(a) < aa or len(b) < bb: | |
if aa > bb: | |
a += [aa, bb] | |
else: | |
b += [bb, aa] | |
else: | |
winner, _ = play_game(a[:aa], b[:bb]) | |
if winner == 1: | |
a += [aa, bb] | |
else: | |
b += [bb, aa] | |
if len(a) > 0: | |
return 1, score(a) | |
else: | |
return 2, score(b) | |
print(play_game(a, b)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment