From 52623631c7b79ff1171ea2106a7fceb0ac7cecb8 Mon Sep 17 00:00:00 2001 From: Matthias Bilger Date: Tue, 22 Dec 2020 07:14:57 +0100 Subject: [PATCH] frozen set --- day22/module.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/day22/module.py b/day22/module.py index 934b941..d11f600 100644 --- a/day22/module.py +++ b/day22/module.py @@ -53,6 +53,7 @@ def p1(): globalgame = 0 +mdepth = 0 def p2(): inp = get_input(pw) players = [] @@ -67,12 +68,15 @@ def p2(): print(players) final = False wins = [0, 0] - def play(players): + def play(players, depth=0): global globalgame + global mdepth + mdepth = max(mdepth, depth) globalgame += 1 history = set() while all(filter(lambda x: len(x) > 0, players)): - hv = ','.join(map(str, players[0])) + ':' + ','.join(map(str, players[1])) + hv = frozenset([frozenset(players[0]), frozenset(players[1])]) + # ','.join(map(str, players[0])) + ':' + ','.join(map(str, players[1])) if hv in history: return 0 else: @@ -80,7 +84,7 @@ def p2(): 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) + won = play(players2, depth + 1) if won == 1: won = 0 else: @@ -99,7 +103,7 @@ def p2(): return 1 if(len(players[1]) == 0): return 0 - won = play(players) + won = play(players, 1) if len(players[0]) == 0: @@ -112,7 +116,7 @@ def p2(): #print(i,x) su += i*x - print(su, f'(played games {globalgame})') + print(su, f'(played games {globalgame}/{mdepth})') return 0 result1 = None