2023 Day 4
This commit is contained in:
26
2023/Day-04/day04-1.py
Executable file
26
2023/Day-04/day04-1.py
Executable file
@@ -0,0 +1,26 @@
|
|||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
def main():
|
||||||
|
total = 0
|
||||||
|
|
||||||
|
with open('input.txt', 'r') as input_file:
|
||||||
|
for line in input_file:
|
||||||
|
card_total = 0
|
||||||
|
scratchcard = line.split(':')[1]
|
||||||
|
winners = scratchcard.split('|')[0].strip().split(' ')
|
||||||
|
winners = [winner for winner in winners if winner != '']
|
||||||
|
my_nums = scratchcard.split('|')[1].strip().split(' ')
|
||||||
|
my_nums = [num for num in my_nums if num != '']
|
||||||
|
|
||||||
|
for num in my_nums:
|
||||||
|
if num in winners:
|
||||||
|
if card_total == 0:
|
||||||
|
card_total += 1
|
||||||
|
elif card_total > 0:
|
||||||
|
card_total = card_total * 2
|
||||||
|
total += card_total
|
||||||
|
|
||||||
|
print(total)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
35
2023/Day-04/day04-2.py
Executable file
35
2023/Day-04/day04-2.py
Executable file
@@ -0,0 +1,35 @@
|
|||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
def main():
|
||||||
|
|
||||||
|
num_cards = {}
|
||||||
|
card_points = {}
|
||||||
|
total_cards = 0
|
||||||
|
|
||||||
|
with open('input.txt', 'r') as input_file:
|
||||||
|
for line in input_file:
|
||||||
|
card_total = 0
|
||||||
|
scratchcard = line.split(':')[1]
|
||||||
|
winners = scratchcard.split('|')[0].strip().split(' ')
|
||||||
|
winners = [winner for winner in winners if winner != '']
|
||||||
|
my_nums = scratchcard.split('|')[1].strip().split(' ')
|
||||||
|
my_nums = [num for num in my_nums if num != '']
|
||||||
|
|
||||||
|
for num in my_nums:
|
||||||
|
if num in winners:
|
||||||
|
card_total += 1
|
||||||
|
card_points[tuple(my_nums)] = card_total
|
||||||
|
num_cards[tuple(my_nums)] = 1
|
||||||
|
total_cards += 1
|
||||||
|
|
||||||
|
cards = list(num_cards.keys())
|
||||||
|
for card in cards:
|
||||||
|
index = cards.index(card)
|
||||||
|
cards_to_copy = list(range(index+1, index+1+card_points[card]))
|
||||||
|
for new_card in cards_to_copy:
|
||||||
|
num_cards[cards[new_card]] += (1 * num_cards[card])
|
||||||
|
total_cards += (len(cards_to_copy) * num_cards[card])
|
||||||
|
print(total_cards)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
Reference in New Issue
Block a user