master
E. Almqvist 4 years ago
parent d3c088bd1f
commit 6fce4261e5
  1. 103
      2020/16.py
  2. 472
      2020/debug.txt

@ -2,6 +2,7 @@
from aoc import get_input # AoC
import re # regex
from collections import defaultdict as dd
data = get_input(16).strip()
@ -14,7 +15,7 @@ tickets.pop(0)
def parseParam(param):
name, rnga1, rnga2, rngb1, rngb2 = re.match(r"([a-z\ ]+): ([0-9]+)-([0-9]+) or ([0-9]+)-([0-9]+)", param).groups()
name, rnga1, rnga2, rngb1, rngb2 = re.match(r"(\w+ *\w*): ([0-9]+)-([0-9]+) or ([0-9]+)-([0-9]+)", param).groups()
rng1 = range(int(rnga1), int(rnga2) + 1)
rng2 = range(int(rngb1), int(rngb2) + 1)
@ -31,6 +32,8 @@ for i in range(len(params)):
def parseTicket(ticket):
return [int(n) for n in ticket.split(",")]
myticket = parseTicket(myticket)
badvals = []
goodtickets = [ti for ti in tickets]
@ -53,97 +56,57 @@ for i in range(len(tickets)):
break
if( not valid ):
print(f"Bad: {ticket=} {any(checks)=}")
goodtickets.remove(ticket)
print("\nPart1:", sum(badvals))
tickets = [parseTicket(t) for t in goodtickets]
ticketlen = len(tickets[0])
# make seen list for param and index
#
# go through all indexes, i=0
# make list of possibleParams for that i
# # go through all tickets for i
# # # go through all params
# # # # check if ticket[i] is in param range
# # # # # if not then remove param from iparams
# # # # # else next param
# take first param
# exclude param from list and exclude index from list
# next index
seenparams = []
seenindex = []
paramsIndex = dict()
works = dd(set)
def getPosParams(i):
iparams = dict()
possibleParams = []
for p in params:
paramclass, rng1, rng2 = parseParam(p)
iparams[paramclass] = (rng1, rng2)
possibleParams.append(paramclass)
for i in range(ticketlen):
clthing = dict()
for cl, rng in ranges.items():
clthing[cl] = 0
for t in goodtickets:
ticket = parseTicket(t)
for ticket in goodtickets:
ticket = parseTicket(ticket)
num = ticket[i]
for param, rng in iparams.items():
check = num in rng[0] or num in rng[1]
if(not check and param in possibleParams):
possibleParams.remove(param)
else:
continue
return possibleParams
for cl, rng in ranges.items():
clcount = 0
if(num in rng[0] or num in rng[1]):
clcount += 1
clthing[cl] += clcount
for i in range(ticketlen):
posparam = getPosParams(i)
posparam.sort(key=len)
print(posparam)
for cl, count in clthing.items():
if( count == len(goodtickets) ):
works[i].add(cl)
thisparam = None
for cl in posparam:
if(not cl in seenparams):
thisparam = cl
seenparams.append(cl)
break
paramsIndex[i] = thisparam
used = set()
while( any( [len(paramwork) > 1 for paramwork in works.values()] ) ):
for index, cl in works.items():
if(len(cl) > 1):
works[index] = (cl | used) ^ used
else:
used.add(next(iter(cl)))
print("####", paramsIndex)
thenums = []
for i, cl in works.items():
num = myticket[i]
cl = list(cl)
for i, cl in paramsIndex.items():
print(f"{cl}: {parseTicket(myticket)[i]}")
if( "departure" in cl[0] ):
thenums.append(num)
if(cl):
if( "departure" in cl ):
thenums.append(parseTicket(myticket)[i])
print(thenums)
import math
print(" 1409959524847 !=")
print("#",math.prod(thenums))
print(" 21095351239483")
print(" 30145908219919 reverse")
print(sum(thenums))
print("Part 1", sum(badvals))
print("Part 2:", math.prod(thenums))

@ -0,0 +1,472 @@
Bad: ticket='766,118,421,157,683,323,803,410,731,626,5,919,871,339,317,624,469,349,757,902' any(checks)=False
Bad: ticket='886,758,364,882,372,565,436,463,291,388,143,877,933,15,735,350,119,80,151,229' any(checks)=False
Bad: ticket='233,291,683,991,497,273,221,851,459,337,431,736,800,313,354,697,947,633,361,389' any(checks)=False
Bad: ticket='238,371,486,167,228,440,738,377,445,883,15,898,334,152,694,328,461,735,943,692' any(checks)=False
Bad: ticket='75,114,548,292,157,82,994,79,293,689,857,376,337,948,172,443,506,753,326,343' any(checks)=False
Bad: ticket='166,480,234,154,500,260,428,521,417,908,517,983,801,861,868,908,944,255,55,354' any(checks)=False
Bad: ticket='105,314,112,494,166,714,76,511,451,112,322,567,512,993,271,147,918,436,860,935' any(checks)=False
Bad: ticket='228,292,328,856,715,358,344,686,682,370,846,259,677,225,261,467,24,453,869,625' any(checks)=False
Bad: ticket='173,383,480,216,684,860,448,312,678,16,270,870,259,468,371,520,560,511,388,678' any(checks)=False
Bad: ticket='231,334,418,925,560,219,11,387,345,446,942,910,105,148,518,111,559,880,930,718' any(checks)=False
Bad: ticket='107,464,225,629,804,420,914,264,689,441,636,23,636,234,292,142,932,578,697,907' any(checks)=False
Bad: ticket='256,491,947,510,420,477,484,455,437,544,158,320,915,468,14,66,696,733,731,264' any(checks)=False
Bad: ticket='754,358,925,383,686,910,13,351,333,544,111,385,338,797,490,695,315,111,684,269' any(checks)=False
Bad: ticket='412,719,556,591,149,676,694,342,918,161,594,796,237,552,160,451,990,862,765,447' any(checks)=False
Bad: ticket='925,514,475,76,353,932,65,453,733,50,883,315,148,4,848,437,51,863,679,159' any(checks)=False
Bad: ticket='350,441,692,461,908,267,872,940,736,495,190,989,152,69,317,389,334,917,496,749' any(checks)=False
Bad: ticket='851,741,627,944,588,663,388,347,104,519,365,929,158,173,472,388,18,147,456,509' any(checks)=False
Bad: ticket='886,430,24,380,429,945,317,870,628,230,227,567,68,549,740,757,112,323,947,621' any(checks)=False
Bad: ticket='683,802,681,859,340,344,630,563,429,445,503,147,749,546,432,881,793,997,317,949' any(checks)=False
Bad: ticket='174,436,111,919,68,855,515,376,114,427,491,173,717,999,759,689,336,736,259,230' any(checks)=False
Bad: ticket='590,480,556,897,588,363,168,17,851,753,366,117,385,864,491,464,67,488,363,665' any(checks)=False
Bad: ticket='867,943,941,997,322,419,751,563,142,732,885,911,795,141,916,499,426,415,345,265' any(checks)=False
Bad: ticket='983,928,74,510,438,323,690,364,637,745,378,916,476,515,173,219,878,580,492,358' any(checks)=False
Bad: ticket='934,935,898,146,941,933,221,318,680,695,543,516,990,271,799,232,549,737,588,467' any(checks)=False
Bad: ticket='328,119,690,416,949,427,475,161,679,170,236,83,81,864,169,359,21,942,501,155' any(checks)=False
Bad: ticket='158,664,261,163,464,746,667,451,523,146,114,681,360,565,714,688,514,362,14,436' any(checks)=False
Bad: ticket='190,147,332,217,913,868,338,381,936,870,110,761,934,945,803,112,271,522,5,379' any(checks)=False
Bad: ticket='164,386,422,670,687,543,689,762,562,227,412,901,142,359,988,934,170,271,692,876' any(checks)=False
Bad: ticket='551,553,446,437,521,721,154,258,481,72,872,51,449,993,515,802,482,887,220,231' any(checks)=False
Bad: ticket='484,859,629,634,337,521,442,879,219,795,904,146,664,580,558,931,943,333,429,988' any(checks)=False
Bad: ticket='358,238,426,264,299,916,76,939,852,678,339,143,686,545,692,871,379,20,265,218' any(checks)=False
Bad: ticket='268,926,388,862,795,17,744,687,562,513,312,676,716,918,315,460,624,924,911,76' any(checks)=False
Bad: ticket='111,342,162,674,554,864,150,929,747,325,226,689,222,795,799,1,743,794,482,416' any(checks)=False
Bad: ticket='899,901,763,920,229,760,81,346,795,259,739,942,449,235,70,217,312,458,448,4' any(checks)=False
Bad: ticket='24,932,415,152,113,162,553,468,162,78,152,449,902,342,366,430,293,863,167,560' any(checks)=False
Bad: ticket='68,222,804,111,545,549,463,427,518,484,172,490,743,440,672,142,895,153,21,55' any(checks)=False
Bad: ticket='11,172,795,931,336,425,473,271,315,874,385,794,430,689,941,879,369,170,635,255' any(checks)=False
Bad: ticket='870,761,918,75,350,427,894,487,556,383,342,683,719,566,494,22,490,225,169,589' any(checks)=False
Bad: ticket='330,317,459,115,312,496,796,915,987,877,922,157,456,154,503,119,946,519,345,757' any(checks)=False
Bad: ticket='670,563,922,799,680,507,623,982,471,80,875,238,271,339,907,452,642,477,386,678' any(checks)=False
Bad: ticket='721,226,758,762,764,348,873,52,433,484,940,642,450,119,351,5,933,525,145,666' any(checks)=False
Bad: ticket='387,580,257,117,900,489,862,924,935,635,116,377,107,676,751,351,912,660,742,993' any(checks)=False
Bad: ticket='496,889,668,715,410,853,633,716,749,455,525,329,314,171,514,173,797,472,21,506' any(checks)=False
Bad: ticket='378,866,544,662,436,498,800,227,521,565,174,428,457,108,359,663,881,224,521,17' any(checks)=False
Bad: ticket='593,376,463,340,452,21,223,746,755,948,238,803,366,463,506,759,77,220,865,922' any(checks)=False
Bad: ticket='855,412,321,375,81,982,551,737,923,945,379,949,446,880,54,503,450,445,350,854' any(checks)=False
Bad: ticket='110,255,222,895,765,236,523,299,327,740,149,410,293,333,74,520,147,266,998,227' any(checks)=False
Part1: 19240
0
189 20
189 20
189 20
189 20
189 20
189 20
189 20
189 20
189 20
190 20
189 20
189 20
189 20
190 20
189 20
189 20
189 20
190 20
189 20
189 20
1
189 20
189 20
189 20
189 20
189 20
189 20
189 20
189 20
189 20
190 20
189 20
189 20
190 20
190 20
189 20
189 20
189 20
190 20
189 20
189 20
2
190 20
190 20
190 20
190 20
190 20
190 20
190 20
189 20
189 20
190 20
189 20
190 20
190 20
190 20
190 20
190 20
189 20
190 20
190 20
190 20
3
189 20
189 20
189 20
189 20
189 20
189 20
189 20
189 20
189 20
189 20
189 20
189 20
189 20
190 20
189 20
189 20
189 20
189 20
189 20
189 20
4
189 20
189 20
189 20
189 20
189 20
189 20
189 20
189 20
189 20
190 20
189 20
189 20
190 20
190 20
189 20
190 20
189 20
190 20
189 20
189 20
5
189 20
189 20
189 20
189 20
189 20
189 20
189 20
189 20
189 20
189 20
189 20
189 20
189 20
190 20
189 20
189 20
189 20
190 20
189 20
189 20
6
190 20
190 20
190 20
190 20
190 20
190 20
190 20
189 20
189 20
190 20
189 20
190 20
190 20
190 20
190 20
190 20
189 20
190 20
189 20
190 20
7
189 20
190 20
189 20
189 20
189 20
190 20
190 20
189 20
189 20
190 20
189 20
190 20
190 20
190 20
190 20
190 20
189 20
190 20
189 20
190 20
8
189 20
189 20
189 20
189 20
189 20
189 20
190 20
189 20
189 20
190 20
189 20
190 20
190 20
190 20
190 20
190 20
189 20
190 20
189 20
190 20
9
189 20
189 20
189 20
189 20
189 20
189 20
190 20
189 20
189 20
190 20
189 20
190 20
190 20
190 20
190 20
190 20
189 20
190 20
189 20
189 20
10
189 20
190 20
190 20
189 20
189 20
190 20
190 20
189 20
189 20
190 20
189 20
190 20
190 20
190 20
190 20
190 20
189 20
190 20
189 20
190 20
11
190 20
190 20
190 20
190 20
190 20
190 20
190 20
190 20
190 20
190 20
190 20
190 20
190 20
190 20
190 20
190 20
189 20
190 20
190 20
190 20
12
189 20
189 20
189 20
189 20
189 20
189 20
189 20
189 20
189 20
190 20
189 20
190 20
190 20
190 20
189 20
190 20
189 20
190 20
189 20
189 20
13
189 20
189 20
189 20
189 20
189 20
189 20
189 20
189 20
189 20
190 20
189 20
190 20
190 20
190 20
190 20
190 20
189 20
190 20
189 20
189 20
14
190 20
190 20
190 20
190 20
190 20
190 20
190 20
189 20
190 20
190 20
189 20
190 20
190 20
190 20
190 20
190 20
189 20
190 20
190 20
190 20
15
190 20
190 20
190 20
190 20
190 20
190 20
190 20
190 20
190 20
190 20
190 20
190 20
190 20
190 20
190 20
190 20
190 20
190 20
190 20
190 20
16
190 20
190 20
190 20
190 20
190 20
190 20
190 20
190 20
190 20
190 20
189 20
190 20
190 20
190 20
190 20
190 20
189 20
190 20
190 20
190 20
17
189 20
190 20
189 20
189 20
189 20
189 20
190 20
189 20
189 20
190 20
189 20
190 20
190 20
190 20
190 20
190 20
189 20
190 20
189 20
190 20
18
190 20
190 20
190 20
190 20
189 20
190 20
190 20
189 20
189 20
190 20
189 20
190 20
190 20
190 20
190 20
190 20
189 20
190 20
189 20
190 20
19
189 20
190 20
190 20
190 20
189 20
190 20
190 20
189 20
189 20
190 20
189 20
190 20
190 20
190 20
190 20
190 20
189 20
190 20
189 20
190 20
######WORKS defaultdict(<class 'set'>, {0: {'type', 'arrival track', 'route'}, 1: {'price', 'arrival track', 'route', 'type'}, 2: {'departure station', 'duration', 'arrival location', 'departure location', 'row', 'price', 'departure platform', 'departure track', 'seat', 'zone', 'arrival track', 'departure date', 'departure time', 'route', 'wagon', 'type'}, 3: {'route'}, 4: {'price', 'seat', 'arrival track', 'route', 'type'}, 5: {'type', 'route'}, 6: {'departure station', 'duration', 'arrival location', 'departure location', 'row', 'price', 'departure platform', 'departure track', 'seat', 'zone', 'arrival track', 'departure date', 'departure time', 'route', 'type'}, 7: {'departure station', 'duration', 'arrival location', 'row', 'price', 'seat', 'zone', 'arrival track', 'departure time', 'route', 'type'}, 8: {'duration', 'arrival location', 'row', 'price', 'seat', 'zone', 'arrival track', 'route', 'type'}, 9: {'duration', 'arrival location', 'row', 'price', 'seat', 'arrival track', 'route', 'type'}, 10: {'departure station', 'duration', 'arrival location', 'row', 'price', 'departure platform', 'seat', 'zone', 'arrival track', 'departure time', 'route', 'type'}, 11: {'class', 'arrival location', 'departure location', 'row', 'departure platform', 'arrival track', 'departure date', 'departure station', 'arrival platform', 'price', 'wagon', 'zone', 'departure time', 'route', 'type', 'duration', 'departure track', 'seat', 'arrival station'}, 12: {'duration', 'price', 'seat', 'arrival track', 'route', 'type'}, 13: {'duration', 'row', 'price', 'seat', 'arrival track', 'route', 'type'}, 14: {'departure station', 'arrival platform', 'duration', 'arrival location', 'departure location', 'row', 'price', 'departure platform', 'departure track', 'seat', 'zone', 'arrival track', 'departure date', 'departure time', 'route', 'wagon', 'type'}, 15: {'class', 'arrival location', 'departure location', 'row', 'departure platform', 'arrival track', 'departure date', 'departure station', 'arrival platform', 'price', 'wagon', 'zone', 'departure time', 'route', 'type', 'train', 'duration', 'departure track', 'seat', 'arrival station'}, 16: {'departure station', 'arrival platform', 'duration', 'arrival location', 'departure location', 'row', 'price', 'departure platform', 'departure track', 'seat', 'zone', 'arrival track', 'departure date', 'departure time', 'route', 'wagon', 'arrival station', 'type'}, 17: {'departure station', 'duration', 'arrival location', 'row', 'price', 'seat', 'zone', 'arrival track', 'route', 'type'}, 18: {'departure station', 'duration', 'arrival location', 'departure location', 'row', 'price', 'departure platform', 'departure track', 'seat', 'zone', 'arrival track', 'departure time', 'route', 'type'}, 19: {'departure station', 'duration', 'arrival location', 'row', 'price', 'departure platform', 'departure track', 'seat', 'zone', 'arrival track', 'departure time', 'route', 'type'}})
i=0 work={'type', 'arrival track', 'route'} idk=['type', 'arrival track', 'route'] cl='type'
seen
Loading…
Cancel
Save