From 413a8b50f2946fdf78aa7571425dc7d163b5f01e Mon Sep 17 00:00:00 2001 From: Matthias Bilger Date: Tue, 22 Dec 2020 07:08:57 +0100 Subject: [PATCH] day22 --- day22/data/data.txt | 54 ++++++++++++++++++ day22/data/sample.txt | 14 +++++ day22/index.html | 1 + day22/module.py | 130 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 199 insertions(+) create mode 100644 day22/data/data.txt create mode 100644 day22/data/sample.txt create mode 100644 day22/index.html create mode 100644 day22/module.py diff --git a/day22/data/data.txt b/day22/data/data.txt new file mode 100644 index 0000000..b941972 --- /dev/null +++ b/day22/data/data.txt @@ -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 + diff --git a/day22/data/sample.txt b/day22/data/sample.txt new file mode 100644 index 0000000..063a5c7 --- /dev/null +++ b/day22/data/sample.txt @@ -0,0 +1,14 @@ +Player 1: +9 +2 +6 +3 +1 + +Player 2: +5 +8 +4 +7 +10 + diff --git a/day22/index.html b/day22/index.html new file mode 100644 index 0000000..44d986c --- /dev/null +++ b/day22/index.html @@ -0,0 +1 @@ +404 Not Found diff --git a/day22/module.py b/day22/module.py new file mode 100644 index 0000000..934b941 --- /dev/null +++ b/day22/module.py @@ -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') + +