aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2023-12-04 06:20:23 +0100
committerMarvin Borner2023-12-04 06:20:23 +0100
commit135a2f84d2929743b8d83074e277702d02f9281a (patch)
treea19b683511b9d9522705aab0ef8b9362c4d233f5
parent15926e6c3031cc576879e6885cf0bff96fa1e8ad (diff)
tired
-rw-r--r--2023/04/solve.py43
1 files changed, 43 insertions, 0 deletions
diff --git a/2023/04/solve.py b/2023/04/solve.py
new file mode 100644
index 0000000..ea3c7fd
--- /dev/null
+++ b/2023/04/solve.py
@@ -0,0 +1,43 @@
+import re
+
+L = [re.sub(" +", " ", l.strip()) for l in open("input").readlines()]
+
+
+def solve1():
+ res = 0
+ for l in L:
+ ls = l.split(": ")
+ card = ls[0]
+ winning, have = [
+ list(map(int, a.split(" "))) for a in ls[1].split(" | ")
+ ]
+ s = 0
+ i = True
+ for x in have:
+ if x in winning:
+ s = s + 1 if i else s * 2
+ i = False
+ res += s
+ print(res)
+
+
+def solve2():
+ res = 0
+ dup = [1] * len(L)
+ for i, l in enumerate(L):
+ ls = l.split(": ")
+ card = ls[0]
+ winning, have = [
+ list(map(int, a.split(" "))) for a in ls[1].split(" | ")
+ ]
+ s = 0
+ for x in have:
+ if x in winning:
+ s += 1
+ for j in range(i + 1, i + s + 1):
+ dup[j] += dup[i]
+ print(sum(dup))
+
+
+solve1()
+solve2()