Further optimise day 4
This commit is contained in:
parent
2194015259
commit
4dcc750416
1 changed files with 23 additions and 20 deletions
43
day04.py
43
day04.py
|
@ -1,24 +1,27 @@
|
||||||
import fileinput
|
import fileinput
|
||||||
|
|
||||||
#
|
def main():
|
||||||
# Part 1
|
#
|
||||||
#
|
# Part 1
|
||||||
cards = []
|
#
|
||||||
part1 = 0
|
cards = []
|
||||||
for i, line in enumerate(fileinput.input()):
|
part1 = 0
|
||||||
card = line.rstrip().split(": ")[1]
|
for line in fileinput.input():
|
||||||
(winning, owned) = ({int(i) for i in h.split()} for h in card.split(" | "))
|
card = line.split(": ", 1)[1]
|
||||||
count = len(winning & owned)
|
(winning, owned) = ({int(i) for i in h.split()} for h in card.split(" | ", 1))
|
||||||
cards.append(count)
|
count = len(winning & owned)
|
||||||
part1 += 1 << count - 1 if count else 0
|
cards.append(count)
|
||||||
|
part1 += 1 << count - 1 if count else 0
|
||||||
|
|
||||||
#
|
#
|
||||||
# Part 2
|
# Part 2
|
||||||
#
|
#
|
||||||
dp = [0] * len(cards)
|
dp = [0] * len(cards)
|
||||||
part2 = 0
|
part2 = 0
|
||||||
for i in range(len(cards)):
|
for i in range(len(cards)):
|
||||||
dp[i] = 1 + sum(dp[i - cards[-i - 1] : i])
|
dp[i] = 1 + sum(dp[i - cards[-i - 1] : i])
|
||||||
part2 += dp[i]
|
part2 += dp[i]
|
||||||
|
|
||||||
print(part1, part2)
|
print(part1, part2)
|
||||||
|
|
||||||
|
main()
|
||||||
|
|
Loading…
Reference in a new issue