diff options
author | Marvin Borner | 2023-12-04 06:50:26 +0100 |
---|---|---|
committer | Marvin Borner | 2023-12-04 06:50:26 +0100 |
commit | 8785821aec6ab28de2bf5e802284795e2405f4d6 (patch) | |
tree | e49b38564acc3dea26308052809c022a651d3f8b /2023/04/solve.py | |
parent | 135a2f84d2929743b8d83074e277702d02f9281a (diff) |
slightly cleaner
Diffstat (limited to '2023/04/solve.py')
-rw-r--r-- | 2023/04/solve.py | 46 |
1 files changed, 11 insertions, 35 deletions
diff --git a/2023/04/solve.py b/2023/04/solve.py index ea3c7fd..1391b06 100644 --- a/2023/04/solve.py +++ b/2023/04/solve.py @@ -1,43 +1,19 @@ -import re +L = [" ".join(l.split()) for l in open("input").readlines()] -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) +def solve(): + p1 = 0 + p2 = [1] * len(L) for i, l in enumerate(L): - ls = l.split(": ") - card = ls[0] + card, nums = l.split(": ") winning, have = [ - list(map(int, a.split(" "))) for a in ls[1].split(" | ") + list(map(int, a.split(" "))) for a in nums.split(" | ") ] - s = 0 - for x in have: - if x in winning: - s += 1 + s = sum(1 for x in have if x in winning) for j in range(i + 1, i + s + 1): - dup[j] += dup[i] - print(sum(dup)) + p2[j] += p2[i] + p1 += 0 if s == 0 else 2 ** (s - 1) + print(p1, sum(p2)) -solve1() -solve2() +solve() |