From f5df4c2e4ae0a59ec2fb0749d4e9fb4df00f4255 Mon Sep 17 00:00:00 2001 From: Alfred Melch Date: Wed, 23 Dec 2020 14:53:08 +0100 Subject: [PATCH] Solve 2020/22 --- 2020/22/example | 13 +++++++++ 2020/22/example2 | 9 ++++++ 2020/22/input | 53 +++++++++++++++++++++++++++++++++++ 2020/22/solution1.py | 38 +++++++++++++++++++++++++ 2020/22/solution2.py | 66 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 179 insertions(+) create mode 100644 2020/22/example create mode 100644 2020/22/example2 create mode 100644 2020/22/input create mode 100644 2020/22/solution1.py create mode 100644 2020/22/solution2.py diff --git a/2020/22/example b/2020/22/example new file mode 100644 index 0000000..391cd24 --- /dev/null +++ b/2020/22/example @@ -0,0 +1,13 @@ +Player 1: +9 +2 +6 +3 +1 + +Player 2: +5 +8 +4 +7 +10 diff --git a/2020/22/example2 b/2020/22/example2 new file mode 100644 index 0000000..b0fae37 --- /dev/null +++ b/2020/22/example2 @@ -0,0 +1,9 @@ +Player 1: +43 +19 + +Player 2: +2 +29 +14 + diff --git a/2020/22/input b/2020/22/input new file mode 100644 index 0000000..4aed5fe --- /dev/null +++ b/2020/22/input @@ -0,0 +1,53 @@ +Player 1: +31 +24 +5 +33 +7 +12 +30 +22 +48 +14 +16 +26 +18 +45 +4 +42 +25 +20 +46 +21 +40 +38 +34 +17 +50 + +Player 2: +1 +3 +41 +8 +37 +35 +28 +39 +43 +29 +10 +27 +11 +36 +49 +32 +2 +23 +19 +9 +13 +15 +47 +6 +44 diff --git a/2020/22/solution1.py b/2020/22/solution1.py new file mode 100644 index 0000000..e89b47d --- /dev/null +++ b/2020/22/solution1.py @@ -0,0 +1,38 @@ + +from collections import deque + + +p1 = deque() +p2 = deque() + + +with open('input', 'r') as f: + f.readline() # player 1 + while (line := f.readline()) and line != '\n': + p1.append(int(line)) + f.readline() # player 2 + while (line := f.readline()) and line != '\n': + p2.append(int(line)) + + +print(p1) +print(p2) + +while len(p1) and len(p2): + a = p1.popleft() + b = p2.popleft() + winner = p1 if a > b else p2 + winner.append(max(a, b)) + winner.append(min(a, b)) + +print(p1) +print(p2) + +winner = p1 if len(p1) else p2 +i = 1 +score = 0 +while len(winner): + score += winner.pop() * i + i += 1 + +print(score) diff --git a/2020/22/solution2.py b/2020/22/solution2.py new file mode 100644 index 0000000..01a7863 --- /dev/null +++ b/2020/22/solution2.py @@ -0,0 +1,66 @@ + +from collections import deque + + +p1 = deque() +p2 = deque() + + +with open('input', 'r') as f: + f.readline() # player 1 + while (line := f.readline()) and line != '\n': + p1.append(int(line)) + f.readline() # player 2 + while (line := f.readline()) and line != '\n': + p2.append(int(line)) + +print('p1:', p1) +print('p2:', p2) + + +def game(p1, p2): + rounds = set() + while len(p1) and len(p2): + # prevent infinite games + round_hash = str(p1) + str(p2) + if round_hash in rounds: + return 1 + rounds.add(round_hash) + + a = p1.popleft() + b = p2.popleft() + + # check if each player has enough cards for recursion + if len(p1) >= a and len(p2) >= b: + new_p1 = p1.copy() + new_p2 = p2.copy() + while len(new_p1) > a: + new_p1.pop() + while len(new_p2) > b: + new_p2.pop() + winner = game(new_p1, new_p2) + else: + # normal round + winner = 1 if a > b else 2 + + # append cards to winner + winning_player = p1 if winner == 1 else p2 + winning_player.append(a if winner == 1 else b) + winning_player.append(b if winner == 1 else a) + return 1 if len(p1) else 2 + + +game(p1, p2) + +print('After game:') +print('p1:', p1) +print('p2:', p2) +winner = p1 if len(p1) else p2 +i = 1 +score = 0 +while len(winner): + score += winner.pop() * i + i += 1 + +print('Score:') +print(score)