This commit is contained in:
2020-12-22 07:08:57 +01:00
parent 338ee35832
commit 413a8b50f2
4 changed files with 199 additions and 0 deletions

54
day22/data/data.txt Normal file
View File

@@ -0,0 +1,54 @@
Player 1:
1
10
28
29
13
11
35
7
43
8
30
25
4
5
17
32
22
39
50
46
16
26
45
38
21
Player 2:
19
40
2
12
49
23
34
47
9
14
20
24
42
37
48
44
27
6
33
18
15
3
36
41
31

14
day22/data/sample.txt Normal file
View File

@@ -0,0 +1,14 @@
Player 1:
9
2
6
3
1
Player 2:
5
8
4
7
10

1
day22/index.html Normal file
View File

@@ -0,0 +1 @@
404 Not Found

130
day22/module.py Normal file
View File

@@ -0,0 +1,130 @@
from aoc.input import get_input
import time
import copy
import itertools
import time
import collections
import re
from aoc.partselector import part_one, part_two
import functools
def pw(line):
return line.strip()
def p1():
inp = get_input(pw)
players = []
temp = []
for sample in inp:
if sample == '':
players.append(temp)
temp = []
continue
if not 'Player' in sample:
temp.append(int(sample))
while True:
i = (players[0].pop(0), players[1].pop(0))
print(i)
if i[0] > i[1]:
players[0].append(i[0])
players[0].append(i[1])
else:
players[1].append(i[1])
players[1].append(i[0])
if(len(players[0]) == 0):
break
if(len(players[1]) == 0):
break
if len(players[0]) == 0:
w = players[1]
else:
w = players[0]
su = 0
for i, x in enumerate(reversed(w), start=1):
print(i,x)
su += i*x
print(su)
return inp
globalgame = 0
def p2():
inp = get_input(pw)
players = []
temp = []
for sample in inp:
if sample == '':
players.append(temp)
temp = []
continue
if not 'Player' in sample:
temp.append(int(sample))
print(players)
final = False
wins = [0, 0]
def play(players):
global globalgame
globalgame += 1
history = set()
while all(filter(lambda x: len(x) > 0, players)):
hv = ','.join(map(str, players[0])) + ':' + ','.join(map(str, players[1]))
if hv in history:
return 0
else:
history.add(hv)
i = (players[0].pop(0), players[1].pop(0))
if i[0] <= len(players[0]) and i[1] <= len(players[1]):
players2 = [players[0][:i[0]], players[1][:i[1]]]
won = play(players2)
if won == 1:
won = 0
else:
won = 1
#print(f'cont game {game} {won}')
else:
won = i[0] > i[1]
if won:
players[0].append(i[0])
players[0].append(i[1])
else:
players[1].append(i[1])
players[1].append(i[0])
if(len(players[0]) == 0):
return 1
if(len(players[1]) == 0):
return 0
won = play(players)
if len(players[0]) == 0:
w = players[1]
else:
w = players[0]
su = 0
for i, x in enumerate(reversed(w), start=1):
#print(i,x)
su += i*x
print(su, f'(played games {globalgame})')
return 0
result1 = None
if part_one():
start = time.time()
result1 = p1()
print(round(1000*(time.time() - start), 2), 'ms')
if part_two():
start = time.time()
p2()
print(round(1000*(time.time() - start), 2), 'ms')