Skip to content

Commit

Permalink
Day 4
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobemerick committed Dec 11, 2023
1 parent 647e033 commit f19b25c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
18 changes: 18 additions & 0 deletions 04/1.py
@@ -0,0 +1,18 @@
totalPoints = 0

file = open('input.txt', 'r')
for line in file:
parsedLine = line.strip().split(':')[1].split('|')
winningNumbers = list(filter(None, parsedLine[0].split(' ')))
cardNumbers = list(filter(None, parsedLine[1].split(' ')))

matchingNumbers = 0
for cardNumber in cardNumbers:
if cardNumber in winningNumbers:
matchingNumbers += 1

if matchingNumbers > 0:
totalPoints += pow(2, matchingNumbers - 1)
file.close()

print('Total points won', totalPoints)
27 changes: 27 additions & 0 deletions 04/2.py
@@ -0,0 +1,27 @@
cardList = []
cardCount = []

file = open('input.txt', 'r')
for line in file:
parsedLine = line.strip().split(':')[1].split('|')
winningNumbers = list(filter(None, parsedLine[0].split(' ')))
cardNumbers = list(filter(None, parsedLine[1].split(' ')))

matchingNumbers = 0
for cardNumber in cardNumbers:
if cardNumber in winningNumbers:
matchingNumbers += 1

cardList.append(matchingNumbers)
cardCount.append(1)
file.close()

for i, cardWinnings in enumerate(cardList):
copyCount = cardCount[i]

j = 0
while j < cardWinnings:
j += 1
cardCount[i + j] += copyCount

print('Total number of cards:', sum(cardCount))

0 comments on commit f19b25c

Please sign in to comment.