1.5 KiB
1.5 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
data = open('data/puzzle_2.txt', 'r').readlines()
Deel 1
color_dict = {
'red': 12,
'green': 13,
'blue': 14
}
total = 0
for line in data:
possible = True
for amount, color in re.findall('(\d+)\W*(red|green|blue)', line):
if (color_dict[color] < int(amount)):
possible = False
if possible:
total += int(re.findall('(\d+)', line)[0])
total
3035
Deel 2
total = 0
for line in data:
color_dict = {'red': [], 'green': [], 'blue': []}
for pair in re.findall('(\d+)\W*(red|green|blue)', line):
color_dict[pair[1]].append(int(pair[0]))
total += max(color_dict['red']) * max(color_dict['green']) * max(color_dict['blue'])
total
66027