aboutsummaryrefslogtreecommitdiff
path: root/2021/day21/21.py
blob: 8cd83079105765f84dc17451f2aa3883202da56c (plain)
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
die_val = 1
rolls = 0
def roll():
    global die_val, rolls
    x = die_val

    die_val += 1
    if die_val > 100:
        die_val = 1

    rolls += 1

    return x


class Player:
    def __init__(self, pos):
        self.pos = pos
        self.score = 0

    def move(self, roll):
        self.pos += roll
        while self.pos > 10:
            self.pos -= 10

    def take_turn(self):
        r = sum([roll() for _ in range(0, 3)])
        self.move(r)
        self.score += self.pos

    def __str__(self):
        return "<Player pos=%d score=%d>" % (self.pos, self.score)

inp = open("./input").read().split("\n")
p1_start = int(inp[0].split(": ")[1])
p2_start = int(inp[1].split(": ")[1])

p1 = Player(p1_start)
p2 = Player(p2_start)
turn = p1
while p1.score < 1000 and p2.score < 1000:
    turn.take_turn()
    if turn == p1:
        turn = p2
    else:
        turn = p1

ans = p1.score * rolls
if p1.score >= 1000:
    ans = p2.score * rolls
print("Part 1: %d" % ans)