Browse Source

day 22 part 1

Juan José Gutiérrez de Quevedo Pérez 1 year ago
parent
commit
264496e0e1
2 changed files with 94 additions and 0 deletions
  1. 41 0
      22/1.py
  2. 53 0
      22/input

+ 41 - 0
22/1.py

@@ -0,0 +1,41 @@
+with open("input") as f:
+    content = [x.strip() for x in f]
+
+player1 = []
+player2 = []
+part = 0
+for c in content:
+    if not c:
+        part += 1
+        continue
+    if part == 0: # reading player 2 stack
+        if c.startswith("Player"):
+            continue
+        player1.append(int(c))
+    if part == 1: # reading player 1 stack
+        if c.startswith("Player"):
+            continue
+        player2.append(int(c))
+
+while len(player1) and len(player2):
+    if player1[0] > player2[0]:
+        player1.append(player1[0])
+        player1.append(player2[0])
+        del(player1[0])
+        del(player2[0])
+    else:
+        player2.append(player2[0])
+        player2.append(player1[0])
+        del(player1[0])
+        del(player2[0])
+
+if len(player1):
+    winner = player1
+else:
+    winner = player2
+
+result = 0
+for i in range(len(winner)):
+    result += (len(winner) - i ) * winner[i]
+
+print(result)

+ 53 - 0
22/input

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