-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday21_task2.py
More file actions
59 lines (41 loc) · 1.28 KB
/
Copy pathday21_task2.py
File metadata and controls
59 lines (41 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
###############################################################################
# Day x, Task y #
###############################################################################
import aoc_util
day = 21
data_str = """Player 1 starting position: 4
Player 2 starting position: 8"""
dice_roll_to_n = {
3: 1,
4: 3,
5: 6,
6: 7,
7: 6,
8: 3,
9: 1
}
def play(n, pos_1, pos_2, score_1, score_2):
if score_2 >= 21:
return 0, n
player_1_wins = 0
player_2_wins = 0
for p in range(3, 10):
new_pos_1 = (pos_1 + p) % 10
new_score_1 = score_1 + new_pos_1 + 1
player_2_wins_tmp, player_1_wins_tmp = play(
n * dice_roll_to_n[p],
pos_2,
new_pos_1,
score_2,
new_score_1
)
player_1_wins += player_1_wins_tmp
player_2_wins += player_2_wins_tmp
return player_1_wins, player_2_wins
def task(data_set: list[str]) -> int:
pos_1 = int(data_set[0][-1]) - 1
pos_2 = int(data_set[1][-1]) - 1
player_1_wins, player_2_wins = play(1, pos_1, pos_2, 0, 0)
return max(player_1_wins, player_2_wins)
aoc_util.run_with_data_str(task, data_str)
aoc_util.run_with_data_set(task, day)