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