-
Notifications
You must be signed in to change notification settings - Fork 1
/
19.py
39 lines (35 loc) · 1.18 KB
/
19.py
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
"""--- Day 19: An Elephant Named Joseph ---"""
def solve_1(elves):
circle = list(map(lambda x: [1, 1+x], range(elves)))
while len(circle) > 1:
for i in range(0, len(circle)):
p = circle[i]
if p[0] > 0:
# get second elf
p2 = circle[(i+1) % len(circle)]
# steal presents
p2[0] = 0
# only need to update once we've been around once
circle = [e for e in circle if e[0] > 0]
return circle[0][1]
def solve_2(elves):
circle = list(map(lambda x: 1+x, range(elves)))
# start at first elf
i = 0
while len(circle) > 1:
# find elf opposite
opposite = (i + int(len(circle) / 2)) % len(circle)
# remove that elf
circle.pop(opposite)
# determine next elf in circle
if i == len(circle):
i = 0 # last elf, next is first
elif i < opposite:
i += 1 # we removed elf ahead of us
else:
continue # we removed an elf before us
return circle[0]
with open("input/19.txt", "r") as f:
text = int(f.readline())
print(solve_1(text))
print(solve_2(text))