Browse Source

Add solution for day 15 (inefficient)

pull/1/head
Maya Herrscher 4 years ago
parent
commit
0608b23e2a
  1. 164
      AoC2021/challenges/challenge15
  2. 75
      AoC2021/code/AoC15.py
  3. 100
      AoC2021/inputs/input15
  4. 10
      AoC2021/inputs/test15

164
AoC2021/challenges/challenge15

@ -0,0 +1,164 @@
--- Day 15: Chiton ---
You've almost reached the exit of the cave, but the walls are getting closer together. Your submarine can barely still fit, though; the main problem is that the walls of the cave are covered in chitons, and it would be best not to bump any of them.
The cavern is large, but has a very low ceiling, restricting your motion to two dimensions. The shape of the cavern resembles a square; a quick scan of chiton density produces a map of risk level throughout the cave (your puzzle input). For example:
1163751742
1381373672
2136511328
3694931569
7463417111
1319128137
1359912421
3125421639
1293138521
2311944581
You start in the top left position, your destination is the bottom right position, and you cannot move diagonally. The number at each position is its risk level; to determine the total risk of an entire path, add up the risk levels of each position you enter (that is, don't count the risk level of your starting position unless you enter it; leaving it adds no risk to your total).
Your goal is to find a path with the lowest total risk. In this example, a path with the lowest total risk is highlighted here:
1163751742
1381373672
2136511328
3694931569
7463417111
1319128137
1359912421
3125421639
1293138521
2311944581
The total risk of this path is 40 (the starting position is never entered, so its risk is not counted).
What is the lowest total risk of any path from the top left to the bottom right?
Your puzzle answer was 487.
--- Part Two ---
Now that you know how to find low-risk paths in the cave, you can try to find your way out.
The entire cave is actually five times larger in both dimensions than you thought; the area you originally scanned is just one tile in a 5x5 tile area that forms the full map. Your original map tile repeats to the right and downward; each time the tile repeats to the right or downward, all of its risk levels are 1 higher than the tile immediately up or left of it. However, risk levels above 9 wrap back around to 1. So, if your original map had some position with a risk level of 8, then that same position on each of the 25 total tiles would be as follows:
8 9 1 2 3
9 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
Each single digit above corresponds to the example position with a value of 8 on the top-left tile. Because the full map is actually five times larger in both dimensions, that position appears a total of 25 times, once in each duplicated tile, with the values shown above.
Here is the full five-times-as-large version of the first example above, with the original map in the top left corner highlighted:
11637517422274862853338597396444961841755517295286
13813736722492484783351359589446246169155735727126
21365113283247622439435873354154698446526571955763
36949315694715142671582625378269373648937148475914
74634171118574528222968563933317967414442817852555
13191281372421239248353234135946434524615754563572
13599124212461123532357223464346833457545794456865
31254216394236532741534764385264587549637569865174
12931385212314249632342535174345364628545647573965
23119445813422155692453326671356443778246755488935
22748628533385973964449618417555172952866628316397
24924847833513595894462461691557357271266846838237
32476224394358733541546984465265719557637682166874
47151426715826253782693736489371484759148259586125
85745282229685639333179674144428178525553928963666
24212392483532341359464345246157545635726865674683
24611235323572234643468334575457944568656815567976
42365327415347643852645875496375698651748671976285
23142496323425351743453646285456475739656758684176
34221556924533266713564437782467554889357866599146
33859739644496184175551729528666283163977739427418
35135958944624616915573572712668468382377957949348
43587335415469844652657195576376821668748793277985
58262537826937364893714847591482595861259361697236
96856393331796741444281785255539289636664139174777
35323413594643452461575456357268656746837976785794
35722346434683345754579445686568155679767926678187
53476438526458754963756986517486719762859782187396
34253517434536462854564757396567586841767869795287
45332667135644377824675548893578665991468977611257
44961841755517295286662831639777394274188841538529
46246169155735727126684683823779579493488168151459
54698446526571955763768216687487932779859814388196
69373648937148475914825958612593616972361472718347
17967414442817852555392896366641391747775241285888
46434524615754563572686567468379767857948187896815
46833457545794456865681556797679266781878137789298
64587549637569865174867197628597821873961893298417
45364628545647573965675868417678697952878971816398
56443778246755488935786659914689776112579188722368
55172952866628316397773942741888415385299952649631
57357271266846838237795794934881681514599279262561
65719557637682166874879327798598143881961925499217
71484759148259586125936169723614727183472583829458
28178525553928963666413917477752412858886352396999
57545635726865674683797678579481878968159298917926
57944568656815567976792667818781377892989248891319
75698651748671976285978218739618932984172914319528
56475739656758684176786979528789718163989182927419
67554889357866599146897761125791887223681299833479
Equipped with the full map, you can now find a path from the top left corner to the bottom right corner with the lowest total risk:
11637517422274862853338597396444961841755517295286
13813736722492484783351359589446246169155735727126
21365113283247622439435873354154698446526571955763
36949315694715142671582625378269373648937148475914
74634171118574528222968563933317967414442817852555
13191281372421239248353234135946434524615754563572
13599124212461123532357223464346833457545794456865
31254216394236532741534764385264587549637569865174
12931385212314249632342535174345364628545647573965
23119445813422155692453326671356443778246755488935
22748628533385973964449618417555172952866628316397
24924847833513595894462461691557357271266846838237
32476224394358733541546984465265719557637682166874
47151426715826253782693736489371484759148259586125
85745282229685639333179674144428178525553928963666
24212392483532341359464345246157545635726865674683
24611235323572234643468334575457944568656815567976
42365327415347643852645875496375698651748671976285
23142496323425351743453646285456475739656758684176
34221556924533266713564437782467554889357866599146
33859739644496184175551729528666283163977739427418
35135958944624616915573572712668468382377957949348
43587335415469844652657195576376821668748793277985
58262537826937364893714847591482595861259361697236
96856393331796741444281785255539289636664139174777
35323413594643452461575456357268656746837976785794
35722346434683345754579445686568155679767926678187
53476438526458754963756986517486719762859782187396
34253517434536462854564757396567586841767869795287
45332667135644377824675548893578665991468977611257
44961841755517295286662831639777394274188841538529
46246169155735727126684683823779579493488168151459
54698446526571955763768216687487932779859814388196
69373648937148475914825958612593616972361472718347
17967414442817852555392896366641391747775241285888
46434524615754563572686567468379767857948187896815
46833457545794456865681556797679266781878137789298
64587549637569865174867197628597821873961893298417
45364628545647573965675868417678697952878971816398
56443778246755488935786659914689776112579188722368
55172952866628316397773942741888415385299952649631
57357271266846838237795794934881681514599279262561
65719557637682166874879327798598143881961925499217
71484759148259586125936169723614727183472583829458
28178525553928963666413917477752412858886352396999
57545635726865674683797678579481878968159298917926
57944568656815567976792667818781377892989248891319
75698651748671976285978218739618932984172914319528
56475739656758684176786979528789718163989182927419
67554889357866599146897761125791887223681299833479
The total risk of this path is 315 (the starting position is still never entered, so its risk is not counted).
Using the full map, what is the lowest total risk of any path from the top left to the bottom right?
Your puzzle answer was 2821.
Both parts of this puzzle are complete! They provide two gold stars: **

75
AoC2021/code/AoC15.py

@ -0,0 +1,75 @@
#!/usr/bin/env python3
import sys
def memo(f):
memo = {}
def f2(a,x,y,c,e):
if (x,y) not in memo: memo[(x,y)] = f(a,x,y,c,e)
return memo[(x,y)]
return f2
def nb(sx, sy):
n = [(0,1), (1,0), (0,-1), (-1,0)]
return [(max(0,sx+x), max(0,sy+y)) for x,y in n]
def dijkstra(graph, s):
a = {}
v = {}
Q = initG(graph, s, a, v)
lq = len(Q)
while len(Q) > 0:
u = min([u for u in Q], key=lambda x: a[x])
Q.remove(u)
for vo in nb(*u):
if vo in Q:
distance_update(graph, u,vo,a,v)
if (len(Q)/lq * 100 % 1) == 0:
print("|", end='', flush=True)
print("\n")
return v
def initG(graph, s, a, v):
for vo in graph.keys():
a[vo] = 9999999999999999
v[vo] = 'null'
a[s] = 0
Q = [n for n in graph.keys()]
return Q
def distance_update(g, u,vo,a,v):
alt = a[u] + g[u]
if alt < a[vo]:
a[vo] = alt
v[vo] = u
def shortest_path(g, d, v):
w = [d]
u = d
r = 0
while v[u] != 'null':
r += g[u]
u = v[u]
w.append(u)
return w.reverse(), r
if __name__ == '__main__':
nums = [[int(i) for i in list(line.strip('\n'))] for line in open(sys.argv[1])]
# challenge 1
g1 = {(x,y) : nums[y][x] for x in range(len(nums[0])) for y in range(len(nums))}
v = dijkstra(g1, (0,0))
w, r = shortest_path(g1, (len(nums)-1, len(nums[0])-1), v)
res1 = str(r)
print("challenge 1:" +"\n" + res1 + "\n")
# challenge 2 - inefficient
g2 = {(x+i*len(nums[0]),y+j*len(nums)) : (nums[y][x] + i + j if nums[y][x] + i + j < 10 else (nums[y][x] + i + j) % 10 + 1) for x in range(len(nums[0])) for y in range(len(nums)) for i in range(5) for j in range(5)}
v = dijkstra(g2, (0,0))
w, r = shortest_path(g2, (len(nums[0])*5-1, len(nums)*5-1), v)
res2 = str(r)
print("challenge 2:" +"\n" + res2 + "\n")

100
AoC2021/inputs/input15

@ -0,0 +1,100 @@
3576219475874583191916312133474175459337114195988185136398151631965391991813219974121211251194786128
1713881999231519357799114192443351147195293575386923868711388519361669464234397975938889146414199688
2151922113728581598379349983928489926261239862882888968998791929828316718921297117449631496888879522
3291722311299652548445221254456535112944431135116631433192483199481594768169444158222431199929579291
1472287315918113113959972189351931626141472991992791413146153989481318998519171231313898962992319251
8978798318873934543151823824117132228417869392811216547172919119998739911121199246294815175868992773
4711149989992717728465321161611967173674239298593935117599173279267114155265393397571372124856718732
9843245319692998612781837729915931371619886371128818266322121623171343132131239399743799827972585645
7183211712173272827112268971459424836293613294388761928991542137194981939169315879277494581991465822
2385721996578834579224651431411717198341139751429156637229888849181154922897746418293184413519931124
6188313732929496115412842928273812153715747411177189187726751991191841181119822534511125656211991265
8797112815141319881991983914861912838966446818811271274141461487947171138945321912192198815813211761
6131514331472951494911549373681638637191295997625615885219668259269297171939941498581919718223529141
3916711951831246219991217199412116783411218814949692281987659123812288937983969116847986196936523779
9111121227192813382983442234171148191131821497435387936912532695293912972491131977272231919798477344
9994893855122115213919995636978443773161196511775674783979472217917498285423121574533491643891265199
9245818921321877317663254739619499915821162412924816263648913499811482474392667993739139899758919393
1717618849193152316325119128859116191132672215933282127611134982961918179152189411131259943819997123
3697848795188715264992962617223177221118919863975198881197837936258983111819488622362584888223331999
1798259211962116675922944295117391732295963342131192112612386194749389991199821195162975124619149837
5365696872959269494682128272373619348283964191994187749824841195582221478325111187843285531119934786
6717218734155417511564162235845524121493736962442884479191718924594119911521121422231982184671122116
9346412964321187889663345611132183519588187791111227269112129947122999176114184938494824342939375892
2912811929919891428891223818529115949522464779949526791515797886471246181118183121786524127979998512
7353285281129811915971956929718433284441492484117162562212973157265812816731843791838571691421923924
3239161911151141369392413114665193927469988999219478935992541193184945926828911597158489999651151172
9229716159183212219791136121569512978927999177196111291668152292896495877722611129462617229299962112
5994461796358342348216915883991574569784887978731368749888449599246152414391635925763156831839199496
1569319989929452917219726368583887924179117119284971126181346919789949393972171262535283921926293357
9983673949614595396426594299567925779229751982433631919159114841538978735893759492592481573495963491
3725632421623191323789991288299296415942162988289929992238877153268488127718372211951117951486111926
1219319492614741819142363452974731825929485665978219725131218962626179997611254524524915768686381164
8248145978417221154822663831966124539168641311913211295941167919583189792718599334616827916567313217
3483959252172951953519211189255336234459522397526829492685113849198611624111948187714151963198671919
7812289559812455122297839958719917624841136314322613942214613249662544617928521344873779598816941946
6868958791119815122313931178313637489923479492171741443993619161318781194421239654468391996636923611
6999166111129212624151555925991311538952878946191234511973499736237742491811348911419449161895978178
3532973784281182181552481928144111246711742443831184236962981395142863562121961323169398316921741321
8433727191521455193223914171391447439164791659118411391327413285243717794299999523811137619197999791
8169125649963232439519311131691539694589231314268683691811431259295132344195953951287639889737849189
8928161919934761971442719391119881113318323987227229831917897151155121657624142152884624176461913498
2949199492196424164751892146395174669111921799261527568751283712133148212821261936297739912939743599
9931616797885267969938591912672996183187926259991298429278229232257219428413174972999193796827459799
3138768154311742899419449741617489465993174645228814938145273266941566223933119891379181296548999222
1338961668848636157444719898575174699419997972125194827176897565699991611654214368232592793825617931
9283643143692917314535119897889711191946688911436965194232811624712861576291987716317193114445363435
8718611398299869991788113111214414269554542114329621286592313532782819131919527228417764279755899812
8128249329314753847725634378737239939769913811444529224619715471961999278915341955188871714271145449
1286217894699991715153139268991918988338684391252271254127193342854156438873836258484975151167996316
4191439845728418348368191516746499654147257851256861534634988281199187541199892317145428248531129171
9799283477747387253566811199414256363339122372629545249542373591951156699415998714124115397811359516
3191985767689943186838293175311429527499612824265518522881922917119891424717751985621941995373481629
8127933395192162886259956635985821651844893796541627799276258692298972881471991789861232424926758792
4349683335349673317521761437148933192989961489478395417258212949865876242843242464123471244631449998
3983861771861891251448475182984833811718959521263444111998834975935221115813839514917176912994677265
3361812177831144971531441184671112122322312961859915722393944269211333951428175372918136112814926411
2492891241191991149319731182431211211971267682931551534199328118919751416881458658314218331565282229
1316191719349114251772629411233483317516143684488291792691839198723174287211241498955137331159443961
1295632882814319179937625629218199381191127966181781921829819969815912382191294796979515315471941392
1952318366571121641614137937116326339638859628226299126699118638171813164397332391891669798271559135
1123676641998146639499323488991286491444174358479157916696196841912233773269986812299996975117612192
2496461124219121332118311532239527696198787778122345883331631119147159212262659717289674149798169111
3994915157217763242581922474512992479146377117411981999549977495462427199524334285127723311599337186
2856249932899117429471617195415229923432185189218399415357391954728651191591964713418532917111296399
1916415314958527255299763241981929186681257596912948977397875248189748655173819381269954513499896991
4214987991254389723346911149661118597277889213928551947599177915888233175182921923128853579525754718
3582279578128192231339943295188563998645998149526284997789731787116991199391984141983177825883111821
3318115949152512995142691231324793411691198458654473675135993122216324137859959272731599996923465431
9431374329994955614816239978928993493129129641248882311462522524447519388869238199852941551532223632
6749132793518479891929891686579791123712167784913331559922969149125915827657111724348929978159964476
6899888192198113996189996152913216258469182711411338723293823541574149295918323829819122941288693461
9833728958423343229397292861582211971713299293611778961979819175799893793971822162383179128129741328
9719459171294372288418126215914532995125999274359633129923191213332337513812761149562146497198614329
6323381739518574154814713691189651618965915153972197836392492518147816118266818195856772641337997448
9515391698994467556612815425651153225477392335219196697477512338938131933398177519117437353271396315
2881769116189531115832167881372917543267529333184621316918481786111252281738298936313393416198972192
1262821848371618529985323295192217356152412696271318793198457225899782896767339942896611231999996177
7611115824284211421951282951346181221891123993118231772195952872865982323819415318945742389969468591
6289176957971112617212793915412958239574931721216722823933461763146195116737161463663388274118352919
2739192179312181318764617662499189345995579913463399176128133623751919135855775349195561978131664671
7419179135228196296753972713643556671138381494838116814212892453118963456121883515171981991493112197
2351962484381996196119791327953881225881124837211622273581789216339733276411173141146816794919111532
4828494193941149581788515219884312818182198261739598611663311138255885557141492174934279519487959315
6266441934234532987821537914696156313934795144412166199339199381197916324959558778257894958593689511
1218836684761811498991162549547375891999161535133511817388897491266957819219117988952234747197245136
7714718988198712181711441996373181351671962319416917816121119483959397254815953421521642116185224622
6315878819598412161782318179586237893968466116199183185764132999388411414821117761121199725212571499
9298381112525419214597534611761811137687414291394795245955115116482261788724982532573616634235949295
2513837618149681781571514782119218925111312739891194994857915371836413112952729282168681511999132471
9859244419394114469927618849921646911349163815889492121537711577378978633345419421881312649299234749
6241913851991287776811271361777919519213726771122769111499925516752149976397933753482495291399144132
7549785323789821947513966787125532113931855716249321712383292746672763573114656989299712174939322181
9188852123922512181114117536955617335173328988385462159798591117758397298294919117614163738414981949
2974419341361292817129453536824618155299993142149899966226312193848695911943416589913541194129913422
1672761999113289962322634642122163368424955339864962951462929691975798622972131599198673671183449929
3462285147627397867977869242451116812198183328383813959284449969199819896174618466572298882951459875
6153638634925613615423256118159531429294172496996993184762972841759153392753653191942715379879852345
6516291128536155113712481272992636373774429965994966353494115221979925592128929821611928492143929534
3993993153713717844719492314391992638719111242291783837279215399219822611221331985991484863975594721
5995891457939115884519762228128552168915218299831775259316392719113962218429351553267511523198149462

10
AoC2021/inputs/test15

@ -0,0 +1,10 @@
1163751742
1381373672
2136511328
3694931569
7463417111
1319128137
1359912421
3125421639
1293138521
2311944581
Loading…
Cancel
Save