-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.py
31 lines (25 loc) · 879 Bytes
/
part1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
with open("input.txt") as file:
line = file.readline().strip()
# Making dictionaty of fields:
fields = {}
while line:
fields[line.split(':')[0]] = [list(map(int, range_.split('-'))) for range_ in line.split(':')[1].split("or")]
line = file.readline().strip()
# Reading my ticket
file.readline()
my_ticket = list(map(int, file.readline().split(',')))
file.readline()
file.readline()
line = file.readline().strip()
# Determining valid nearby tickets
sum_of_invalid = 0
while line:
ticket = list(map(int, line.split(',')))
for value in ticket:
if not any(
any(left <= value <= right for left, right in range_)
for range_ in fields.values()
):
sum_of_invalid += value
line = file.readline()
print(sum_of_invalid)