Skip to content

Commit 4aa9799

Browse files
committed
Added day 2021-21
1 parent ed8a2e2 commit 4aa9799

File tree

1 file changed

+154
-0
lines changed

1 file changed

+154
-0
lines changed

2021/21-Dirac Dice.py

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
# -------------------------------- Input data ---------------------------------------- #
2+
import os, grid, graph, dot, assembly, re, itertools, copy, functools
3+
from collections import Counter, deque, defaultdict
4+
5+
from compass import *
6+
7+
8+
# This functions come from https://github.com/mcpower/adventofcode - Thanks!
9+
def lmap(func, *iterables):
10+
return list(map(func, *iterables))
11+
12+
13+
def ints(s: str):
14+
return lmap(int, re.findall(r"-?\d+", s)) # thanks mserrano!
15+
16+
17+
def positive_ints(s: str):
18+
return lmap(int, re.findall(r"\d+", s)) # thanks mserrano!
19+
20+
21+
def floats(s: str):
22+
return lmap(float, re.findall(r"-?\d+(?:\.\d+)?", s))
23+
24+
25+
def positive_floats(s: str):
26+
return lmap(float, re.findall(r"\d+(?:\.\d+)?", s))
27+
28+
29+
def words(s: str):
30+
return re.findall(r"[a-zA-Z]+", s)
31+
32+
33+
test_data = {}
34+
35+
test = 1
36+
test_data[test] = {
37+
"input": """Player 1 starting position: 4
38+
Player 2 starting position: 8""",
39+
"expected": ["745 * 993 = 739785", "444356092776315"],
40+
}
41+
42+
test = "real"
43+
input_file = os.path.join(
44+
os.path.dirname(__file__),
45+
"Inputs",
46+
os.path.basename(__file__).replace(".py", ".txt"),
47+
)
48+
test_data[test] = {
49+
"input": open(input_file, "r+").read(),
50+
"expected": ["920580", "Unknown"],
51+
}
52+
53+
54+
# -------------------------------- Control program execution ------------------------- #
55+
56+
case_to_test = "real"
57+
part_to_test = 2
58+
59+
# -------------------------------- Initialize some variables ------------------------- #
60+
61+
puzzle_input = test_data[case_to_test]["input"]
62+
puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1]
63+
puzzle_actual_result = "Unknown"
64+
65+
66+
# -------------------------------- Actual code execution ----------------------------- #
67+
68+
# Conver integer to 36-character binary
69+
# str_value = "{0:>036b}".format(value)
70+
# Convert binary string to number
71+
# value = int(str_value, 2)
72+
73+
74+
p1_pos = ints(puzzle_input)[1]
75+
p2_pos = ints(puzzle_input)[3]
76+
if part_to_test == 1:
77+
p1_score = 0
78+
p2_score = 0
79+
i = 0
80+
while p1_score < 1000 and p2_score < 1000:
81+
p1_pos += 8 * i + 6 # real= 18*i+6, but 18%10==8
82+
p1_pos = (p1_pos - 1) % 10 + 1
83+
p1_score += p1_pos
84+
85+
if p1_score >= 1000:
86+
i += 0.5
87+
break
88+
p2_pos += 8 * i + 5 # real = 18*n+15
89+
p2_pos = (p2_pos - 1) % 10 + 1
90+
p2_score += p2_pos
91+
92+
print(i, p1_pos, p1_score, p2_pos, p2_score)
93+
94+
i += 1
95+
96+
puzzle_actual_result = int(min(p1_score, p2_score) * 6 * i)
97+
98+
99+
else:
100+
steps = defaultdict(int)
101+
steps[(0, p1_pos, 0, p2_pos, 0)] = 1
102+
probabilities = dict(
103+
Counter([i + j + k + 3 for i in range(3) for j in range(3) for k in range(3)])
104+
)
105+
universes = [0] * 2
106+
107+
print(probabilities)
108+
print(steps)
109+
110+
i = 0
111+
max_len = 0
112+
while steps:
113+
i += 1
114+
step, frequency = next(iter(steps.items()))
115+
del steps[step]
116+
player = step[-1]
117+
# print ('Player', player, 'plays from', step, frequency)
118+
for dice_score, proba in probabilities.items():
119+
new_step = list(step)
120+
121+
# Add dice to position
122+
new_step[player * 2 + 1] += dice_score
123+
new_step[player * 2 + 1] = (new_step[player * 2 + 1] - 1) % 10 + 1
124+
125+
# Add position to score
126+
new_step[player * 2] += new_step[player * 2 + 1]
127+
128+
if new_step[player * 2] >= 21:
129+
# print ('Adding', frequency * proba, 'to', player)
130+
universes[player] += frequency * proba
131+
else:
132+
new_step[-1] = 1 - new_step[-1]
133+
# print ('Player', player, 'does', new_step, frequency, proba)
134+
steps[tuple(new_step)] += frequency * proba
135+
136+
# print (steps.values())
137+
# if i == 30:
138+
# break
139+
140+
# print (len(steps), universes)
141+
max_len = max(len(steps), max_len)
142+
# print (max_len)
143+
144+
puzzle_actual_result = max(universes)
145+
146+
147+
# -------------------------------- Outputs / results --------------------------------- #
148+
149+
print("Case :", case_to_test, "- Part", part_to_test)
150+
print("Expected result : " + str(puzzle_expected_result))
151+
print("Actual result : " + str(puzzle_actual_result))
152+
# Date created: 2021-12-21 08:13:41.813570
153+
# Part 1: 2021-12-21 08:41:31
154+
# Part 1: 2021-12-21 09:35:03

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