From 6fbe7871d54bd07c4a412004f52701e9e4158b2a Mon Sep 17 00:00:00 2001 From: Jake Charman Date: Mon, 4 Dec 2023 20:21:58 +0000 Subject: [PATCH] 2023 Day 4 --- 2023/Day-04/day04-1.py | 26 ++++++++++++++++++++++++++ 2023/Day-04/day04-2.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100755 2023/Day-04/day04-1.py create mode 100755 2023/Day-04/day04-2.py diff --git a/2023/Day-04/day04-1.py b/2023/Day-04/day04-1.py new file mode 100755 index 0000000..20822c8 --- /dev/null +++ b/2023/Day-04/day04-1.py @@ -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() diff --git a/2023/Day-04/day04-2.py b/2023/Day-04/day04-2.py new file mode 100755 index 0000000..52388a8 --- /dev/null +++ b/2023/Day-04/day04-2.py @@ -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()