Compare commits
10 Commits
964296b439
...
6ff4a25bf3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6ff4a25bf3 | ||
|
|
18c76f694b | ||
|
|
93e009c5a8 | ||
|
|
c7bc2439a6 | ||
|
|
de0a250ed9 | ||
|
|
507987855f | ||
|
|
73f6ea9be5 | ||
|
|
e44d059a88 | ||
|
|
dc97742371 | ||
|
|
bac46a6cf9 |
@@ -1,2 +1,2 @@
|
||||
#!/usr/bin/env python3
|
||||
import numpy as p;import scipy.ndimage as i;a=p.array([[int(n)for n in line.strip()]for line in open("input.txt")]);n=i.generate_binary_structure(len(a.shape),1);print(sum(a[p.where((i.minimum_filter(a,footprint=n)==a)*~(i.maximum_filter(a,footprint=n)==a))]+1))
|
||||
import numpy as p;import scipy.ndimage as i;a=p.array([[int(n)for n in line.strip()]for line in open("input.txt")]);n=p.array([[0,1,0],[1,1,1],[0,1,0]]);print(sum(a[p.where((i.minimum_filter(a,footprint=n)==a)*~(i.maximum_filter(a,footprint=n)==a))]+1))
|
||||
|
||||
@@ -2,13 +2,11 @@
|
||||
|
||||
import numpy as np
|
||||
import scipy.ndimage.filters as filters
|
||||
import scipy.ndimage.morphology as morphology
|
||||
|
||||
|
||||
arr = np.array([[int(n) for n in line.strip()] for line in open("input.txt")])
|
||||
|
||||
# https://stackoverflow.com/questions/3986345/how-to-find-the-local-minima-of-a-smooth-multidimensional-array-in-numpy-efficie
|
||||
neighborhood = morphology.generate_binary_structure(len(arr.shape), 1)
|
||||
neighborhood = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]])
|
||||
local_min = filters.minimum_filter(arr, footprint=neighborhood) == arr
|
||||
local_max = filters.maximum_filter(arr, footprint=neighborhood) == arr
|
||||
local_min_without_plateau = np.logical_and(local_min, np.logical_not(local_max))
|
||||
|
||||
41
day13/generator.py
Normal file
41
day13/generator.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from itertools import repeat
|
||||
from random import random, randrange
|
||||
|
||||
target = """
|
||||
# # ####### # # ####### ###### ##### ####### # # # # # ##### ### # # ####### # # ####### # # # # ######
|
||||
## # # # # # # # # # # # ## # ## # # # # # # # # # # # # # # # # # # #
|
||||
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
||||
# # # ##### # # ##### ###### # #### # # # # # # # # # # # #### # # # ##### # # # # # # # ######
|
||||
# # # # # # # # # # # # # # # # # # # ####### # # # # # # # # # # # # # #
|
||||
# ## # # # # # # # # # # # ## # ## # # # # # # # # # # # # # # # #
|
||||
# # ####### # ####### # # ##### ####### # # # # # # ##### ### # ####### # ####### ##### ##### #
|
||||
"""
|
||||
|
||||
points = {(x, y) for y, line in enumerate(target.strip().split('\n')) for x, c in enumerate(line) if c == "#"}
|
||||
img_x, img_y = max_x, max_y = tuple(map(max, zip(*points)))
|
||||
instructions = []
|
||||
|
||||
for _ in range(20):
|
||||
is_x, fold = (True, randrange(max_x+1, max_x*2))\
|
||||
if random() < 0.5\
|
||||
else (False, randrange(max_y+1, max_y*2))
|
||||
instructions.append((is_x, fold))
|
||||
next_max = fold*2
|
||||
if is_x:
|
||||
points = set(v for (x, y), rand in zip(points, iter(random, 2)) for v in [
|
||||
(x, y) if rand <= 0.66 or x <= max_x-next_max+fold else None,
|
||||
(2*fold - x, y) if rand > 0.33 and x > max_x-next_max+fold else None,
|
||||
] if v is not None)
|
||||
max_x = next_max
|
||||
else:
|
||||
points = set(v for (x, y), rand in zip(points, iter(random, 2)) for v in [
|
||||
(x, y) if rand <= 0.66 or y <= max_y-next_max+fold else None,
|
||||
(x, 2*fold - y) if rand > 0.33 and y > max_y-next_max+fold else None,
|
||||
] if v is not None)
|
||||
max_y = next_max
|
||||
|
||||
for point in points:
|
||||
print(','.join(map(str, point)))
|
||||
print()
|
||||
for instruction in instructions[::-1]:
|
||||
print("fold along " + ("x" if instruction[0] else "y") + "=" + str(instruction[1]))
|
||||
@@ -1,5 +1,4 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
lines = (x.strip() for x in open("input.txt"))
|
||||
points = set()
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
lines = (x.strip() for x in open("input.txt"))
|
||||
points = set()
|
||||
|
||||
@@ -14,4 +13,4 @@ for line in lines:
|
||||
points.add(tuple(int(num) for num in line.split(",")))
|
||||
|
||||
max_x, max_y = tuple(map(max, zip(*points)))
|
||||
print('\n'.join(''.join('#' if (x, y) in points else ' ' for x in range(max_x+1)) for y in range(max_y+1)))
|
||||
print('\n'.join(''.join('█' if (x, y) in points else ' ' for x in range(max_x+1)) for y in range(max_y+1)))
|
||||
|
||||
102
day14/input.txt
Normal file
102
day14/input.txt
Normal file
@@ -0,0 +1,102 @@
|
||||
PBFNVFFPCPCPFPHKBONB
|
||||
|
||||
KK -> S
|
||||
FO -> B
|
||||
PP -> O
|
||||
HN -> S
|
||||
CN -> H
|
||||
VH -> P
|
||||
BK -> B
|
||||
VC -> N
|
||||
CB -> H
|
||||
OC -> K
|
||||
BF -> P
|
||||
FV -> K
|
||||
SP -> F
|
||||
OP -> K
|
||||
SS -> B
|
||||
NN -> O
|
||||
CS -> K
|
||||
CF -> K
|
||||
FF -> S
|
||||
SV -> P
|
||||
OK -> S
|
||||
CO -> F
|
||||
OB -> K
|
||||
BH -> B
|
||||
HH -> S
|
||||
VB -> V
|
||||
KV -> H
|
||||
CK -> V
|
||||
NV -> N
|
||||
SF -> V
|
||||
PK -> H
|
||||
PV -> N
|
||||
FB -> O
|
||||
BO -> K
|
||||
FP -> N
|
||||
OF -> N
|
||||
FK -> O
|
||||
VK -> V
|
||||
NO -> V
|
||||
NS -> C
|
||||
KC -> S
|
||||
VF -> V
|
||||
BV -> N
|
||||
CP -> K
|
||||
PB -> V
|
||||
CC -> S
|
||||
NH -> B
|
||||
CV -> P
|
||||
SO -> V
|
||||
NC -> O
|
||||
HK -> K
|
||||
SB -> H
|
||||
OO -> V
|
||||
HO -> P
|
||||
PS -> B
|
||||
BC -> P
|
||||
KO -> C
|
||||
KB -> C
|
||||
VV -> F
|
||||
BS -> F
|
||||
HB -> B
|
||||
KN -> S
|
||||
FC -> C
|
||||
SN -> S
|
||||
HC -> O
|
||||
HP -> F
|
||||
BP -> V
|
||||
ON -> K
|
||||
BB -> K
|
||||
KH -> O
|
||||
NP -> H
|
||||
KS -> N
|
||||
SH -> K
|
||||
VP -> O
|
||||
PF -> O
|
||||
HF -> S
|
||||
BN -> S
|
||||
NK -> C
|
||||
FH -> O
|
||||
CH -> B
|
||||
KP -> B
|
||||
FN -> K
|
||||
OV -> P
|
||||
VS -> K
|
||||
OH -> V
|
||||
PC -> F
|
||||
VO -> H
|
||||
SK -> S
|
||||
PO -> O
|
||||
KF -> N
|
||||
NF -> V
|
||||
NB -> C
|
||||
PN -> O
|
||||
FS -> C
|
||||
PH -> F
|
||||
VN -> S
|
||||
OS -> V
|
||||
HV -> H
|
||||
HS -> B
|
||||
SC -> C
|
||||
23
day14/part1.py
Normal file
23
day14/part1.py
Normal file
@@ -0,0 +1,23 @@
|
||||
#!/usr/bin/env python3
|
||||
import re
|
||||
from collections import Counter
|
||||
from itertools import groupby
|
||||
from operator import itemgetter
|
||||
|
||||
lines = (x.strip() for x in open("input.txt"))
|
||||
first = next(lines)
|
||||
next(lines)
|
||||
|
||||
repl = (re.match(r"([A-Z]{2}) -> ([A-Z])", line).groups() for line in lines)
|
||||
repl = (v for (a, b), insert in repl for v in [(a + insert, a + b), (insert + b, a + b)])
|
||||
repl = {k: list(map(itemgetter(1), v)) for k, v in groupby(sorted(repl), itemgetter(0))}
|
||||
|
||||
combinations = Counter(first[a:a+2] for a in range(len(first)-1))
|
||||
for _ in range(10):
|
||||
combinations = {k: sum_val for k, v in repl.items() if (sum_val := sum(combinations.get(x, 0) for x in v)) > 0}
|
||||
|
||||
letters = ((key[0], cnt) for key, cnt in combinations.items())
|
||||
letters = {k: sum(map(itemgetter(1), v)) for k, v in groupby(sorted(letters), itemgetter(0))}
|
||||
letters[first[-1]] += 1
|
||||
|
||||
print(max(letters.values()) - min(letters.values()))
|
||||
4
day14/part2.min.py
Normal file
4
day14/part2.min.py
Normal file
@@ -0,0 +1,4 @@
|
||||
#!/usr/bin/env python3
|
||||
import collections as o;import itertools as i;l=[x.strip() for x in open("input.txt")];f=l[0];r=[(k,[*(x[1]for x in v)])for k,v in i.groupby(sorted(v for x in l[2:]for v in[(x[0]+x[-1],x[:2]),(x[-1]+x[1],x[:2])]),lambda x:x[0])];c=o.Counter(f[i:i+2]for i in range(len(f)-1))
|
||||
for _ in range(40):c={k:sum_val for k,v in r if(sum_val:=sum(c.get(x,0)for x in v))>0}
|
||||
e={k:sum(x[1]for x in v)for k,v in i.groupby(sorted([*c.items(),(f[-1],1)]),lambda x:x[0][0])};print(max(e.values())-min(e.values()))
|
||||
22
day14/part2.py
Normal file
22
day14/part2.py
Normal file
@@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env python3
|
||||
import re
|
||||
from collections import Counter
|
||||
from itertools import groupby
|
||||
from operator import itemgetter
|
||||
|
||||
lines = (x.strip() for x in open("input.txt"))
|
||||
first = next(lines)
|
||||
next(lines)
|
||||
|
||||
repl = (re.match(r"([A-Z]{2}) -> ([A-Z])", line).groups() for line in lines)
|
||||
repl = (v for (a, b), m in repl for v in [(a+m, a+b), (m+b, a+b)])
|
||||
repl = {k: list(map(itemgetter(1), v)) for k, v in groupby(sorted(repl), itemgetter(0))}
|
||||
|
||||
combinations = Counter(first[i:i+2] for i in range(len(first)-1))
|
||||
for _ in range(40):
|
||||
combinations = {k: sum_val for k, v in repl.items() if (sum_val := sum(combinations.get(x, 0) for x in v)) > 0}
|
||||
|
||||
letters = {k: sum(map(itemgetter(1), v)) for k, v in groupby(sorted(combinations.items()), lambda x: x[0][0])}
|
||||
letters[first[-1]] += 1
|
||||
|
||||
print(max(letters.values()) - min(letters.values()))
|
||||
100
day15/input.txt
Normal file
100
day15/input.txt
Normal file
@@ -0,0 +1,100 @@
|
||||
1153122486338196417261639921288211131723142991381714663988573539371125147579911789799911191116969182
|
||||
1831332932365721112271938859551961274184513439211372318789319176946137219492492161941188122228928142
|
||||
9821911543414142186827941122266167714126412612261328558992198212729611567923298114131222451713511114
|
||||
7246861617121926197913771333862216431691242331821373133472917112415411291112194329821255594119385913
|
||||
7283532811518311511133564162553362112249591513461311252729218443397781539589433111319711824661393631
|
||||
2515929137212718342949516922111713621859318118719617839125124564721161658335944181553941811143591517
|
||||
4916512552144322517215234299958319381621661767349167411222321248229439251953192958644239143882919241
|
||||
6262117322113477913692595649873914143546632433711128717141141195552948491953485631229194119276499991
|
||||
7392251899873812797958691238437453551415231331821364182879918291981738573962231152511911211128951113
|
||||
8413992251188993837397124261942199712837511841216512261395311785116811331719185479137512367447923291
|
||||
5231148122926582218424829117852548196215169569393712471373474748341292612868417935562413892295951885
|
||||
6969394124432223195175331296521928139597331582718251211495645114941612246194231312174117214121281842
|
||||
1492115983511981562257418211112925117322921195483112122256639228399618419541416852185938991328915818
|
||||
3337387528424632949433449211831892186689931141431821341112121116121183221372428184716111222586158651
|
||||
2919993831163121652312168296893876448879562949946693212289822132922233191147264817817431124414532533
|
||||
9831131365513121681981321111241225246211721245114513343139123311189247841338544122968113145391142716
|
||||
1727694314953352984564533684197836278119128336766459163262254212898918649112624992813951781316416374
|
||||
2271812917172151781117223114852112121981121667671421934421226898295554752996914271128911169789451951
|
||||
9641181231223141385164135392637559471954819529821233561311577143643837514229221871189471954194331152
|
||||
4842773719613551934224188641425166389122136165128121415358911721153164127212813948168736781136952951
|
||||
1192227966237198835311112231914836119223535631325132172552427754922112812179423656275381194611522453
|
||||
4587521661134142224185212119671223232647264672526518611961218313988518799959611424294177988911237867
|
||||
7841978128159981542211119531397179123564484381479727821915161363181216177352158299813648446813627222
|
||||
7284166795958241131999192919211621771144435542434154129323533329121215444282759763242637299473619699
|
||||
1521878281243112584389365171394192883659218227373853328197872435319181781919941925172732491338886311
|
||||
1327321441516991964782129484116368713248197522187147122388963111996334499727625711211235126217275463
|
||||
5432221784759463981982328941214217778116531411971739126861352255217191611697312757994113151311379972
|
||||
1185912419912167768598675227235243957241898718929234342122112138667448683289595212416151634918119782
|
||||
2142391197693165425931997914283877473934922668732952746725116377382215211222574165995121111118383226
|
||||
2517671922159312542191175127158214971341213911213276212516415975771618811321511144775331189114464662
|
||||
7422195152192114261215487181219928252132916876218849814118182116115931338892173123147871155216241119
|
||||
3619695411396123442155212519261891139921251199959962859246511384671182317569141243719313574613299114
|
||||
2895333123772993112294182624661491247795159971883633469119158351982981542649324118914796267121748913
|
||||
1421145424229377289371813129244589145113287748254253711637741529919416291278816369198475199772891934
|
||||
4226139942429431915575192843585749419348133173935216599874822673919238822771191128122524833751214529
|
||||
1682417751943991485271671248268311378571948341453293291196141934239152143595141717431161329855424112
|
||||
9916921853141162653286394526948121272378729683198391723494146711544133983626379417921189376352821798
|
||||
3251721727717131197339261311211519541459852173796114841169131848229719476992898439988142411881982514
|
||||
4224411911215323811873118144131911362211323994194273231611315238461136131411252251162414111299942115
|
||||
2245998155138161121631272285319936718281123752141998833128632147447117622119239893148242825136988411
|
||||
5128199191117746931424197696535944336182191931232292669562695112789951271493241291449919211342319956
|
||||
6185982121672329189115713515854662969113811769468312383127983222331916531821851915411131158716583266
|
||||
5447295461711381231589918947462282687649213121842921247116618391182167591292292272427141281715111177
|
||||
8257924948412473451961749898696413137392994416792374818227237143259116197488773961321624141163921432
|
||||
5189182291437314198489212141482184531697461226931512711191277526274712938812991613191947812232211244
|
||||
4555193184311911877999628197321551669874119131381183472179843972338461241911119924731168169352182272
|
||||
1121437621439149119353295941221222611786169917291122315558611419191633395499286111392381131171511238
|
||||
3181921181713184161942383164918982475421419929329239389548923261242417419233827235774153744131399151
|
||||
2144212113563135873312739193234226974131375159978272831447529333649424911425997613845276711174127394
|
||||
3139444361591195812958262711719931339591119823111113659752771219379921513182322111739482111579992212
|
||||
5621114341514728539451953111111724132613731214322289351224233177342291112529731353511549332152218981
|
||||
6158168164191463855184279714113662914367391459179668815811157124186552341548726599639264298312521815
|
||||
1995166266961996427961626535695212999191152112511338187611994115221111513691288611241921111251372396
|
||||
1339761931754125912894291611219167392229911558394529712234844524138351824911642926117836229449412319
|
||||
9127349377251112496211472777128795852299174781996397114134149188119193322618691616951273511925125149
|
||||
8979991927511499882118151229977284915581711491442571271831929283464123921696618123459172952156541118
|
||||
1911692955379514126841178174387511771789182282127314533841585231141954116191778276825221831612429254
|
||||
5812355192199983562136559621645794551755421236216311141149191254313741187262232164511315391297811915
|
||||
3424115732536482892978161186279711991592926127111589527519294376927741975118126955891355141746112114
|
||||
6989941658111289514721269122153856231312841221973781139894173114171591227833319376143957344117411911
|
||||
6125319278191932229114977252161577595177236842461172428859131892491678152164849198117325377432911476
|
||||
1152159324115134285596152231419112191947931122416535331212222762281719735281322382683941829919252797
|
||||
1119721171315374228473139951219963744542691955341851975111136422261596313992998971487819823291188481
|
||||
7916691142991816614421279131181771221333191348939869225827422531914334297979111781117184291912141817
|
||||
4459327512328523319571281171326141578961259819944448411558661132918284758431913397353129322141161339
|
||||
2636366417937791462122113461636413998893541599942374691221751914312218911394582119131724772161614249
|
||||
2312134166373181653911821772162314821142231391492122319111729983112421444715911629144339121416957937
|
||||
3112516582317416535542536957174341267787173494338391217911299591219692263281699341161832522294199592
|
||||
9514591666441193718119658181314241938166223118279113851527114813726452723842267929929348492111811581
|
||||
2482729124382111776179548733255881848149314161382118317221111921159882699921428496911916531781511634
|
||||
9673188569811459713271234169339644147411196161312836419131114224582591681121732551983658361231121738
|
||||
5977261429276572591351918817394787862812237116862142154391916283123565283389128193466927821786719949
|
||||
1769231445614929166298921531456368161915897211818781845929893127147256121829334316761162311125197837
|
||||
1312932111292124723831423127589975348261278421245946222431414328266141181139541218291313371199864618
|
||||
1591541125718121791926128149145611995157614226342145453466331146123719411716214432222151824877286273
|
||||
5619772119639321277117794132142411693117313872211776175416651697194197314115755297937932111148312514
|
||||
3178155419234246512532322711217369829144825612481941883499963679121511192214851616393725262185717671
|
||||
8114544152321812691913671222116334143125135245317392176495119442215315158941951195112914646111416558
|
||||
2741424121962339159791233465491165831161141431641127346424975111522251556164932982924154345265421893
|
||||
9921292358581466829119129911354441116986795112151124693382512674629191214183823931133116279292839481
|
||||
4569992111222111767136498792647332989342421348986559111618194724271412113395213251221446621936113217
|
||||
4991122344121493911982135121517399926352621891628211138999762133631115524112414391988299737366143123
|
||||
1114219232723168147329499525731115791152145213242183484163395711685154619941295185822161212821127794
|
||||
3839939524162212272145271115941119628111292192143171182921748857216541211912191929681297597929989911
|
||||
6189222318123913691725491531195561764199223114289691819733939824312544261311413928241912419255471755
|
||||
6426739442179681969621433347831187114554482194517792954314812523872919511933951481153159254111697312
|
||||
3891136638118997612371161161869992758221428676819193193631251844929339437511925493964819195151911142
|
||||
3115528121186224125618416185811129492665191334214154857416725217848287113213313571291989451911312351
|
||||
1921697432735261814811926199115788283324123249786652194311225419818513851175191913114233615261679173
|
||||
9444991493136334111115319771955169591434195411111175815119829517874288512561129573999727849941111891
|
||||
1431112211573298299618391113146347462491189192712317692165138111135921811621112921281114883152314218
|
||||
5349597921218431142281944271195411131831918391119511453181163891916131323533571231586827199329874375
|
||||
2393311319453249344541783217229449844183323339879189832259959321537131883822116122414429411226898557
|
||||
5457346279314981225362682181133799953141791522219253884328373198344561547931335524431911513277866156
|
||||
9699896735252183122231391818121491312934119311392547291169316419871163336631292698139515916993934313
|
||||
8556743922581192811645225163415825162574946272881497914233171916774511883981111996335911294624327449
|
||||
7521896193918149224732111989318369361944639558148182889252323156955979551215231329134223563528193297
|
||||
1911494977559119125389742991144919361368163938188656739221192361834613121494215789387959266616116631
|
||||
9511636215215555892846453398163358213162129919192181114219891142443311555291982463217261131119391179
|
||||
1549621141231921135259218425986586892253161313771915914851811321611989191411321126995515519721279591
|
||||
28
day15/part1.py
Normal file
28
day15/part1.py
Normal file
@@ -0,0 +1,28 @@
|
||||
#!/usr/bin/env python3
|
||||
import heapq
|
||||
|
||||
import numpy as np
|
||||
|
||||
arr = np.array([[int(n) for n in line.strip()] for line in open("input.txt")])
|
||||
|
||||
open_list = [(arr[0, 0], (0, 0))]
|
||||
closed_list = set()
|
||||
target = ty, tx = arr[:-1, :-1].shape
|
||||
|
||||
while len(open_list):
|
||||
val, curr = heapq.heappop(open_list)
|
||||
if curr in closed_list:
|
||||
continue
|
||||
if curr == target:
|
||||
print(val - arr[0, 0])
|
||||
break
|
||||
y, x = curr
|
||||
closed_list.add(curr)
|
||||
if 0 < y and (nxt := (y-1, x)) not in closed_list:
|
||||
heapq.heappush(open_list, (arr[nxt] + val, nxt))
|
||||
if 0 < x and (nxt := (y, x-1)) not in closed_list:
|
||||
heapq.heappush(open_list, (arr[nxt] + val, nxt))
|
||||
if ty > y and (nxt := (y+1, x)) not in closed_list:
|
||||
heapq.heappush(open_list, (arr[nxt] + val, nxt))
|
||||
if tx > x and (nxt := (y, x+1)) not in closed_list:
|
||||
heapq.heappush(open_list, (arr[nxt] + val, nxt))
|
||||
31
day15/part2.py
Normal file
31
day15/part2.py
Normal file
@@ -0,0 +1,31 @@
|
||||
#!/usr/bin/env python3
|
||||
import heapq
|
||||
|
||||
import numpy as np
|
||||
|
||||
arr = np.array([[int(n) for n in line.strip()] for line in open("input.txt")], dtype='int16')
|
||||
add = np.array([list(range(i, i+5)) for i in range(5)], dtype='int16')
|
||||
add = np.repeat(np.repeat(add, arr.shape[0], axis=0), arr.shape[1], axis=1)
|
||||
arr = (np.tile(arr, (5, 5)) + add - 1) % 9 + 1
|
||||
|
||||
open_list = [(arr[0, 0], (0, 0))]
|
||||
closed_list = set()
|
||||
target = ty, tx = arr[:-1, :-1].shape
|
||||
|
||||
while len(open_list):
|
||||
val, curr = heapq.heappop(open_list)
|
||||
if curr in closed_list:
|
||||
continue
|
||||
if curr == target:
|
||||
print(val - arr[0, 0])
|
||||
break
|
||||
y, x = curr
|
||||
closed_list.add(curr)
|
||||
if 0 < y and (nxt := (y-1, x)) not in closed_list:
|
||||
heapq.heappush(open_list, (arr[nxt] + val, nxt))
|
||||
if 0 < x and (nxt := (y, x-1)) not in closed_list:
|
||||
heapq.heappush(open_list, (arr[nxt] + val, nxt))
|
||||
if ty > y and (nxt := (y+1, x)) not in closed_list:
|
||||
heapq.heappush(open_list, (arr[nxt] + val, nxt))
|
||||
if tx > x and (nxt := (y, x+1)) not in closed_list:
|
||||
heapq.heappush(open_list, (arr[nxt] + val, nxt))
|
||||
86
day16/generator.py
Normal file
86
day16/generator.py
Normal file
@@ -0,0 +1,86 @@
|
||||
import random
|
||||
|
||||
def int_to_bin(num, l):
|
||||
return bin(num)[2:].zfill(l)
|
||||
|
||||
def literal(ver, num):
|
||||
bin_num = bin(num)[2:]
|
||||
bin_num = bin_num.zfill((len(bin_num)+3) // 4 * 4)
|
||||
return int_to_bin(ver, 3) + int_to_bin(4, 3) + ''.join(('1' if i+4 < len(bin_num) else "0") + bin_num[i:i+4] for i in range(0, len(bin_num), 4))
|
||||
|
||||
def sub_by_length(ver, op, subs):
|
||||
bin_sub = ''.join(subs)
|
||||
bin_len = int_to_bin(len(bin_sub), 15)
|
||||
return int_to_bin(ver, 3) + int_to_bin(op, 3) + '0' + bin_len + bin_sub
|
||||
|
||||
def sub_by_amount(ver, op, subs):
|
||||
bin_sub = ''.join(subs)
|
||||
bin_len = int_to_bin(len(subs), 11)
|
||||
return int_to_bin(ver, 3) + int_to_bin(op, 3) + '1' + bin_len + bin_sub
|
||||
|
||||
def bin_to_hex(bin_num):
|
||||
pad_len = (len(bin_num)+7) // 8 * 8 - len(bin_num)
|
||||
bin_num = bin_num + '0' * pad_len
|
||||
return ''.join(hex(int(bin_num[i:i+4], 2))[2:] for i in range(0, len(bin_num), 4)).upper()
|
||||
|
||||
print(bin_to_hex(
|
||||
literal(6, 2021)
|
||||
) == 'D2FE28')
|
||||
print(bin_to_hex(
|
||||
sub_by_length(1, 6, [
|
||||
literal(6, 10),
|
||||
literal(2, 20)
|
||||
])
|
||||
) == '38006F45291200')
|
||||
print(bin_to_hex(
|
||||
sub_by_amount(7, 3, [
|
||||
literal(2, 1),
|
||||
literal(4, 2),
|
||||
literal(1, 3)
|
||||
])
|
||||
) == 'EE00D40C823060')
|
||||
print(bin_to_hex(
|
||||
sub_by_amount(4, 2, [
|
||||
sub_by_amount(1, 2, [
|
||||
sub_by_length(5, 2, [
|
||||
literal(6, 15)
|
||||
])
|
||||
])
|
||||
])
|
||||
) == '8A004A801A8002F478')
|
||||
print(bin_to_hex(
|
||||
sub_by_amount(3, 0, [
|
||||
sub_by_length(0, 0, [
|
||||
literal(0, 10),
|
||||
literal(5, 11)
|
||||
]),
|
||||
sub_by_amount(1, 0, [
|
||||
literal(0, 12),
|
||||
literal(3, 13)
|
||||
])
|
||||
])
|
||||
) == '620080001611562C8802118E34')
|
||||
print(bin_to_hex(
|
||||
sub_by_length(3, 0, [
|
||||
sub_by_length(0, 0, [
|
||||
literal(0, 1000),
|
||||
literal(5, 51641)
|
||||
]),
|
||||
sub_by_length(1, 0, [
|
||||
literal(0, 65213),
|
||||
literal(3, 213511)
|
||||
]),
|
||||
sub_by_length(1, 0, [
|
||||
literal(0, 65213),
|
||||
literal(3, 213511)
|
||||
]),
|
||||
sub_by_length(1, 0, [
|
||||
literal(0, 65213),
|
||||
literal(3, 213511)
|
||||
]),
|
||||
sub_by_length(1, 0, [
|
||||
literal(0, 65213),
|
||||
literal(3, 213511)
|
||||
])
|
||||
])
|
||||
))
|
||||
11
day16/input.txt
Normal file
11
day16/input.txt
Normal file
@@ -0,0 +1,11 @@
|
||||
D2FE28
|
||||
38006F45291200
|
||||
EE00D40C823060
|
||||
8A004A801A8002F478
|
||||
620080001611562C8802118E34
|
||||
C0015000016115A2E0802F182340
|
||||
A0016C880162017C3686B18A3D4780
|
||||
#
|
||||
6006040002F127E459CCED24001C89FF6DAE4E9281C8003913FEDB5C9D250390007227FDB6B93A4A072000E44FFB6D7274940E
|
||||
#
|
||||
6051639005B56008C1D9BB3CC9DAD5BE97A4A9104700AE76E672DC95AAE91425EF6AD8BA5591C00F92073004AC0171007E0BC248BE0008645982B1CA680A7A0CC60096802723C94C265E5B9699E7E94D6070C016958F99AC015100760B45884600087C6E88B091C014959C83E740440209FC89C2896A50765A59CE299F3640D300827902547661964D2239180393AF92A8B28F4401BCC8ED52C01591D7E9D2591D7E9D273005A5D127C99802C095B044D5A19A73DC0E9C553004F000DE953588129E372008F2C0169FDB44FA6C9219803E00085C378891F00010E8FF1AE398803D1BE25C743005A6477801F59CC4FA1F3989F420C0149ED9CF006A000084C5386D1F4401F87310E313804D33B4095AFBED32ABF2CA28007DC9D3D713300524BCA940097CA8A4AF9F4C00F9B6D00088654867A7BC8BCA4829402F9D6895B2E4DF7E373189D9BE6BF86B200B7E3C68021331CD4AE6639A974232008E663C3FE00A4E0949124ED69087A848002749002151561F45B3007218C7A8FE600FC228D50B8C01097EEDD7001CF9DE5C0E62DEB089805330ED30CD3C0D3A3F367A40147E8023221F221531C9681100C717002100B36002A19809D15003900892601F950073630024805F400150D400A70028C00F5002C00252600698400A700326C0E44590039687B313BF669F35C9EF974396EF0A647533F2011B340151007637C46860200D43085712A7E4FE60086003E5234B5A56129C91FC93F1802F12EC01292BD754BCED27B92BD754BCED27B100264C4C40109D578CA600AC9AB5802B238E67495391D5CFC402E8B325C1E86F266F250B77ECC600BE006EE00085C7E8DF044001088E31420BCB08A003A72BF87D7A36C994CE76545030047801539F649BF4DEA52CBCA00B4EF3DE9B9CFEE379F14608
|
||||
18
day16/part1.py
Normal file
18
day16/part1.py
Normal file
@@ -0,0 +1,18 @@
|
||||
#!/usr/bin/env python3
|
||||
from day16.shared import parse_packets
|
||||
|
||||
lines = (x.strip() for x in open("input.txt"))
|
||||
|
||||
|
||||
def sum_version(exprs):
|
||||
return sum(expr[0] + (sum_version(expr[2]) if isinstance(expr[2], list) else 0) for expr in exprs)
|
||||
|
||||
|
||||
for line in lines:
|
||||
print("NEW LINE:", line)
|
||||
if line[0] == '#':
|
||||
continue
|
||||
bin_string = ''.join(bin(int(c, 16))[2:].zfill(4) for c in line)
|
||||
parsed = parse_packets(bin_string)
|
||||
print(sum_version(parsed))
|
||||
|
||||
39
day16/part2.py
Normal file
39
day16/part2.py
Normal file
@@ -0,0 +1,39 @@
|
||||
#!/usr/bin/env python3
|
||||
from functools import reduce
|
||||
from operator import mul
|
||||
|
||||
from day16.shared import parse_packets
|
||||
|
||||
lines = (x.strip() for x in open("input.txt"))
|
||||
|
||||
|
||||
def calc(expr):
|
||||
_, op, value = expr
|
||||
if op == 4:
|
||||
return value
|
||||
values = [calc(v) if isinstance(v, tuple) else v for v in value]
|
||||
if op == 0:
|
||||
return sum(values)
|
||||
elif op == 1:
|
||||
return reduce(mul, values)
|
||||
elif op == 2:
|
||||
return min(values)
|
||||
elif op == 3:
|
||||
return max(values)
|
||||
elif op == 4:
|
||||
return values
|
||||
elif op == 5:
|
||||
return 1 if values[0] > values[1] else 0
|
||||
elif op == 6:
|
||||
return 1 if values[0] < values[1] else 0
|
||||
elif op == 7:
|
||||
return 1 if values[0] == values[1] else 0
|
||||
|
||||
|
||||
for line in lines:
|
||||
print("NEW LINE:", line)
|
||||
if line[0] == '#':
|
||||
continue
|
||||
bin_string = ''.join(bin(int(c, 16))[2:].zfill(4) for c in line)
|
||||
parsed = parse_packets(bin_string)
|
||||
print(calc(parsed[0]))
|
||||
48
day16/shared.py
Normal file
48
day16/shared.py
Normal file
@@ -0,0 +1,48 @@
|
||||
def bin_to_int(bin_num):
|
||||
return int(bin_num, 2)
|
||||
|
||||
|
||||
def parse_literal(bin_num):
|
||||
res = ""
|
||||
i = 0
|
||||
while True:
|
||||
res += bin_num[i+1:i+5]
|
||||
i += 5
|
||||
if bin_num[i-5] == '0':
|
||||
break
|
||||
return bin_to_int(res), i
|
||||
|
||||
|
||||
def parse_op(bin_num):
|
||||
if bin_num[0] == '0':
|
||||
length = bin_to_int(bin_num[1:16])
|
||||
return parse_packets(bin_num[16:16+length]), length+16
|
||||
else:
|
||||
amount = bin_to_int(bin_num[1:12])
|
||||
pkgs = []
|
||||
i = 0
|
||||
for _ in range(amount):
|
||||
pkg, length = parse_packet(bin_num[12+i:])
|
||||
pkgs.append(pkg)
|
||||
i += length
|
||||
return pkgs, 12+i
|
||||
|
||||
|
||||
def parse_packet(bin_num):
|
||||
ver = bin_to_int(bin_num[:3])
|
||||
op = bin_to_int(bin_num[3:6])
|
||||
if op == 4:
|
||||
value, length = parse_literal(bin_num[6:])
|
||||
else:
|
||||
value, length = parse_op(bin_num[6:])
|
||||
return (ver, op, value), length + 6
|
||||
|
||||
|
||||
def parse_packets(bin_num):
|
||||
pkgs = []
|
||||
i = 0
|
||||
while '1' in bin_num[i:]:
|
||||
pkg, length = parse_packet(bin_num[i:])
|
||||
pkgs.append(pkg)
|
||||
i += length
|
||||
return pkgs
|
||||
Reference in New Issue
Block a user