1.6 KiB
1.6 KiB
Vet mooi notebook van Bas
Laten we beginnen met de imports
import re
Puzzel 1
Deel 1
f = open('data/puzzle_1.txt', 'r')
total = 0
for line in f:
digit_1 = re.search('(\d)', line).group()
digit_2 = re.search('(?:\d)(?!.*\d)', line).group()
total += int(digit_1+digit_2)
total
55834
Deel 2
l = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
digits = '|'+'|'.join(l)
f = open('data/puzzle_1.txt', 'r')
total = 0
for line in f:
string_digits = re.findall('(?=(\d'+digits+'))', line)
string_digits = [digit if (digit not in l) else str(l.index(digit)+1) for digit in string_digits]
total += int(string_digits[0] + string_digits[-1])
total
53221
Puzzel 2
Deel 1
f = open('data/puzzle_2.txt', 'r')
color_dict = {
'red': 12,
'green': 13,
'blue': 14
}
total = 0
for line in f:
id = int(re.findall('(\d+)', line)[0])
possible = True
for pair in re.findall('(\d+)\W*(red|green|blue)', line):
if (color_dict[pair[1]] < int(pair[0])):
possible = False
if possible:
total += id
total
3035
Deel 2
f = open('data/puzzle_2.txt', 'r')
color_dict = {
'red': 0,
'green': 0,
'blue': 0
}
total = 0
for line in f:
color_dict = {
'red': 0,
'green': 0,
'blue': 0
}
for pair in re.findall('(\d+)\W*(red|green|blue)', line):
if color_dict[pair[1]] < int(pair[0]):
color_dict[pair[1]] = int(pair[0])
total += color_dict['red'] * color_dict['green'] * color_dict['blue']
total
66027