diff --git a/day1/part2.py b/day1/part2.py index 80053a6..193293c 100644 --- a/day1/part2.py +++ b/day1/part2.py @@ -30,14 +30,14 @@ with open("input.txt") as input: ".*?([0-9]|zero|one|two|three|four|five|six|seven|eight|nine).*([0-9]|zero|one|two|three|four|five|six|seven|eight|nine).*?", line, ): - #print(match.groups(1)) + # print(match.groups(1)) number = int(digits[match.groups(1)[0]] + digits[match.groups(1)[1]]) - #print(number) + # print(number) accumulator += number elif match := re.match(".*?([0-9]|zero|one|two|three|four|five|six|seven|eight|nine).*?", line): - #print(match.groups(1)) + # print(match.groups(1)) number = int(digits[match.groups(1)[0]] + digits[match.groups(1)[0]]) - #print(number) + # print(number) accumulator += number else: print(line) diff --git a/day2/part1.py b/day2/part1.py index 295b671..62d7600 100755 --- a/day2/part1.py +++ b/day2/part1.py @@ -1,21 +1,25 @@ #!/usr/bin/env python3.11 import re -maximums = { 'red': 12, 'green': 13, 'blue': 14} -def game_turn(game_id:int, data: str) -> int: - sets = data.split('; ') + +maximums = {"red": 12, "green": 13, "blue": 14} + + +def game_turn(game_id: int, data: str) -> int: + sets = data.split("; ") for dset in sets: - cubes = dset.split(', ') + cubes = dset.split(", ") for cube in cubes: - print(cube.split(' ')) - number, color = cube.split(' ') + print(cube.split(" ")) + number, color = cube.split(" ") number = int(number) if number > maximums[color]: return 0 return game_id + id_sum = 0 -with open('input.txt', 'r') as input: +with open("input.txt", "r") as input: while line := input.readline(): match = re.match("Game (\d+): (.*)", line) id_sum += game_turn(int(match.groups(0)[0]), match.groups(0)[1]) -print(id_sum) \ No newline at end of file +print(id_sum) diff --git a/day2/part2.py b/day2/part2.py index bd56452..d64843a 100755 --- a/day2/part2.py +++ b/day2/part2.py @@ -1,26 +1,25 @@ #!/usr/bin/env python3.11 import re -maximums = { 'red': 12, 'green': 13, 'blue': 14} -def game_turn(game_id:int, data: str) -> int: - local_maximums = { - 'red': 0, - 'green': 0, - 'blue': 0 - } - sets = data.split('; ') +maximums = {"red": 12, "green": 13, "blue": 14} + + +def game_turn(game_id: int, data: str) -> int: + local_maximums = {"red": 0, "green": 0, "blue": 0} + sets = data.split("; ") for dset in sets: - cubes = dset.split(', ') + cubes = dset.split(", ") for cube in cubes: - print(cube.split(' ')) - number, color = cube.split(' ') + print(cube.split(" ")) + number, color = cube.split(" ") number = int(number) - local_maximums[color] = max( local_maximums[color], number) - return local_maximums['red'] * local_maximums['green'] * local_maximums['blue'] + local_maximums[color] = max(local_maximums[color], number) + return local_maximums["red"] * local_maximums["green"] * local_maximums["blue"] + id_sum = 0 -with open('input.txt', 'r') as input: +with open("input.txt", "r") as input: while line := input.readline(): match = re.match("Game (\d+): (.*)", line) id_sum += game_turn(int(match.groups(0)[0]), match.groups(0)[1]) -print(id_sum) \ No newline at end of file +print(id_sum) diff --git a/day3/part1.py b/day3/part1.py index 3de9076..8e01c19 100755 --- a/day3/part1.py +++ b/day3/part1.py @@ -72,8 +72,8 @@ for line_no in range(len(lines)): sym_selected = list(filter(selection, numbers)) selected_numbers += sym_selected - #print(symbol, sym_selected) + # print(symbol, sym_selected) # print("") selected_numbers = set(selected_numbers) -#pprint(selected_numbers) +# pprint(selected_numbers) print(sum(list(map(lambda number: number.value, selected_numbers)))) diff --git a/day4/example_input.txt b/day4/example_input.txt new file mode 100644 index 0000000..71f208a --- /dev/null +++ b/day4/example_input.txt @@ -0,0 +1,6 @@ +Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53 +Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19 +Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1 +Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83 +Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36 +Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11 \ No newline at end of file diff --git a/day4/part1.py b/day4/part1.py new file mode 100755 index 0000000..6b48c50 --- /dev/null +++ b/day4/part1.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3.11 +import re + +input_file = "input.txt" +sum = 0 +with open(input_file) as input: + while line := input.readline(): + if match := re.match("Card +(\d+): (.*) \| (.*)", line): + winning_numbers = list(map(int, re.split(" +", match.group(2).lstrip(" ")))) + numbers = map(int, re.split(" +", match.group(3).lstrip(" "))) + # print(match.group(1), winning_numbers, numbers) + points = 0 + # print(winning_numbers, end="; ") + for number in numbers: + # print(number, end=",") + if number in winning_numbers: + # print("is winning", end="; ") + if points == 0: + points = 1 + else: + points *= 2 + else: + # print("is not winning", end="; ") + pass + # print("\n", points) + sum += points +print(sum) diff --git a/day4/part2.py b/day4/part2.py new file mode 100755 index 0000000..53ffd91 --- /dev/null +++ b/day4/part2.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3.11 +import re +from dataclasses import dataclass + +input_file = "input.txt" + + +@dataclass +class Card: + winning_numbers: list[int] + numbers: list[int] + copies: int + + def __hash__(self): + return (self.copies, self.winning_numbers, self.numbers).__hash__() + + +with open(input_file) as input: + raw_cards = input.readlines() + +cards = [Card(winning_numbers=[], numbers=[], copies=1) for i in range(len(raw_cards))] + +for line in raw_cards: + if match := re.match("Card +(\d+): (.*) \| (.*)", line): + card_no = int(match.group(1)) - 1 + cards[card_no].winning_numbers = list(map(int, re.split(" +", match.group(2).lstrip(" ")))) + cards[card_no].numbers = map(int, re.split(" +", match.group(3).lstrip(" "))) + next_no = card_no + 1 + for number in cards[card_no].numbers: + if number in cards[card_no].winning_numbers: + if len(cards) > next_no - 1: + cards[next_no].copies += cards[card_no].copies + next_no += 1 + +the_sum = sum(map(lambda c: c.copies, cards)) + + +print(the_sum)