-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16.py
145 lines (129 loc) · 3.89 KB
/
16.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import os
from collections import defaultdict
import itertools
debug = False
fileName = ""
try:
fileName = sys.argv[1]
if len(fileName) < 1:
fileName = "16.txt"
except:
fileName = "16.txt"
if debug:
print(fileName)
fields = {}
invalidValues = {}
def validateFields(values, min1, max1, min2, max2):
valid = True
for value in values:
if (value < min1 or value > max1) and (value < min2 or value > max2):
valid = False
return valid
with open(fileName) as file:
minVal = 1000
maxVal = 0
for i in range(1000):
invalidValues[i] = True
line = file.readline()
while line != "\n":
allowed = []
name = line[:line.find(":")]
ranges = line[line.find(":") + 2:].split("or")
for r in ranges:
pair = r.split("-")
pair[0] = int(pair[0])
pair[1] = int(pair[1])
allowed.append(pair[0])
allowed.append(pair[1])
if pair[0] < minVal:
minVal = pair[0]
if pair[1] > maxVal:
maxVal = pair[1]
for i in range(pair[0],pair[1] + 1):
try:
del invalidValues[i]
except:
pass
if debug:
print(allowed)
fields[name] = allowed
line = file.readline()
if debug:
print(invalidValues)
myTicket = []
otherTickets = []
while line != "your ticket:\n":
line = file.readline()
line = file.readline()
myTicket = line.split(",")
for i in range(len(myTicket)):
myTicket[i] = int(myTicket[i])
while line != "nearby tickets:\n":
line = file.readline()
line = file.readline()
while len(line) > 1:
ticket = line.split(",")
for i in range(len(ticket)):
ticket[i] = int(ticket[i])
otherTickets.append(ticket)
line = file.readline()
#validate tickets
validTickets = []
validTickets.append(myTicket)
errorRate = 0
for ticket in otherTickets:
valid = True
for field in ticket:
if field in invalidValues:
valid = False
errorRate += field
if valid:
validTickets.append(ticket)
print(errorRate) #part1
#get all the values per field
fieldUse = []
for i in range(len(myTicket)):
values = {}
for j in range(len(validTickets)):
values[validTickets[j][i]] = True
fieldUse.append(values)
fieldNames = defaultdict(list)
for i in range(len(fields)):
fieldNames[i] = []
for field in fields.keys():
for i in range(len(fields)):
ranges = fields[field]
if validateFields(fieldUse[i],ranges[0],ranges[1],ranges[2],ranges[3]):
fieldNames[i].append(field)
#print(fieldNames)
possibilities = 0
for field in fieldNames:
possibilities += len(fieldNames[field])
while len(fields) < possibilities:
if debug:
print("-------",len(fields), possibilities)
targetDeletion = []
for field in fieldNames:
if len(fieldNames[field]) == 1:
targetDeletion.append(fieldNames[field][0])
for field in fieldNames:
if len(fieldNames[field]) == 1:
pass
else:
newFields = []
for name in fieldNames[field]:
if name not in targetDeletion:
newFields.append(name)
fieldNames[field] = newFields
if debug:
print(fieldNames)
possibilities = 0
for field in fieldNames:
possibilities += len(fieldNames[field])
if debug:
print("*****", fieldNames)
myVal = 1
for i in range(len(fieldNames)):
if "departure" in fieldNames[i][0]:
myVal *= myTicket[i]
print(myVal) #part2