Skip to content

Commit 0e4e769

Browse files
committed
Added solution for part 1 of day 22
1 parent 0fae03d commit 0e4e769

File tree

3 files changed

+126
-1
lines changed

3 files changed

+126
-1
lines changed

advent2020/day22.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# MIT License
2+
#
3+
# Copyright (c) 2021 Andrew Krepps
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
24+
from . import util
25+
26+
27+
def parse_input(lines):
28+
deck = []
29+
all_decks = []
30+
for line in lines:
31+
if line.startswith("Player"):
32+
if deck:
33+
all_decks.append(deck)
34+
deck = []
35+
else:
36+
deck.insert(0, int(line))
37+
all_decks.append(deck)
38+
return all_decks
39+
40+
41+
def calculate_score(deck):
42+
score = 0
43+
for idx, card in enumerate(deck):
44+
score += (idx + 1) * card
45+
return score
46+
47+
48+
def play_game(all_decks):
49+
num_cards = sum(len(deck) for deck in all_decks)
50+
while max(len(deck) for deck in all_decks) < num_cards:
51+
played_cards = [(player_idx, deck.pop()) for player_idx, deck in enumerate(all_decks) if deck]
52+
played_cards.sort(key=lambda x: x[1], reverse=True)
53+
winning_player_idx = played_cards[0][0]
54+
for _, card in played_cards:
55+
all_decks[winning_player_idx].insert(0, card)
56+
return max(calculate_score(deck) for deck in all_decks)
57+
58+
59+
def get_part1_answer(lines):
60+
all_decks = parse_input(lines)
61+
return play_game(all_decks)
62+
63+
64+
def get_part2_answer(lines):
65+
return None
66+
67+
68+
def run():
69+
lines = util.get_input_file_lines("day22.txt")
70+
print(f"The answer to part 1 is {get_part1_answer(lines)}")
71+
print(f"The answer to part 2 is {get_part2_answer(lines)}")

main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
from advent2020 import day19
4545
from advent2020 import day20
4646
from advent2020 import day21
47+
from advent2020 import day22
4748

4849

4950
day_runners = [
@@ -67,7 +68,8 @@
6768
lambda: day18.run(),
6869
lambda: day19.run(),
6970
lambda: day20.run(),
70-
lambda: day21.run()
71+
lambda: day21.run(),
72+
lambda: day22.run()
7173
]
7274

7375

test/test_day22.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# MIT License
2+
#
3+
# Copyright (c) 2021 Andrew Krepps
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
24+
import unittest
25+
26+
from advent2020.day22 import get_part1_answer
27+
from advent2020.day22 import get_part2_answer
28+
from advent2020.util import get_input_data_lines
29+
30+
31+
data = """
32+
Player 1:
33+
9
34+
2
35+
6
36+
3
37+
1
38+
39+
Player 2:
40+
5
41+
8
42+
4
43+
7
44+
10
45+
"""
46+
47+
48+
class Day22Test(unittest.TestCase):
49+
def test_day22(self):
50+
lines = get_input_data_lines(data)
51+
self.assertEqual(get_part1_answer(lines), 306)
52+
self.assertEqual(get_part2_answer(lines), None)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy