You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.0 KiB
Python

4 years ago
import re
rules = []
my_ticket = None
tickets = []
with open('input', 'r') as f:
line = f.readline() # heading rules
while line != '\n':
rules.append(line)
line = f.readline()
f.readline() # heading my_ticket
my_ticket = f.readline()
f.readline() # newline
f.readline() # heading tickets
tickets = f.readlines()
def parse_ticket(raw):
return list(map(int, raw.split(',')))
def parse_rule(raw):
return list(map(int, re.findall('[0-9]+', raw)))
def in_range(num, lower, upper):
return num >= lower and num <= upper
my_ticket = parse_ticket(my_ticket)
tickets = list(map(parse_ticket, tickets))
rules = list(map(parse_rule, rules))
error_rate = 0
for ticket in tickets:
for num in ticket:
matches_rule = False
for rule in rules:
minA, maxA, minB, maxB = rule
if in_range(num, minA, maxA) or in_range(num, minB, maxB):
matches_rule = True
if not matches_rule:
error_rate += num
print(error_rate)