parent
							
								
									2aff679fea
								
							
						
					
					
						commit
						3a843c6b3e
					
				@ -0,0 +1,100 @@
 | 
				
			||||
7257173117871274148119191397411866741961893915191233591781237171134183111991588567743913598398999129
 | 
				
			||||
9199956198317542321811795167539333593959845233586245178771259721629672984999396181817424129792113429
 | 
				
			||||
5678391114239515741618386378916979112716269165195269215584939279279177393199175187199511833999819385
 | 
				
			||||
7149919968927397171639311441114199789621399783958844626227879671633692776519178119978312379854918472
 | 
				
			||||
9571133571872511883799281362819193417112413972551887416264299171789218539132957196979811132721291672
 | 
				
			||||
8172584925581929988661688649146133971138896125391412411926754121134983167188929529959966385437588844
 | 
				
			||||
4914518468152915388917729744941583692326581473756415317665271248313684872717734981831164961169161441
 | 
				
			||||
2519894881987711813817528992187872878321614865385871812543911299816979364869972221856158852969592112
 | 
				
			||||
9859391668949943293812278997458981968712239911133451788394169935992685191958894671638974814216612719
 | 
				
			||||
2192529153611518191595896814996141119517992288611125714717119399491112913829241231917539211119881849
 | 
				
			||||
2337843263693226789699931111398112969887535286492296226999311848178157169949315516494848924316823474
 | 
				
			||||
1191281328169873275751982595447194592918411459316261266123821567822849396139339812112776322962112239
 | 
				
			||||
4587143466247931931363896317279914687959418292417719618921579781314932674121288194141812419924513491
 | 
				
			||||
9188217988495399118711317519126435116785399685763129885168131465437119937687542179912685827611129973
 | 
				
			||||
9281999423754831489181352222362255825969879819312357829319722961312991751988497399311813231979551234
 | 
				
			||||
8411642818749914959745792191319718911388132253292482346295961171634916811938584616484592994128913896
 | 
				
			||||
3388255416913192166863912671592797172349778144861261162626679169122118178816999781511482128586841931
 | 
				
			||||
1544111521579847285289829899211419185418871795132727999177828715458885628798278694521919225186212479
 | 
				
			||||
6112394533149912224244478554154888237766677192511968451313528663845219413639188496229811993163929889
 | 
				
			||||
7198784899159217522265428239842119185341314211991273329136173711277137989451533985143943379599711279
 | 
				
			||||
5322174898961411153515736312827315967799457744138498991267929611147897735269199298727139863199825449
 | 
				
			||||
9389598982325839969741219429682597992913484299849122148988986313319119891881251311761168419988993984
 | 
				
			||||
9731391996727591141584769372292618329676991274389449899178113115589128811112539136473142798111999659
 | 
				
			||||
5158499781719185191335698726337225419942769281296219215168381368296377583192114793345154114939919138
 | 
				
			||||
9489436472297254387595375823195948188155998979224885397929111219894329444244314131186498373329899628
 | 
				
			||||
5115948294552681116994961691838819122926817811428719719487293692225795236769313128246954783896581419
 | 
				
			||||
7842378538599781529689123131749873922371451111511113114844145628981899497289212258729489151386198379
 | 
				
			||||
5657182954384884192389177113923946799271142897712314759172399912237297817994565459889643847449996591
 | 
				
			||||
2114463882686822117918787199822775881529895139151499196692246274147217497821149527235387719991114714
 | 
				
			||||
2911917115481432912158219697929535145897818898214661343833193385121919978318122826712681212138729941
 | 
				
			||||
7919482212881442617559399592111184197611192148899237895295211389594619418194519266268597128875922164
 | 
				
			||||
5122782781762591222295946745991662147485323299849117125858933893496129327282821421189644352917799563
 | 
				
			||||
1136291298829627566691527929262125187126922289732289227716821815619551911944779995415278137991622552
 | 
				
			||||
4929328679851289388213321419985631998929472889692146329429231293698273199118713262892523784612611999
 | 
				
			||||
1327734151191996421344997185478394257228713274441955249899986342244731153572974191897991393662691931
 | 
				
			||||
2781117154312271979199389368319489151943763236781711191122134726691762925242929616867585411791129159
 | 
				
			||||
1439186637181196485238285593819594866112889871679966657834249176324314497819969991879491995926214924
 | 
				
			||||
8198715159568382591431231227339698151116662552918333813978498567586137499214111391961922138831958891
 | 
				
			||||
1791636999843894411347725615937522999231159118674816321332412988177251964743241117549699136494419193
 | 
				
			||||
2347191949569898652196299127952395252161168176339428456658821198913454439135327112298841851793131139
 | 
				
			||||
6517373396832351522818699933865532731822114597879322225665521379149517111835298818315911951121182358
 | 
				
			||||
9989219811198438961112789214341437915858986541454335893139567183897549391541933121816392249992757195
 | 
				
			||||
2751242393169519965822749148113198418478749175225597965669585119898274413133594511599239813819261729
 | 
				
			||||
2333615561781383963589211869624423159983279225123169449222598319329695159532597919931391498257683522
 | 
				
			||||
2326925593466238521963381246934599431321918392819124294951311331116521125288316176431186199751635491
 | 
				
			||||
9341367399225381779967242896675118399271292593248972922271792198875912499185119129993955719191131219
 | 
				
			||||
9419488192386241298831213479619891591112812195761439362996925799233613913141343831817117189287994755
 | 
				
			||||
9695968821621727482419983392371417945181396516317947384995853931879231912925789698357961179754119511
 | 
				
			||||
9819267839831973911319938469292179778511998125121931891146223692296192299952153342285175781729419271
 | 
				
			||||
1863816696271622173271985549176326189359343784972161815896794319879938399995919111197381311974788614
 | 
				
			||||
9951198498951459991236295523134837224482241663114944798913138999819143627393989956711324837996945589
 | 
				
			||||
5636493872291432645969776969437321939689259195165871611951271619514731193819693277839717689838523535
 | 
				
			||||
1599873877797591958659883217591162792939133915996748148738729819416279661719716399869947198411871934
 | 
				
			||||
8794639995351381527537286278892222682139243726394685977188691436889598769141824739519232576663818623
 | 
				
			||||
9312694112319895396113225123691699114916381799538178372616115397895462387932975997638239393799315212
 | 
				
			||||
5832652557695274383198568431192817489141326899824429893139485722158832888768989154554522478498498714
 | 
				
			||||
8782845924111831912127921565587919113818125774499381223562975114549551996121626761137916739478221568
 | 
				
			||||
9811929922686225152399211122397597158513891191615161416432783564767191169849849511985178913416153852
 | 
				
			||||
1137711214593792114391112795839813891552389836983814596893489181536998977653386261925692938856897897
 | 
				
			||||
2891454257881276297539979515161138819723112914812122617911912212922972482812982987377897974782821188
 | 
				
			||||
7671589249839457412545235186153935156941884381134221489611219191921272892597772389234492565914198258
 | 
				
			||||
1552483399116635372974898931188883743171689242311789612968114416972112951486134993116143393399793998
 | 
				
			||||
8711188851122588629852724495172527356897321495229616597821333465358285422331924491418522517979118993
 | 
				
			||||
9593315278649975951219952268211871741223727356181132397183838894499829221111978873973591418997289513
 | 
				
			||||
3969997988729933184995613429919894232321166598283911973373115779211228919166877393889434215381781389
 | 
				
			||||
5588221472469964598623295656797377199853717996631331495111278692241929496187813341637872397899624175
 | 
				
			||||
9921637195158912596622263381891921897978588191413981249169189173199699294541171879989121615634351342
 | 
				
			||||
3798931227871974751799777995169123924251123997219388112822621636398239724484812154741112812741472923
 | 
				
			||||
1318745799964813125829815417129787311384489283415939129112478189111179617966392125599951899588399933
 | 
				
			||||
9919168243159595589113819319611976889991646271868112472392818222893941139743434115977476121333191818
 | 
				
			||||
3115972825613468816687921319481739413919883837749478584158912513417673612383549994987313463899975111
 | 
				
			||||
9819245879871957475393932424291294189961391917998628819131487621819218799996929997391516748191973218
 | 
				
			||||
2543119159599118542114782197275925148321199297951316238291197471953953113117231732381348855871885341
 | 
				
			||||
8597192929968786896989279839331134411223192189898926119875521124153282436126568329811999323445191679
 | 
				
			||||
3975785449971831818312568579176154978689122133184732928183381881141862482849263341279915278911683894
 | 
				
			||||
3179111996949988298153199151389479257689125228811639199771592599998978715664289996372311512944263139
 | 
				
			||||
3717731949295466943999325285695444691229948881923328813265999285999127111938229969918689613331778988
 | 
				
			||||
3899774572142914257631398127999644341916848491762691872194259535697139312281868155191287342929193183
 | 
				
			||||
3419531741121147372119481389754229993641415577459396295899232815823975392974927395711568972974293925
 | 
				
			||||
1722176361244419285779288543598751211392531919462574932268633933695192352441119172662173735119515712
 | 
				
			||||
8259287379497359112197399192874789929899857534134534892997117372192299398799696792833998919968157692
 | 
				
			||||
9113588331521199442922126152433321148852971919741151158559612459789766141191421226791434779238851542
 | 
				
			||||
9934485995397918323116671821935121371411639119196141711221111598878781853442188129954293234742142941
 | 
				
			||||
5863996738774189559631311914763988372133998564893667341114799688887419869498499571261945352924543659
 | 
				
			||||
1219173111621632848325722799121771234351313226119137299611516914998734631224397849155891733316457236
 | 
				
			||||
2581119126696683198948223933432829124786552193519396275241779267529656466959196852322344769712989425
 | 
				
			||||
9967141179556989992264146994141761832265411668193157248414232474791714884711961318296968187812563731
 | 
				
			||||
2489879759889926897312354996751186797873869898953112749555984922414591917338857833991412625172181498
 | 
				
			||||
9389457158881973888911321813181681241492541783691881299169396894618761158125679495966134894392982199
 | 
				
			||||
9619992269841241296891999961333629572281414928995452682633291959449286127129612124227252269791663654
 | 
				
			||||
1614835267511194414153578263179929291589313129387133978158911722592272783869578732691311815861241491
 | 
				
			||||
9652116592834327221168619761719533625339966299211384947128885411279177514867942937963481178919941711
 | 
				
			||||
2933298721252991578245341817819977199267549571111491876711439883927615991991196211296294697272313774
 | 
				
			||||
1431715794741427753127381461152185596778586461419451428969132784392522194651111917169662896769767812
 | 
				
			||||
5919195742129923589897349316296264144952749521471522921641452152978765849165829872135669664214112327
 | 
				
			||||
6339474432243943991959119983361889756918344251994119969825144749941178381321286493218961118191911875
 | 
				
			||||
5517228343299391617317192214769719192189853799115811159493116197825637676714396432979816281155693495
 | 
				
			||||
6388461935842284623142112416943162883259817194297799981283439458172297191119123521515932483495126961
 | 
				
			||||
7843191912999868941538811232841971381689242136381989791436989848478992513449168132392761291119899184
 | 
				
			||||
1424114349573937523917871789194999469999119751164761884221519813273935995996288159921682217898434989
 | 
				
			||||
@ -0,0 +1,192 @@
 | 
				
			||||
#!/usr/bin/env julia
 | 
				
			||||
 | 
				
			||||
#=
 | 
				
			||||
--- 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?
 | 
				
			||||
=#
 | 
				
			||||
 | 
				
			||||
infile = length(ARGS) > 0 ? ARGS[1] : "input.txt"
 | 
				
			||||
println("infile = ", infile)
 | 
				
			||||
 | 
				
			||||
risk_map = reduce(vcat, [parse.(Int, split(line, ""))'
 | 
				
			||||
                  for line in  eachline(infile)])
 | 
				
			||||
 | 
				
			||||
function least_risk(E)
 | 
				
			||||
    nrows, ncols = size(E)
 | 
				
			||||
    row = nrows
 | 
				
			||||
    col = ncols
 | 
				
			||||
    cost = 0
 | 
				
			||||
    path = zeros(Int, size(E))
 | 
				
			||||
    while row > 1 || col > 1
 | 
				
			||||
        cost += E[row, col]
 | 
				
			||||
        path[row, col] = 1
 | 
				
			||||
        if col == 1
 | 
				
			||||
            row -= 1
 | 
				
			||||
        elseif row == 1
 | 
				
			||||
            col -= 1
 | 
				
			||||
        else
 | 
				
			||||
            if E[row, col-1] < E[row-1, col]
 | 
				
			||||
                col -= 1
 | 
				
			||||
            else
 | 
				
			||||
                row -= 1
 | 
				
			||||
            end
 | 
				
			||||
        end
 | 
				
			||||
    end
 | 
				
			||||
    return cost, path
 | 
				
			||||
end
 | 
				
			||||
 | 
				
			||||
function neighbor_idx(nrows, ncols, y, x)
 | 
				
			||||
    v = CartesianIndex[]
 | 
				
			||||
    if x > 1
 | 
				
			||||
        push!(v, CartesianIndex(y, x-1))
 | 
				
			||||
    end
 | 
				
			||||
    if x < ncols
 | 
				
			||||
        push!(v, CartesianIndex(y, x+1))
 | 
				
			||||
    end
 | 
				
			||||
    if y > 1
 | 
				
			||||
        push!(v, CartesianIndex(y-1, x))
 | 
				
			||||
    end
 | 
				
			||||
    if y < nrows
 | 
				
			||||
        push!(v, CartesianIndex(y+1, x))
 | 
				
			||||
    end
 | 
				
			||||
    return v
 | 
				
			||||
end
 | 
				
			||||
 | 
				
			||||
function get_costs(R)
 | 
				
			||||
    costs = zeros(Int, size(R))
 | 
				
			||||
    dirs = zeros(Int, size(R))
 | 
				
			||||
    nrows, ncols = size(R)
 | 
				
			||||
    for col in 2:ncols
 | 
				
			||||
        costs[1, col] = costs[1, col-1] + R[1, col]
 | 
				
			||||
    end
 | 
				
			||||
    for row in 2:nrows
 | 
				
			||||
        costs[row, 1] = costs[row-1, 1] + R[row, 1]
 | 
				
			||||
        for col in 2:ncols
 | 
				
			||||
            if costs[row-1, col] < costs[row, col-1]
 | 
				
			||||
                dirs[row, col] = 1
 | 
				
			||||
                costs[row, col] = costs[row-1, col] + R[row, col]
 | 
				
			||||
            else
 | 
				
			||||
                costs[row, col] = costs[row, col-1] + R[row, col]
 | 
				
			||||
            end
 | 
				
			||||
        end
 | 
				
			||||
    end
 | 
				
			||||
 | 
				
			||||
    if false
 | 
				
			||||
    # more passes
 | 
				
			||||
    for i in 1:max(nrows, ncols)
 | 
				
			||||
        for col in ncols-1:-1:1
 | 
				
			||||
            rcost = costs[end, col+1] + R[end, col]
 | 
				
			||||
            if rcost < costs[end, col]
 | 
				
			||||
                costs[end, col] = rcost
 | 
				
			||||
            end
 | 
				
			||||
        end
 | 
				
			||||
        for row in nrows-1:-1:1
 | 
				
			||||
            dcost = costs[row+1, end] + R[row, end]
 | 
				
			||||
            if dcost < costs[row, end]
 | 
				
			||||
                costs[row, end] = dcost
 | 
				
			||||
            end
 | 
				
			||||
            for col in ncols-1:-1:1
 | 
				
			||||
                rcost = costs[row, col+1] + R[row, col]
 | 
				
			||||
                if rcost < costs[row, col]
 | 
				
			||||
                    costs[row, col] = rcost
 | 
				
			||||
                end
 | 
				
			||||
                dcost = costs[row+1, col] + R[row, col]
 | 
				
			||||
                if dcost < costs[row, col]
 | 
				
			||||
                    costs[row, col] = dcost
 | 
				
			||||
                end
 | 
				
			||||
            end
 | 
				
			||||
        end
 | 
				
			||||
    end
 | 
				
			||||
    end
 | 
				
			||||
 | 
				
			||||
    for row in nrows:-1:1
 | 
				
			||||
        for col in ncols:-1:1
 | 
				
			||||
            idx = neighbor_idx(nrows, ncols, row, col)
 | 
				
			||||
            c = minimum(costs[i] for i in idx) + R[row, col]
 | 
				
			||||
            if c < costs[row, col]
 | 
				
			||||
                costs[row, col] = c
 | 
				
			||||
            end
 | 
				
			||||
        end
 | 
				
			||||
    end
 | 
				
			||||
 | 
				
			||||
    for row in nrows:-1:1
 | 
				
			||||
        for col in 1:ncols
 | 
				
			||||
            idx = neighbor_idx(nrows, ncols, row, col)
 | 
				
			||||
            c = minimum(costs[i] for i in idx) + R[row, col]
 | 
				
			||||
            if c < costs[row, col]
 | 
				
			||||
                costs[row, col] = c
 | 
				
			||||
            end
 | 
				
			||||
        end
 | 
				
			||||
    end
 | 
				
			||||
 | 
				
			||||
    for row in 1:nrows
 | 
				
			||||
        for col in ncols:-1:1
 | 
				
			||||
            idx = neighbor_idx(nrows, ncols, row, col)
 | 
				
			||||
            c = minimum(costs[i] for i in idx) + R[row, col]
 | 
				
			||||
            if c < costs[row, col]
 | 
				
			||||
                costs[row, col] = c
 | 
				
			||||
            end
 | 
				
			||||
        end
 | 
				
			||||
    end
 | 
				
			||||
 | 
				
			||||
    for row in 1:nrows
 | 
				
			||||
        for col in 1:ncols
 | 
				
			||||
            idx = neighbor_idx(nrows, ncols, row, col)
 | 
				
			||||
            c = minimum(costs[i] for i in idx) + R[row, col]
 | 
				
			||||
            if c < costs[row, col]
 | 
				
			||||
                costs[row, col] = c
 | 
				
			||||
            end
 | 
				
			||||
        end
 | 
				
			||||
    end
 | 
				
			||||
 | 
				
			||||
    return costs, dirs
 | 
				
			||||
end
 | 
				
			||||
 | 
				
			||||
#=
 | 
				
			||||
cost, path = least_risk(risk_map)
 | 
				
			||||
 | 
				
			||||
display(path)
 | 
				
			||||
println()
 | 
				
			||||
 | 
				
			||||
println("cost = ", cost)
 | 
				
			||||
=#
 | 
				
			||||
 | 
				
			||||
costs, dirs = get_costs(risk_map)
 | 
				
			||||
 | 
				
			||||
display(costs)
 | 
				
			||||
println()
 | 
				
			||||
 | 
				
			||||
println("cost = ", costs[end, end])
 | 
				
			||||
@ -0,0 +1,284 @@
 | 
				
			||||
#!/usr/bin/env julia
 | 
				
			||||
 | 
				
			||||
#=
 | 
				
			||||
--- 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?
 | 
				
			||||
=#
 | 
				
			||||
 | 
				
			||||
infile = length(ARGS) > 0 ? ARGS[1] : "input.txt"
 | 
				
			||||
println("infile = ", infile)
 | 
				
			||||
 | 
				
			||||
risk_map = reduce(vcat, [parse.(Int, split(line, ""))'
 | 
				
			||||
                  for line in  eachline(infile)])
 | 
				
			||||
 | 
				
			||||
function tile_map(R)
 | 
				
			||||
    R2 = Matrix{eltype(R)}(undef, size(R) .* 5)
 | 
				
			||||
    nrows, ncols = size(R)
 | 
				
			||||
    for tile_row in 0:4
 | 
				
			||||
        for tile_col in 0:4
 | 
				
			||||
            add = tile_row + tile_col
 | 
				
			||||
            rowstart = 1 + tile_row*nrows
 | 
				
			||||
            rowend   = rowstart + nrows - 1
 | 
				
			||||
            colstart = 1 + tile_col*ncols
 | 
				
			||||
            colend   = colstart + ncols - 1
 | 
				
			||||
            R2[rowstart:rowend, colstart:colend] = mod.(R .+ add .- 1, 9) .+ 1
 | 
				
			||||
        end
 | 
				
			||||
    end
 | 
				
			||||
    return R2
 | 
				
			||||
end
 | 
				
			||||
 | 
				
			||||
risk_map = tile_map(risk_map)
 | 
				
			||||
 | 
				
			||||
#display(risk_map)
 | 
				
			||||
#println()
 | 
				
			||||
#exit()
 | 
				
			||||
 | 
				
			||||
function neighbor_idx(nrows, ncols, y, x)
 | 
				
			||||
    v = CartesianIndex[]
 | 
				
			||||
    if x > 1
 | 
				
			||||
        push!(v, CartesianIndex(y, x-1))
 | 
				
			||||
    end
 | 
				
			||||
    if x < ncols
 | 
				
			||||
        push!(v, CartesianIndex(y, x+1))
 | 
				
			||||
    end
 | 
				
			||||
    if y > 1
 | 
				
			||||
        push!(v, CartesianIndex(y-1, x))
 | 
				
			||||
    end
 | 
				
			||||
    if y < nrows
 | 
				
			||||
        push!(v, CartesianIndex(y+1, x))
 | 
				
			||||
    end
 | 
				
			||||
    return v
 | 
				
			||||
end
 | 
				
			||||
 | 
				
			||||
function get_costs(R)
 | 
				
			||||
    costs = zeros(Int, size(R))
 | 
				
			||||
    dirs = zeros(Int, size(R))
 | 
				
			||||
    nrows, ncols = size(R)
 | 
				
			||||
    for col in 2:ncols
 | 
				
			||||
        costs[1, col] = costs[1, col-1] + R[1, col]
 | 
				
			||||
    end
 | 
				
			||||
    for row in 2:nrows
 | 
				
			||||
        costs[row, 1] = costs[row-1, 1] + R[row, 1]
 | 
				
			||||
        for col in 2:ncols
 | 
				
			||||
            if costs[row-1, col] < costs[row, col-1]
 | 
				
			||||
                dirs[row, col] = 1
 | 
				
			||||
                costs[row, col] = costs[row-1, col] + R[row, col]
 | 
				
			||||
            else
 | 
				
			||||
                costs[row, col] = costs[row, col-1] + R[row, col]
 | 
				
			||||
            end
 | 
				
			||||
        end
 | 
				
			||||
    end
 | 
				
			||||
 | 
				
			||||
    if false
 | 
				
			||||
    # more passes
 | 
				
			||||
    for i in 1:max(nrows, ncols)
 | 
				
			||||
        for col in ncols-1:-1:1
 | 
				
			||||
            rcost = costs[end, col+1] + R[end, col]
 | 
				
			||||
            if rcost < costs[end, col]
 | 
				
			||||
                costs[end, col] = rcost
 | 
				
			||||
            end
 | 
				
			||||
        end
 | 
				
			||||
        for row in nrows-1:-1:1
 | 
				
			||||
            dcost = costs[row+1, end] + R[row, end]
 | 
				
			||||
            if dcost < costs[row, end]
 | 
				
			||||
                costs[row, end] = dcost
 | 
				
			||||
            end
 | 
				
			||||
            for col in ncols-1:-1:1
 | 
				
			||||
                rcost = costs[row, col+1] + R[row, col]
 | 
				
			||||
                if rcost < costs[row, col]
 | 
				
			||||
                    costs[row, col] = rcost
 | 
				
			||||
                end
 | 
				
			||||
                dcost = costs[row+1, col] + R[row, col]
 | 
				
			||||
                if dcost < costs[row, col]
 | 
				
			||||
                    costs[row, col] = dcost
 | 
				
			||||
                end
 | 
				
			||||
            end
 | 
				
			||||
        end
 | 
				
			||||
    end
 | 
				
			||||
    end
 | 
				
			||||
 | 
				
			||||
    for i in 1:max(nrows, ncols)
 | 
				
			||||
 | 
				
			||||
        for row in nrows:-1:1
 | 
				
			||||
            for col in ncols:-1:1
 | 
				
			||||
                idx = neighbor_idx(nrows, ncols, row, col)
 | 
				
			||||
                c = minimum(costs[i] for i in idx) + R[row, col]
 | 
				
			||||
                if c < costs[row, col]
 | 
				
			||||
                    costs[row, col] = c
 | 
				
			||||
                end
 | 
				
			||||
            end
 | 
				
			||||
        end
 | 
				
			||||
 | 
				
			||||
        for row in nrows:-1:1
 | 
				
			||||
            for col in 1:ncols
 | 
				
			||||
                idx = neighbor_idx(nrows, ncols, row, col)
 | 
				
			||||
                c = minimum(costs[i] for i in idx) + R[row, col]
 | 
				
			||||
                if c < costs[row, col]
 | 
				
			||||
                    costs[row, col] = c
 | 
				
			||||
                end
 | 
				
			||||
            end
 | 
				
			||||
        end
 | 
				
			||||
 | 
				
			||||
        for row in 1:nrows
 | 
				
			||||
            for col in ncols:-1:1
 | 
				
			||||
                idx = neighbor_idx(nrows, ncols, row, col)
 | 
				
			||||
                c = minimum(costs[i] for i in idx) + R[row, col]
 | 
				
			||||
                if c < costs[row, col]
 | 
				
			||||
                    costs[row, col] = c
 | 
				
			||||
                end
 | 
				
			||||
            end
 | 
				
			||||
        end
 | 
				
			||||
 | 
				
			||||
        for row in 1:nrows
 | 
				
			||||
            for col in 1:ncols
 | 
				
			||||
                idx = neighbor_idx(nrows, ncols, row, col)
 | 
				
			||||
                c = minimum(costs[i] for i in idx) + R[row, col]
 | 
				
			||||
                if c < costs[row, col]
 | 
				
			||||
                    costs[row, col] = c
 | 
				
			||||
                end
 | 
				
			||||
            end
 | 
				
			||||
        end
 | 
				
			||||
 | 
				
			||||
    end
 | 
				
			||||
 | 
				
			||||
    return costs, dirs
 | 
				
			||||
end
 | 
				
			||||
 | 
				
			||||
#=
 | 
				
			||||
cost, path = least_risk(risk_map)
 | 
				
			||||
 | 
				
			||||
display(path)
 | 
				
			||||
println()
 | 
				
			||||
 | 
				
			||||
println("cost = ", cost)
 | 
				
			||||
=#
 | 
				
			||||
 | 
				
			||||
costs, dirs = get_costs(risk_map)
 | 
				
			||||
 | 
				
			||||
if length(costs) < 1000
 | 
				
			||||
    display(costs)
 | 
				
			||||
    println()
 | 
				
			||||
end
 | 
				
			||||
 | 
				
			||||
println("cost = ", costs[end, end])
 | 
				
			||||
@ -0,0 +1,10 @@
 | 
				
			||||
1163751742
 | 
				
			||||
1381373672
 | 
				
			||||
2136511328
 | 
				
			||||
3694931569
 | 
				
			||||
7463417111
 | 
				
			||||
1319128137
 | 
				
			||||
1359912421
 | 
				
			||||
3125421639
 | 
				
			||||
1293138521
 | 
				
			||||
2311944581
 | 
				
			||||
@ -0,0 +1,50 @@
 | 
				
			||||
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
 | 
				
			||||
@ -0,0 +1,4 @@
 | 
				
			||||
19111
 | 
				
			||||
19191
 | 
				
			||||
11191
 | 
				
			||||
99991
 | 
				
			||||
@ -0,0 +1,5 @@
 | 
				
			||||
11199
 | 
				
			||||
99199
 | 
				
			||||
11199
 | 
				
			||||
19999
 | 
				
			||||
11111
 | 
				
			||||
					Loading…
					
					
				
		Reference in new issue