Day 4 completed
+ refactored day 1 to 3 with black -l 119
This commit is contained in:
parent
97021a58b2
commit
142696bf6e
@ -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)
|
||||
|
@ -1,20 +1,24 @@
|
||||
#!/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])
|
||||
|
@ -1,25 +1,24 @@
|
||||
#!/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])
|
||||
|
@ -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))))
|
||||
|
6
day4/example_input.txt
Normal file
6
day4/example_input.txt
Normal file
@ -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
|
27
day4/part1.py
Executable file
27
day4/part1.py
Executable file
@ -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)
|
38
day4/part2.py
Executable file
38
day4/part2.py
Executable file
@ -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)
|
Loading…
x
Reference in New Issue
Block a user