Compare commits
7 Commits
ea0f583644
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
08a93a5418 | ||
|
|
ceff063ed0 | ||
|
|
3a843c6b3e | ||
|
|
2aff679fea | ||
|
|
519710cc7c | ||
|
|
20e91ce3dd | ||
|
|
acaefd1155 |
100
day15/input.txt
Normal file
100
day15/input.txt
Normal file
@@ -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
|
||||
192
day15/part1.jl
Executable file
192
day15/part1.jl
Executable file
@@ -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])
|
||||
240
day15/part2.jl
Executable file
240
day15/part2.jl
Executable file
@@ -0,0 +1,240 @@
|
||||
#!/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 relax(nrows, ncols, R, costs, row, col; first=false)
|
||||
c1 = costs[row, col]
|
||||
c = c1
|
||||
if col > 1
|
||||
c = min(c, costs[row, col-1] + R[row, col])
|
||||
end
|
||||
if col < ncols
|
||||
c = min(c, costs[row, col+1] + R[row, col])
|
||||
end
|
||||
if row > 1
|
||||
c = min(c, costs[row-1, col] + R[row, col])
|
||||
end
|
||||
if row < nrows
|
||||
c = min(c, costs[row+1, col] + R[row, col])
|
||||
end
|
||||
|
||||
if c < c1
|
||||
costs[row, col] = c
|
||||
return true
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
function relax_all(nrows, ncols, R, costs)
|
||||
relaxed = false
|
||||
for row in 1:nrows
|
||||
for col in 1:ncols
|
||||
v_relaxed = relax(nrows, ncols, R, costs, row, col)
|
||||
relaxed = relaxed || v_relaxed
|
||||
end
|
||||
end
|
||||
return relaxed
|
||||
end
|
||||
|
||||
function get_costs(R)
|
||||
costs = zeros(Int, size(R))
|
||||
dirs = zeros(Int, size(R))
|
||||
nrows, ncols = size(R)
|
||||
|
||||
# first pass, unconditional update looking only right/down. Avoids
|
||||
# having to add an infinity value
|
||||
costs[1,1] = 0
|
||||
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
|
||||
|
||||
edges = ( (nrows-2)*(ncols-2)*4
|
||||
+(nrows+ncols-4)*3
|
||||
+4*2 )
|
||||
max_passes = (nrows*ncols - 1) * edges
|
||||
|
||||
i = 1
|
||||
while i <= max_passes
|
||||
relaxed = relax_all(nrows, ncols, R, costs)
|
||||
if !relaxed
|
||||
break
|
||||
end
|
||||
i += 1
|
||||
end
|
||||
println("passes ", i)
|
||||
|
||||
return costs, dirs
|
||||
end
|
||||
|
||||
@time costs, dirs = get_costs(risk_map)
|
||||
|
||||
if length(costs) < 1000
|
||||
display(costs)
|
||||
println()
|
||||
end
|
||||
|
||||
println("cost = ", costs[end, end])
|
||||
10
day15/test.txt
Normal file
10
day15/test.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
1163751742
|
||||
1381373672
|
||||
2136511328
|
||||
3694931569
|
||||
7463417111
|
||||
1319128137
|
||||
1359912421
|
||||
3125421639
|
||||
1293138521
|
||||
2311944581
|
||||
50
day15/test2.txt
Normal file
50
day15/test2.txt
Normal file
@@ -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
|
||||
4
day15/test_snake.txt
Normal file
4
day15/test_snake.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
19111
|
||||
19191
|
||||
11191
|
||||
99991
|
||||
5
day15/test_snake2.txt
Normal file
5
day15/test_snake2.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
11199
|
||||
99199
|
||||
11199
|
||||
19999
|
||||
11111
|
||||
110
day7.jl
Normal file
110
day7.jl
Normal file
@@ -0,0 +1,110 @@
|
||||
#!/usr/bin/env julia
|
||||
|
||||
#=
|
||||
--- Day 7: The Treachery of Whales ---
|
||||
|
||||
A giant whale has decided your submarine is its next meal, and it's much faster than you are. There's nowhere to run!
|
||||
|
||||
Suddenly, a swarm of crabs (each in its own tiny submarine - it's too deep for them otherwise) zooms in to rescue you! They seem to be preparing to blast a hole in the ocean floor; sensors indicate a massive underground cave system just beyond where they're aiming!
|
||||
|
||||
The crab submarines all need to be aligned before they'll have enough power to blast a large enough hole for your submarine to get through. However, it doesn't look like they'll be aligned before the whale catches you! Maybe you can help?
|
||||
|
||||
There's one major catch - crab submarines can only move horizontally.
|
||||
|
||||
You quickly make a list of the horizontal position of each crab (your puzzle input). Crab submarines have limited fuel, so you need to find a way to make all of their horizontal positions match while requiring them to spend as little fuel as possible.
|
||||
|
||||
For example, consider the following horizontal positions:
|
||||
|
||||
16,1,2,0,4,2,7,1,2,14
|
||||
|
||||
This means there's a crab with horizontal position 16, a crab with horizontal position 1, and so on.
|
||||
|
||||
Each change of 1 step in horizontal position of a single crab costs 1 fuel. You could choose any horizontal position to align them all on, but the one that costs the least fuel is horizontal position 2:
|
||||
|
||||
Move from 16 to 2: 14 fuel
|
||||
Move from 1 to 2: 1 fuel
|
||||
Move from 2 to 2: 0 fuel
|
||||
Move from 0 to 2: 2 fuel
|
||||
Move from 4 to 2: 2 fuel
|
||||
Move from 2 to 2: 0 fuel
|
||||
Move from 7 to 2: 5 fuel
|
||||
Move from 1 to 2: 1 fuel
|
||||
Move from 2 to 2: 0 fuel
|
||||
Move from 14 to 2: 12 fuel
|
||||
|
||||
This costs a total of 37 fuel. This is the cheapest possible outcome; more expensive outcomes include aligning at position 1 (41 fuel), position 3 (39 fuel), or position 10 (71 fuel).
|
||||
|
||||
Determine the horizontal position that the crabs can align to using the least fuel possible. How much fuel must they spend to align to that position?
|
||||
|
||||
--- Part Two ---
|
||||
|
||||
The crabs don't seem interested in your proposed solution. Perhaps you misunderstand crab engineering?
|
||||
|
||||
As it turns out, crab submarine engines don't burn fuel at a constant rate. Instead, each change of 1 step in horizontal position costs 1 more unit of fuel than the last: the first step costs 1, the second step costs 2, the third step costs 3, and so on.
|
||||
|
||||
As each crab moves, moving further becomes more expensive. This changes the best horizontal position to align them all on; in the example above, this becomes 5:
|
||||
|
||||
Move from 16 to 5: 66 fuel
|
||||
Move from 1 to 5: 10 fuel
|
||||
Move from 2 to 5: 6 fuel
|
||||
Move from 0 to 5: 15 fuel
|
||||
Move from 4 to 5: 1 fuel
|
||||
Move from 2 to 5: 6 fuel
|
||||
Move from 7 to 5: 3 fuel
|
||||
Move from 1 to 5: 10 fuel
|
||||
Move from 2 to 5: 6 fuel
|
||||
Move from 14 to 5: 45 fuel
|
||||
|
||||
This costs a total of 168 fuel. This is the new cheapest possible outcome; the old alignment position (2) now costs 206 fuel instead.
|
||||
|
||||
Determine the horizontal position that the crabs can align to using the least fuel possible so they can make you an escape route! How much fuel must they spend to align to that position?
|
||||
=#
|
||||
|
||||
using DelimitedFiles
|
||||
using Statistics
|
||||
|
||||
infile = length(ARGS) > 0 ? ARGS[1] : "day7input.txt"
|
||||
println("infile = ", infile)
|
||||
|
||||
hpos = vec(readdlm(infile, ',', Int))
|
||||
|
||||
display(hpos)
|
||||
println()
|
||||
|
||||
mean_hpos = mean(hpos)
|
||||
println(mean_hpos)
|
||||
|
||||
center = round(Int, mean_hpos)
|
||||
|
||||
function fuel_cost(crab_pos, align_pos)
|
||||
return sum(abs.(crab_pos .- align_pos))
|
||||
end
|
||||
|
||||
best = [-1, -1]
|
||||
for i in minimum(hpos):maximum(hpos)
|
||||
cost = fuel_cost(hpos, i)
|
||||
if best[1] == -1 || cost < best[1]
|
||||
best[1] = cost
|
||||
best[2] = i
|
||||
end
|
||||
end
|
||||
|
||||
println("center ", best[2])
|
||||
println("cost ", best[1])
|
||||
|
||||
function fuel_cost2(crab_pos, align_pos)
|
||||
diffs = abs.(crab_pos .- align_pos)
|
||||
return sum(diffs .* (diffs .+ 1) ./ 2)
|
||||
end
|
||||
|
||||
best = [-1, -1]
|
||||
for i in minimum(hpos):maximum(hpos)
|
||||
cost = fuel_cost2(hpos, i)
|
||||
if best[1] == -1 || cost < best[1]
|
||||
best[1] = cost
|
||||
best[2] = i
|
||||
end
|
||||
end
|
||||
|
||||
println("center ", best[2])
|
||||
println("cost ", best[1])
|
||||
1
day7input.txt
Normal file
1
day7input.txt
Normal file
@@ -0,0 +1 @@
|
||||
1101,1,29,67,1102,0,1,65,1008,65,35,66,1005,66,28,1,67,65,20,4,0,1001,65,1,65,1106,0,8,99,35,67,101,99,105,32,110,39,101,115,116,32,112,97,115,32,117,110,101,32,105,110,116,99,111,100,101,32,112,114,111,103,114,97,109,10,1425,266,740,842,335,1076,1125,108,728,131,553,757,316,361,475,1058,555,157,37,1501,287,61,22,394,886,535,235,734,1381,428,200,838,84,0,99,397,516,1260,1079,457,685,669,85,1161,851,1413,207,125,23,396,1024,637,712,942,320,507,32,686,1073,449,736,619,120,1092,674,769,519,26,42,366,187,261,389,583,170,700,695,531,57,263,1058,755,1215,413,201,617,311,443,694,285,677,722,1262,934,790,31,272,410,129,22,186,49,1040,399,19,624,132,1,35,515,423,1039,128,963,254,152,1306,33,360,484,463,483,254,741,284,14,155,6,16,1053,36,1299,637,985,470,476,383,717,304,31,209,263,70,1196,2,283,470,45,20,226,249,654,692,107,31,123,131,42,36,469,249,74,703,798,195,126,1699,135,143,1028,180,33,248,4,118,22,783,721,1033,1250,779,213,241,170,1026,0,124,709,672,349,286,494,134,361,938,985,539,267,240,951,496,431,449,242,804,422,24,202,76,947,414,396,681,142,366,342,256,978,373,677,1471,187,307,579,437,17,779,81,1380,241,69,61,758,1290,98,514,275,510,1427,185,139,816,1401,105,74,978,544,248,413,0,45,1107,223,332,723,745,71,70,330,727,261,1223,914,16,980,306,331,1011,132,70,1735,281,993,976,1,370,280,502,41,644,213,1191,518,464,693,446,44,930,1,23,1412,219,722,1028,84,552,1261,601,433,538,728,385,9,346,212,1017,7,80,88,336,480,1264,219,750,0,1080,711,1095,849,1270,175,20,314,452,620,1283,81,57,193,392,79,1330,220,396,184,922,921,902,199,56,107,32,67,275,91,202,49,4,312,372,262,49,172,493,1473,989,70,373,941,1116,798,709,865,105,442,555,1616,74,402,703,439,120,262,442,1704,1459,195,237,1763,376,734,28,867,370,6,1080,548,750,391,367,123,324,221,453,131,516,586,72,57,185,1667,468,439,225,1407,663,12,355,1320,595,60,59,158,279,365,670,505,14,240,1299,337,128,615,823,576,823,890,284,1196,717,955,1282,1002,20,176,32,222,33,248,634,885,703,543,368,585,1151,110,124,41,475,958,252,99,30,620,793,1021,540,154,635,1194,420,54,33,452,797,157,576,86,116,842,94,98,0,1162,38,483,138,949,316,1248,79,249,40,234,698,275,1239,573,649,815,348,48,78,1039,276,12,261,317,638,304,20,184,1152,711,1673,917,40,244,655,268,151,41,851,79,242,788,611,300,27,141,635,274,330,900,1023,498,269,267,46,436,844,1228,38,142,467,192,399,86,87,645,792,405,844,108,487,356,1251,332,146,128,383,1123,145,0,1148,688,127,316,579,15,215,293,73,1648,599,432,155,317,1054,205,155,451,1411,291,104,536,719,35,25,24,62,747,702,224,971,107,1210,114,41,472,29,286,4,920,0,197,135,112,308,191,1017,438,206,239,6,11,69,945,248,274,397,50,173,80,1349,268,585,590,1071,1127,351,929,106,989,396,209,691,17,149,1001,354,1296,473,179,152,141,1049,376,590,196,27,656,67,275,153,916,849,27,1093,73,156,30,1206,276,623,395,38,760,33,222,371,489,246,309,385,498,517,748,1384,1203,465,360,237,763,1173,94,431,48,770,491,132,564,84,472,1804,57,59,187,351,1340,265,1099,36,199,60,608,148,1209,1142,231,268,254,105,1020,200,1202,661,225,1313,55,808,770,80,522,185,129,36,476,815,1424,534,583,285,15,21,607,722,242,33,299,672,1253,1078,142,285,417,461,261,310,296,1934,271,144,1572,155,1039,881,1097,18,226,45,789,213,309,32,603,1102,5,81,511,672,314,7,1471,104,196,875,286,4,198,472,549,613,1453,139,596,270,164,417,709,437,27,86,758,1365,216,38,1047,124,96,255,72,67,1372,143,120,502,276,922,89,231,491,1330,245,473,25,944,266,1475,569,215,484,73,264,214,608,423,333,879,251,300,32,18,514,135,1349,80,493,569,784,2,794,846,596,30,862,318,207,546,551,1548,547,181,1219,354,650,791,53,20,629,52,105,98,312,140,111,1451,973,11,17,821,724,1836,376,82,248,86,730,1061,47,309,142,1039,114,157,26,307,1058,803,723,105,170,59,239,181,601,79,564,671,636,1465,530,533,75,261,1522,537,96,984,71,504,572,923,85,103,567,780,102,4,835,463,684,427,1091,1104,1163,626,1015,395,1881,43,490,906,1013,398,113,95,332,215,14,8,85,92,1579
|
||||
1
day7test.txt
Normal file
1
day7test.txt
Normal file
@@ -0,0 +1 @@
|
||||
16,1,2,0,4,2,7,1,2,14
|
||||
200
day8/input.txt
Normal file
200
day8/input.txt
Normal file
@@ -0,0 +1,200 @@
|
||||
fgcae ebafc cabdef eg abecfg abgfed feg gafdc bceg ebgcadf | defagbc faecg cfdag gecb
|
||||
eagd cad fgadbc aefdcg dcebfg fcegd cbeaf ad dbgfeca defca | cfdeg gdcabf fcgde afgced
|
||||
gfdeca aeb eb fbdag eafdc adfbe cefdab bdaegcf efbc ecadgb | eb dbafe eab faecd
|
||||
facbdge efdg gcafd daegc caegb aecfdb ade ed gafbdc dgfeac | de abcdef faedcg dfgca
|
||||
dcaegbf bc dcgeab cbgad cebg cdfaeb fbadg acdge cefdga cdb | gdcfae gbec gdace cgadfe
|
||||
ceagfbd fbaeg fadge fbegad abgecf dafcg ed edab ged dfcbeg | gebfa de gbafec ed
|
||||
acedbf adfbg cgafedb geac cgb befcgd bcgaf bfgace cg bfeac | bcdfeg fecba cafgeb bgdfa
|
||||
egadc cea ac agbcedf cgab bgcade fbcaed eadfbg fegdc ebdag | fdecabg fcged cdbage efgdab
|
||||
cbfge gdc cdea efdag dc cdfeg gfbcad gcdeaf cdeagbf egfadb | dfcge egcfd fcdbag dc
|
||||
gaedb fbced gcdbef cge gc decgb dcbgfea cgfb cgdfea fbcade | fdegbac fecbd ecg degba
|
||||
geaf bcfedg agbcd bfeagd dgfbe cbdaef beacdfg abe agebd ea | fbecdg geaf gebfad adgbc
|
||||
bed afgebd adecf bafgedc dbca cfegb becdf fabdce bd gcfdea | gfdaeb ebfgad dbefc cbda
|
||||
gacfebd ag bedfca agbfde egfcab dfga degab fbade bag bdecg | dgaf abcfed agb bfecag
|
||||
afebg afdeg gcbe acebdf cfbage cafeb gab decgabf bg gcfdba | gb caebf abgef acfebd
|
||||
efdab fabdgce gcbfa eg gacbfe beg ecag beafg dfabcg bgecfd | bge fgdcbe gcfdba ebdaf
|
||||
aecbd bdagef gd bfega aefgcbd gfde gfdcab dbg edgba febagc | dg dagfbc dgfe eabdgf
|
||||
gdfba egfacbd gdcefa baf gcba fdagcb dgfac ab dcfeba ebgdf | dcfag bfa gafcd cbag
|
||||
afbe bdagc cedfb defcab ebfdgac gfedbc cbfad af adgcfe adf | dafbc dfbac fdcegb gadcb
|
||||
fcgedb ca fdcage cagdbef ceab acedfb fdbce cafdb cda afgbd | ac dcafb efgdac ebcfda
|
||||
fbad gbcda dcefgb cfeadg dgbcaf bcafdeg ad gcabe adc dfbcg | gbcdef bcfdg cgbfd bfadgc
|
||||
afcgeb bedag bdecafg agb bedfga fdagce bg bgfd dfgea cdabe | badge cagdfe efdag afegbd
|
||||
gfed dcaef ge bafdce daceg acebfg cefadg egc ecdabfg gacdb | bcgad bcegaf agecd degcafb
|
||||
deacb adcge ab acb fbdce faedbgc fgadec gabd deabgc ecbfag | dfbec ab cbagfe bcfgae
|
||||
gcabfde ecf cbdeg bacgf fedg ecfgb gfedcb fcaedb cebdag fe | dbcegf bcgdfe gfde defg
|
||||
daefb bfg gedafb fabge dbafec cfega bdga cgbdfe gb gbacedf | aecfg bcedaf begdcf dgba
|
||||
bagce bf bdacfeg efbg fcb cefab gbfadc cafebg ecdfa bacdge | fegb cegba bagce efbg
|
||||
becag cfeg cefgbad fbace gfeacb acdegb bef ef adbcf fdabge | ef bdfac cbegad gbeac
|
||||
egb afcbe fbaeg eabgfdc gcab gb gfdea fgbecd bcdafe gafbec | fcdgeb edcgbfa gbe fgeba
|
||||
dga caebd ecdag cegfd abdfce dafbge ecbgda bcdgefa ga agbc | afdbec ecfdab gdaec gad
|
||||
fc gbdefc cfe ebdaf fcbeda gbafde edacg bfcdgae dacef cabf | decgfba cdbefg cf ecdabf
|
||||
febgd dafgbc caed cgd ceadgf fbecga dcgfe aegfc gbedfca cd | dgfeb fgced dcafebg agdfce
|
||||
ecdafgb bgade bgd fabd gbaef bd egadbf ceadg cfbgea dgbfec | edagc bdage bfda dbgae
|
||||
cgeba aefgcd dfceagb dc ced facbed cdfb dbaec adfeb fgedba | fbdea bgcae efbda fcdeag
|
||||
caegbf dcfga afcedgb afgdcb fcedag ab edbcg dbfa gdcab abg | bag cbdag agb ab
|
||||
gadfecb befc fgdbea cf fedagc fac bcgfa agbfe adgcb feagcb | gbcda cgbad bcgfa cf
|
||||
dae aefcb dgabec dacfg afebdc ebfd afecgb aedcf fbcagde ed | ecabfgd bedf bcadef egdacfb
|
||||
abcf fcega fdgce fegbda eafdcgb ac gabecd gbfae beagfc eca | edbgca edbfga cae gface
|
||||
bgedcf afcdbg ag gcbaef fcedgab afgdb cfgbd befda acdg agf | fdbga ceadbfg egfbac fag
|
||||
bad edafc bdeg abcgde ecafgbd gfadcb bgaec db edcba geafcb | cdafe fdcabg cdfageb dbge
|
||||
geb bgcf agfcde gb befag gfbcae defab gabdec eacfg gabecfd | afbedgc gceabf abgef egacf
|
||||
fdeac acg cfegda cbeadf fegdbca ag acefg adge bfdgca gbcfe | gecdaf efgca cgbafd cfgeb
|
||||
begdf bdfgc ebcf adfcg cdegbf dgeafb cbg cb bfeagdc gbdcea | acgedb fagdbe bgfde dfgac
|
||||
afcgdb aedc bedgf egafcd afdge gecaf fcgebda gda da ceabfg | gedbf gacfe adg agdef
|
||||
bdeafg acgebdf dgcfa fa cafe dgfbc gaf edagfc cgabde agedc | fadbge bdgfcea dgcefa cgeda
|
||||
gacfbde defag cbgef gbca efadcb afcbeg cgbdfe ba eba gefab | fegab abe ebfcgd fbgae
|
||||
gfbdec fbcade cabdegf ebdg bfd dcfgb cadgf gebcf db fcabge | eacdgfb bdge ebdg ecafdb
|
||||
egcfba fdcae fcdaeb gcfdae fabd ba cdeab fcdbaeg eba decbg | cabed facbed cbade dfcea
|
||||
agdfe acfdeg fcdbeg dgf gdaec df cadf ceagbfd abgfe gbdaec | bgcdef abfge gbefcd df
|
||||
dgebf afgdb bfa fcbade af dcafgb agcf gcbda dabgecf gadecb | dbcga abcdge fadgbce eagcbd
|
||||
dbcga bea egacdf dageb be efbd abgfce gadfe fcbegad aebfdg | eba be bdage bedga
|
||||
gcbd egbdafc fgc fbgedc gc aedbfg cefgb fedbg cfaegd efabc | bface egfbc bgdacfe gc
|
||||
fdgae dc ced gbcd gbcefd decfba becfg ecfabg cgfde gbcadfe | edbcfg cd cafgbe cdegbf
|
||||
geadcf efg gadef fg fecad acbgfe dagecbf dcgf gadbe afbdce | edafcg bgcfade gfdc abedg
|
||||
edcba ecgdb aedbcf aebf dabfc fdacge badgfc ae ead gfcbade | gcebdaf degbc ebfadc efdcba
|
||||
caedbg dbgec dcbf ecgdbf cgdef dgf bgaedcf egacf afebgd fd | cdfbeg ecfbgda gcbfed cbgedf
|
||||
bdag acdeb ecgdb egbcfa fcdbega gb dbecag cbdafe ebg gdcef | bedgc abdfcge efagcb cdgef
|
||||
bfade bfdga dfbgec cagbfd dfgacbe gba gacf ag gdcbf abgedc | cfag abdcge ebafd abefd
|
||||
agcdef bcfda bdgca aebdfcg eagdc gb dbg gbea cedagb bfgdec | dfabgec eafdgc acdge gdecab
|
||||
ebacdfg cgbfad egdbc bdgfa daef bae gabed febadg ae gacbef | fagdb daebgf fgdab bea
|
||||
dcgbf cbedf degc bgc fcgdbea fbadg cfdgbe gc efacgb fdbace | edfcb cfeabg cg edbacf
|
||||
bcdagfe dfacb cbfdge bgcdf fdgeac acd agbd acbef da dcgfba | cbgdf dgab ad gbdcaf
|
||||
edgfbca gbe be ceba bdaecg gefcd dbagc egbadf dcegb dfgabc | gcfde gfceabd gfbcad eagfdb
|
||||
dfcbg fcdga efgb gecbfd gecbda dfbgaec bedfc dgb bfadce bg | dbcgf bfge dacgf dfgcbe
|
||||
cga ebdgaf bfdag cbgfe bfadgc gbfeadc ac cagfb cdba cgadef | bfdag agedcf aegcbfd gcfab
|
||||
gcead gfdbec cbdge gabfce eb dfgcba ecfdgab befd dgcbf ebg | cdgae fbdegc bcdge gcdfb
|
||||
da ebgfda fcdeagb acgbfe gedba dbegc agdecf dae bafeg bfda | gdafec fegba edfbga aed
|
||||
fbagec gdfba bcg gebcfd cbafg bcdfgea ebac cefag faedcg bc | gfbac baegcfd fabegc dgeacbf
|
||||
dfbeag fabdg fdbe abdeg eb dcega gcefba cfdbag dgcfbae geb | cgbafd bdef dcage eb
|
||||
abdcfe gba dagf cagbefd fdbea bfega ag dafbge dcbeag gfceb | afbcde ecgfb dfbega bdgeca
|
||||
dg dge gdbc cfdae bcgae cbdage abgfde cfedagb cdgae eacgbf | eabgc bgeac bcdg cagbe
|
||||
bfd dgaeb gcabde fd bfgce dgfa dbefg gadbfce dbafge fabcde | edfcba efbcg df bdf
|
||||
gcbfd agbecd fedc edcbfg fdb eadfbg bcfag fd gedcb gbacdef | fgbac bdagfec gfbdaec aegbdc
|
||||
cgbdfe ce acedg adcfeg bcfegad fcea ceg abdeg afbdcg fcgda | ecg aefdcg dacge ec
|
||||
egbdfca efacb agcdbe cbaed bdafgc dfaecg debg bd bda agced | geadbc efbca gabfdc bd
|
||||
edcfb bfgecd ecabfd eca abcd fgeab fedcag abfedgc ac bfeca | bgfae eca cdgfeb bfaec
|
||||
cadef cebdga ebd bacdfe dagfec dbfa fcebg ecbgfad db fdecb | befcd acgdeb abdf dbfa
|
||||
gbfecda bfd cdagb fbdacg fgdbec gbaf cbadf aefdc eadcgb bf | adcgb dfbca agbf dafcb
|
||||
abedg acbgf fd gaefcb cagbdef cdaf dcbfeg bdagf dgcbfa bfd | gbadcef df fadgb gabcdf
|
||||
fbgadc dbcfg dcega cgdfe cfedbg caefgb ef gef gfaebcd fedb | fcedg febd gbcdfa cdgfab
|
||||
bg adgb fcaedbg cgfda fcbae bcg dfacge fgcdba decgfb bcagf | cgafb dbfcage bdgcfe dbga
|
||||
ed afbdg acefb aecbfg cfdabeg gedfcb fcdabe ecda def ebdfa | gbafd gedfcb dfegcb dfgba
|
||||
gcdfae efgdb abdfcg fde gdabf dbcegfa fedgab efab ef ebdcg | eafb gbfda gacedf cbdge
|
||||
edagfc gfaecbd bgced cbegfd cb gefdc badgfc bdc efbc adegb | dgbec cegafd dcbgef dgecb
|
||||
gedcab beg eg cafgb fbegc gdfe bdgfec gabcedf bdafce bcdfe | gcefb afcbed ge gdfe
|
||||
fdbagc cdgefab edacf ceba ea cfbaed ecfdg abfegd adfbc eda | ea beadfc abcgdef ecdgf
|
||||
fbc gfbacd fecda cbdfa dbgf dceabg gbcdfea efbgac cdabg bf | gbdf agbced gaedbc gfdb
|
||||
aegcb bfgecd fbegad fcagdbe dfgce fcbd gdeacf fb bgf bgfce | bgafedc gcdaef dgfcbe eadgcf
|
||||
def dbce agdfeb ed fdgbcea dgcfb aegfc dfgbac gecfd cdfgbe | cdfegb ed cdgfaeb ebdc
|
||||
ecbfad afdb eagbfc caebgdf edfcb dgfec cgdbae bd bcafe bdc | bfacegd fbdcae dbc bgfdcea
|
||||
dfgba eg cfbead gde bfedgc dceab cabfdge gdbae aecg aedgcb | egd adebg gde debcfa
|
||||
cadfb egfb afgbc adbegc ecgadf ecagb fga ecfgab bgacfde fg | afcbg cgfba gf dacbge
|
||||
cegbf dec efcdg facdge dfae dcbafge ed adgfc fdbagc cgaebd | dce gbcfe afcdgbe dcgabf
|
||||
cfabe fa eabgfc cgaf fba gfbce dcfbge edbgcfa efbadg acebd | cfebga bfa fedcagb abfce
|
||||
acdfb dbagf afcebgd beag afgedb gfdea gbd fgdcbe gb cdeagf | dafge gafed gfacde gb
|
||||
gdfeac dfgce ebfadcg edagc dacf baegdc fd gdf egcfb dabgef | degfbac cfdeg daebgf cdeagb
|
||||
fcgabe ebcd ecfdg dabcgf gfecabd cfd ebcfg dc bcfegd adefg | agedf cd bcefg dc
|
||||
cbaeg bcfea fb feb gfaceb gdfbec dagbec gfab deacf bgcaedf | acbfge bf afgb efb
|
||||
egdba cadgfbe efadbg cg begc bfgdca caegd abcegd dcg acdfe | fbegcad bceg gcd cdgebaf
|
||||
agbcd dafcgb cfdg ceadb fdgeba eafbcg gfdab gbc bgdcafe cg | gfadcbe fbagd decba gc
|
||||
gfbec adgc dge egdcfa abgfde abgdfce dg eacfdb fdaec gdfce | fecgd gd fgcbe acdef
|
||||
fdbceg bfedc af fegcad cbdag daf efab dabcef fbeadgc cbfad | fcabd cbfad gcfdbe gadbc
|
||||
ebgac fabdeg bedca cg aegdcfb geacbf ceg efgba facg fbedcg | gecdfb aebfg fagc ceg
|
||||
eagb dcgeb ba abd cbeagd cdgafb fecbdga fgedcb dacfe dacbe | bdfeacg cadgbf bgedfc fcdae
|
||||
dbac cfgae cbfgde ba bga gcfbad dcfebga gcdbf feagbd cgbaf | cgafb fagbdc dcfbg afgebd
|
||||
adbgfe fgb egdafc cbafd dfegbc edgafbc egfcd cegb gfcdb gb | bg fdacge efcadg egdfba
|
||||
agfbe cgaefdb gbdf edagb afg cdgafe gfedab fg egcdba fabec | fcbdega fag agf agf
|
||||
cae edabfc cdfega ca cfba gadeb fdecb ecagfbd cbgdef acdeb | facdeb fgecad bfadce efcgbad
|
||||
facb dbfec dceabf efgcd cb aecfbgd dgfbae eafdb ebdagc bcd | bfedc cdegab cbdfe adefb
|
||||
fecbga badgc dga bfgadec dg geacb cgedba dfebag gcde fcabd | decg gda baceg gd
|
||||
gecbd gcbdfea cbfg bg bge egbcdf dcbef beafdc cdeag bfgeda | agcde dceag gfabde beg
|
||||
gefba gacfbe facdeg cg cgab gfc efbdcag cfgeb cefbd afegbd | gfc afbge dafegb aebcgf
|
||||
egd aebfgd gefc ge gedbfca bdcfe dfcegb gecdb edbacf cdbag | dacbg edcfbg bcdfe ge
|
||||
df ebdga gcdefa cfbd bgdfa afgcb dfeagbc bfacgd fgd facebg | gaebd gecbaf fdcb dfbag
|
||||
fag cegfdab cadebg dabcgf abefg eafc ecafgb ebacg fbdge fa | ebfag dbcgfae gbecad fdbgac
|
||||
gecbd gefa gfc fabdcg bfaecd ecgafbd fg befgc efabc gecbfa | gfae gcf egbdafc edgbc
|
||||
bfgae fcebag fdbe fd fedag adgfbc fdbgea ecagd agcbfed fad | fgabe dfeb ebfd gefba
|
||||
ecdgfb afge bdage ebcda eg abdgef fgadb gabcfd dge bcdagef | gdebfa eg agfbdec afeg
|
||||
dbc acefd cegadbf fbcde afcb fdcaeg bc acdbge febdac debfg | ceafd caefd gfbaced dcb
|
||||
fecgb fgacd gdcefba de gbafce febd gecfd edc fecdbg ecbgda | becdfg ed becgad gcdeba
|
||||
agfdbe fbac bcd edacfb bgecfd cb cdeba cfgdeab acged edfab | cb dbc adbce bdfae
|
||||
dgacef bdfa ecgbf ab fegba dgafe efdbgca abg acebdg afbdge | gefda ecgafd ecfbg begcad
|
||||
gbfcea bgfa afdebc aegdcb af bgcdfea bgeac gecdf afc fgace | bfgecad fca afc abfg
|
||||
cfe dgcfe fdagcb gebdc egfa gbcdefa dcebfa fe agdfc fgaedc | dfgac fe fce fcdbega
|
||||
fecgab gcbdef cbaefgd dfgbe dgcfe eb bfe dbfga edbc acfegd | fbdeg fdgeca agefbc egcfd
|
||||
aecd bac dfacbe cafdb ac gefcab gfcdbe fcbde gbdaf cbdegaf | gcdbefa fbgcae ecad fagecb
|
||||
eagbf cagedf geafdbc fdgbce adefb aecfd adcb deb faecbd bd | ebd deabf fadcge cdagfe
|
||||
gadefcb bfcga eadcgb egdbfc deag ecg edabc acedbf eg abgce | gdea bagce gce gce
|
||||
agcfe bec gecab gbeda facb dbfgec fcegab gfacde gfdaceb cb | cb cefdgb cadefgb bc
|
||||
fdgb df egabcf fegda cedga fegab efdbac def cgdefba gafedb | facdbe bafceg agedf fdebga
|
||||
fdaecg dbcafg abfgec dc caefg cedg aecfd abdgfce eadbf fcd | cgefab gbfacd fbegac fabecg
|
||||
dfecabg bcfeag gdafce dagbfc cdefb dca gafbc dbag da bcadf | dca bagfce cbfda cdagbf
|
||||
dabfgec febadg gbcdae gfbe adebf ef cabdf edfgca ebgda eaf | gfacbed abgced egdfca egfb
|
||||
cfdea dc fdagebc fegcbd agdc gcfade fcabe cfd efdgba gfead | bdfcge agdc dgefa cfd
|
||||
gbfca febadcg bc bdegcf beca begaf gabedf cgfbae cgfad cbg | bcg dcaefgb dfgceb afebdg
|
||||
cg cgbf bedcg fcagde adefbcg efdgcb ebacd cgd edbfg egdafb | cfgedb bgcf acegdf cfgb
|
||||
bfad adcgbe faceg fagbdc df afdgc efcbdag dfbegc gfd badcg | gfd abgcd fgd abfd
|
||||
bcadgf cgdfb adgcb gf gafc dcfeabg bdagce dgf dfbega cebdf | adcbfg gfca fdcbe gfac
|
||||
daebf fcabgde bgdcef gab ga edbcga afcg ebfga aefbgc egbcf | ebcfg ebcgfa fcgebd agcf
|
||||
gedab fg afgecb cfgd feg cfabed befdc fgdcbe gdceafb befgd | ecafbg ecfbd febdc gf
|
||||
gcbadf ce gfcdb fec feabd gbecdf dbcfage febdc agcdfe egbc | bafed gcbe gefcbd egcb
|
||||
cbdfg gbcafd feb gaecf gcbfe gfbdae be fcgabde bdgcef becd | abgedf gdfebc aedgfb fbe
|
||||
edacgbf fbdae edgac bg gab bagdec dagfce fadbgc cebg bgdea | aedgb bg cebg adcge
|
||||
bda gbfea gedcab gfbceda bgdfa bfcdg febcdg bagcfd cfad da | dbecag bgfdc gdbfa facd
|
||||
ceabd dabfe acgde bc fbca cgbfde febgda dfecagb ebafdc bdc | cbdae dbfea fgdceb gbfedc
|
||||
acg fadbgce cfgdeb dbgce efdca ag dbga cagbef egacd cdgbea | gdecabf gcbfea ecbdg gac
|
||||
dfgacb ged egfdab efcgbda gcefbd debga ebadc ge aegf gfdab | cdfgab cdafgb bfcgda ge
|
||||
gdbeca bcfde eafg cefbgad dgafcb ge degfb adefbg dgfba bge | bdfeg begdf ebfadg eabdfgc
|
||||
cfbdgae gbaec bagfcd gfcba dcaeg bacfeg dbeacf bec efgb eb | ceb egcab cagfb dgcae
|
||||
fdcagb gdaecb fcadg bcdgaef gd dfcea gbcaf aecgfb cgd dgbf | gfadc gfdb badgfc gbfca
|
||||
bcafed defgcab fgcb cfbae cagde agebfc gbcae gbe gadefb gb | bg gfcaeb bg aedgc
|
||||
bcfadeg gbfd eacfbd gdafe afebg bafegd geafcd gebca fb bfa | agdefc eafbdg fgead gedaf
|
||||
fcgeabd dgeaf ebgfad gceafb geadc ac aec ebcdg cafd adgefc | gbfadec egcbd acfd dgabef
|
||||
eac fbdgec ea eadb cbaeg agfdecb eafdgc dcgbe bcfga adcbeg | aec abde eac ea
|
||||
cedb gcadbe ed edgfbac fbega abdge gacdb fdacgb dcefag aed | cdagb gbcafd gdefac gedafc
|
||||
cae agfbec ae begcd feab fadgbc ecfadg ecabg cdgbefa cabfg | ecgba afcgb efba dgeafc
|
||||
fecgabd badf efadg abedfg fea fdgceb fedgb aebfgc af gdcae | ebfgca fae fea af
|
||||
bgedac gbfae dcbefg acbf fb gaedf cagedbf aebgc gebfca fgb | aedgf fdgea bgfae bfdgeac
|
||||
bagcfe eadcb gb facegdb bgc fdcag efbcad beadgc bgadc ebgd | gbc fbcage becad gbfcae
|
||||
cagde gbca dcgbea gc efcad dbgfea dfgecb caedbfg adgbe cdg | gc egdfbc bcga cg
|
||||
efgcbd aebgcd dfe eadfb abged abdcefg bacfd ef bdfaeg faeg | fgae cgdaeb adefb afcbd
|
||||
cgbfea gcbda gefdcb eadf febag bdf fd febgad fadbegc bgadf | gebcfd fgdbea dfb dbf
|
||||
dg cdgfe gecfad dfbce bgefda fgd eafcg efcbag dgac bcdagfe | afdebg gfd aecgf gfcea
|
||||
edfga cgefda gcdbe fbae ab egdabf cfgbda deabg bad adefcgb | ebadg agdeb fbae gbdae
|
||||
gbdcfa gbade ebgac ecbfa adfgbec badecf bgc gcef fbgace gc | gc fcge gacefdb ebgca
|
||||
acefgd ebgaf egfda egcd bgfaecd dgcfa ed abcefd efd abdgcf | def ebgaf fed befcda
|
||||
fecag cag cfagbd agfcbde fabec eabg fdcge cgbfea fadebc ag | cfgbda dfgbeca efdcg dfgcaeb
|
||||
bgecfda ecdag dgfcbe bdgac adbgf afgdbe bfac gdcabf cbd bc | bc acbgefd fagcdb fcba
|
||||
ba efacg cfbgde cfdbg bacgf bgfdea abf bcad cfbeadg bgcfad | bcdgef fdgaeb ab cagfe
|
||||
cbdaf bafdgc eacbd fd dcf fcabg bcdfeg bedgcfa dgfa agbecf | dfga bgfac df agdf
|
||||
edcabf cadbf bgfdcea bgcfad bgcde dfg fg bdfgc cgadfe abfg | adfecg gfbcd acgdfeb cbgdaf
|
||||
badgfc dg bagcde bceda dbcfea beafg debag adg fcbadeg edgc | gebda dcagbf dceba egfab
|
||||
edcf dacfbg dbcefag fbd df bdegfc ebdagc bgfea begcd bfedg | dgebc cbgde dgfbac bdf
|
||||
bdcfg acegfb acebfd fdcag egadcf da daf cgeaf edag bafcegd | gdcaf fgaec bafdec fdaecb
|
||||
cagefd fde gdfba fcgaedb abefd ef dafbeg ebfg afcbgd ebdca | egdbfa bceda dfabg bdace
|
||||
fcgb gcdae bg gfadeb cbfegd fcabed bdg gcdbe fdcbe adbefcg | dgb gfbdec befdc gfbc
|
||||
fabde bgfde bgecda cedgb gf cabfdeg ebfdgc fgbeac fcdg gfb | fgbde efdgb edgbc afedb
|
||||
ebacf cedfab fde eabfd fd baecgf bagde agdebfc dfcb egfadc | ebfdgac acdegf abdefc ebfad
|
||||
gdcb cd cfgae cgbedf fbegd efgdc fadcbe gfeacdb dce dfagbe | gbcd cde gacfe gefdbc
|
||||
geb afedgb bfegca bgade eg dgef gadbc eafdb afebdcg fcbead | cagbd defcba dbafec geafcb
|
||||
fgbdc gbfade gba gefa dbafg cbadge bfgecda ga edbafc dfabe | afbdeg dgfba bgdfa dcfgb
|
||||
agef badce ef febad cabfdg gdfab cbgeafd egfbdc defgba fbe | abefd geaf gafdbe ebcad
|
||||
gd gad bfdg gecba fedabcg fcgeda aedfbc adebf agbdfe dgeab | ebcdaf agd gcfbdae egacb
|
||||
cedbfa ba cgbfa abf dfegac cafeg gabe befgca ebdfcag cbfdg | fab aegcf faecbd fdcebga
|
||||
daefg febdga aegfdbc efgdca eafb bge eb agbdec febgd gbdfc | gbcfd edbfg egb bacegd
|
||||
fbdecg bdafec cfaeb gedac def dceaf abfd fd gfecba gfdaecb | ebfac bdfa gbcdef fd
|
||||
fcbga cbdfeag bafgce fdacg dc fcd fcgbad fadge efcdgb cdab | gaefcb bafegcd acdb abcgf
|
||||
ecgabd acgdf abge ae bcfead fgdaceb ead daegc bcdefg gcbed | egba gaecd cgadbe gdbce
|
||||
aegfbc gf afebdc agebdfc ebcdf feg dcefg bdfg dcgea bgedcf | edbfcg fdgb bdgafce febgcd
|
||||
dcg bcdefa dbfceg fdeacg acbeg bfeagcd cfdae dg dfag agdec | dfcgea dg egdca edagc
|
||||
agcfeb fagbed fdbceg bag gebfd afgd agbfcde acbed ag badeg | cbdea fcedgb gfebac abcegf
|
||||
adbegfc fgdace cadge afcg ebdfca ebgad eac bfgdce cgefd ca | acged fcedg ebagd dgacefb
|
||||
gfeadb gfeab bcfa agebcfd aebgfc fecgbd efc fc gadec egcfa | fgbace fc debgcf afgceb
|
||||
bacg ab cfgbd bfadge acfed fgdecb agedcbf fcabgd bfa dbafc | gfdcb cbga abcg afecbdg
|
||||
ge beagc cfgabd bgaced bgcda fbadgec age ecdagf egbd fcbae | aeg gedbac cadfeg fbgcda
|
||||
dgbafe abfdgec gdf fd agebcf gadec dfcgba befd edfga aebfg | abcgefd dagec dfg df
|
||||
edfcga bcegd baeg cbgdae agdbc gce acgdebf eg gfadbc cefbd | dcbeg bgedc cedbg edcbg
|
||||
adfbe dg cagef dfgb gdbeca aefdg gda edcbaf fdgeba gdecbfa | cdabef eafbd bafcegd dg
|
||||
cd edcbfg dbage fcaebdg facd bgcfea afbedc afbec dbc adecb | adegb bcefag abegd cbgedf
|
||||
gdcbf da ecgbfda adc adgb bagcfd dfbcea egfac dfbceg gdacf | gdba cfdbg adbfec fcaeg
|
||||
bcg bc gdcbae dbca abfgde gdeba ebfacdg egcba faceg dgfbce | geadb badecg ecbfgd baecg
|
||||
92
day8/part1.jl
Executable file
92
day8/part1.jl
Executable file
@@ -0,0 +1,92 @@
|
||||
#!/usr/bin/env julia
|
||||
|
||||
#=
|
||||
--- Day 8: Seven Segment Search ---
|
||||
|
||||
You barely reach the safety of the cave when the whale smashes into the cave mouth, collapsing it. Sensors indicate another exit to this cave at a much greater depth, so you have no choice but to press on.
|
||||
|
||||
As your submarine slowly makes its way through the cave system, you notice that the four-digit seven-segment displays in your submarine are malfunctioning; they must have been damaged during the escape. You'll be in a lot of trouble without them, so you'd better figure out what's wrong.
|
||||
|
||||
Each digit of a seven-segment display is rendered by turning on or off any of seven segments named a through g:
|
||||
|
||||
0: 1: 2: 3: 4:
|
||||
aaaa .... aaaa aaaa ....
|
||||
b c . c . c . c b c
|
||||
b c . c . c . c b c
|
||||
.... .... dddd dddd dddd
|
||||
e f . f e . . f . f
|
||||
e f . f e . . f . f
|
||||
gggg .... gggg gggg ....
|
||||
|
||||
5: 6: 7: 8: 9:
|
||||
aaaa aaaa aaaa aaaa aaaa
|
||||
b . b . . c b c b c
|
||||
b . b . . c b c b c
|
||||
dddd dddd .... dddd dddd
|
||||
. f e f . f e f . f
|
||||
. f e f . f e f . f
|
||||
gggg gggg .... gggg gggg
|
||||
|
||||
So, to render a 1, only segments c and f would be turned on; the rest would be off. To render a 7, only segments a, c, and f would be turned on.
|
||||
|
||||
The problem is that the signals which control the segments have been mixed up on each display. The submarine is still trying to display numbers by producing output on signal wires a through g, but those wires are connected to segments randomly. Worse, the wire/segment connections are mixed up separately for each four-digit display! (All of the digits within a display use the same connections, though.)
|
||||
|
||||
So, you might know that only signal wires b and g are turned on, but that doesn't mean segments b and g are turned on: the only digit that uses two segments is 1, so it must mean segments c and f are meant to be on. With just that information, you still can't tell which wire (b/g) goes to which segment (c/f). For that, you'll need to collect more information.
|
||||
|
||||
For each display, you watch the changing signals for a while, make a note of all ten unique signal patterns you see, and then write down a single four digit output value (your puzzle input). Using the signal patterns, you should be able to work out which pattern corresponds to which digit.
|
||||
|
||||
For example, here is what you might see in a single entry in your notes:
|
||||
|
||||
acedgfb cdfbe gcdfa fbcad dab cefabd cdfgeb eafb cagedb ab |
|
||||
cdfeb fcadb cdfeb cdbaf
|
||||
|
||||
(The entry is wrapped here to two lines so it fits; in your notes, it will all be on a single line.)
|
||||
|
||||
Each entry consists of ten unique signal patterns, a | delimiter, and finally the four digit output value. Within an entry, the same wire/segment connections are used (but you don't know what the connections actually are). The unique signal patterns correspond to the ten different ways the submarine tries to render a digit using the current wire/segment connections. Because 7 is the only digit that uses three segments, dab in the above example means that to render a 7, signal lines d, a, and b are on. Because 4 is the only digit that uses four segments, eafb means that to render a 4, signal lines e, a, f, and b are on.
|
||||
|
||||
Using this information, you should be able to work out which combination of signal wires corresponds to each of the ten digits. Then, you can decode the four digit output value. Unfortunately, in the above example, all of the digits in the output value (cdfeb fcadb cdfeb cdbaf) use five segments and are more difficult to deduce.
|
||||
|
||||
For now, focus on the easy digits. Consider this larger example:
|
||||
|
||||
be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb |
|
||||
fdgacbe cefdb cefbgd gcbe
|
||||
edbfga begcd cbg gc gcadebf fbgde acbgfd abcde gfcbed gfec |
|
||||
fcgedb cgb dgebacf gc
|
||||
fgaebd cg bdaec gdafb agbcfd gdcbef bgcad gfac gcb cdgabef |
|
||||
cg cg fdcagb cbg
|
||||
fbegcd cbd adcefb dageb afcb bc aefdc ecdab fgdeca fcdbega |
|
||||
efabcd cedba gadfec cb
|
||||
aecbfdg fbg gf bafeg dbefa fcge gcbea fcaegb dgceab fcbdga |
|
||||
gecf egdcabf bgf bfgea
|
||||
fgeab ca afcebg bdacfeg cfaedg gcfdb baec bfadeg bafgc acf |
|
||||
gebdcfa ecba ca fadegcb
|
||||
dbcfg fgd bdegcaf fgec aegbdf ecdfab fbedc dacgb gdcebf gf |
|
||||
cefg dcbef fcge gbcadfe
|
||||
bdfegc cbegaf gecbf dfcage bdacg ed bedf ced adcbefg gebcd |
|
||||
ed bcgafe cdgba cbgef
|
||||
egadfb cdbfeg cegd fecab cgb gbdefca cg fgcdab egfdb bfceg |
|
||||
gbdfcae bgc cg cgb
|
||||
gcafb gcf dcaebfg ecagb gf abcdeg gaef cafbge fdbac fegbdc |
|
||||
fgae cfgab fg bagce
|
||||
|
||||
Because the digits 1, 4, 7, and 8 each use a unique number of segments, you should be able to tell which combinations of signals correspond to those digits. Counting only digits in the output values (the part after | on each line), in the above example, there are 26 instances of digits that use a unique number of segments (highlighted above).
|
||||
|
||||
In the output values, how many times do digits 1, 4, 7, or 8 appear?
|
||||
=#
|
||||
|
||||
infile = length(ARGS) > 0 ? ARGS[1] : "input.txt"
|
||||
println("infile = ", infile)
|
||||
|
||||
unique_sizes = Set([2, 4, 3, 7])
|
||||
|
||||
unique_size_output_count = 0
|
||||
|
||||
for line in eachline(infile)
|
||||
global unique_size_output_count
|
||||
inputs, outputs = [split(v) for v in split(line, " | ")]
|
||||
unique_idx = findall(s -> length(s) in unique_sizes, outputs)
|
||||
println(outputs, unique_idx)
|
||||
unique_size_output_count += length(unique_idx)
|
||||
end
|
||||
|
||||
println("unique size output count ", unique_size_output_count)
|
||||
152
day8/part2.jl
Executable file
152
day8/part2.jl
Executable file
@@ -0,0 +1,152 @@
|
||||
#!/usr/bin/env julia
|
||||
|
||||
#=
|
||||
--- Part Two ---
|
||||
|
||||
Through a little deduction, you should now be able to determine the remaining digits. Consider again the first example above:
|
||||
|
||||
acedgfb cdfbe gcdfa fbcad dab cefabd cdfgeb eafb cagedb ab |
|
||||
cdfeb fcadb cdfeb cdbaf
|
||||
|
||||
After some careful analysis, the mapping between signal wires and segments only make sense in the following configuration:
|
||||
|
||||
dddd
|
||||
e a
|
||||
e a
|
||||
ffff
|
||||
g b
|
||||
g b
|
||||
cccc
|
||||
|
||||
So, the unique signal patterns would correspond to the following digits:
|
||||
|
||||
acedgfb: 8
|
||||
cdfbe: 5
|
||||
gcdfa: 2
|
||||
fbcad: 3
|
||||
dab: 7
|
||||
cefabd: 9
|
||||
cdfgeb: 6
|
||||
eafb: 4
|
||||
cagedb: 0
|
||||
ab: 1
|
||||
|
||||
Then, the four digits of the output value can be decoded:
|
||||
|
||||
cdfeb: 5
|
||||
fcadb: 3
|
||||
cdfeb: 5
|
||||
cdbaf: 3
|
||||
|
||||
Therefore, the output value for this entry is 5353.
|
||||
|
||||
Following this same process for each entry in the second, larger example above, the output value of each entry can be determined:
|
||||
|
||||
fdgacbe cefdb cefbgd gcbe: 8394
|
||||
fcgedb cgb dgebacf gc: 9781
|
||||
cg cg fdcagb cbg: 1197
|
||||
efabcd cedba gadfec cb: 9361
|
||||
gecf egdcabf bgf bfgea: 4873
|
||||
gebdcfa ecba ca fadegcb: 8418
|
||||
cefg dcbef fcge gbcadfe: 4548
|
||||
ed bcgafe cdgba cbgef: 1625
|
||||
gbdfcae bgc cg cgb: 8717
|
||||
fgae cfgab fg bagce: 4315
|
||||
|
||||
Adding all of the output values in this larger example produces 61229.
|
||||
|
||||
For each entry, determine all of the wire/segment connections and decode the four-digit output values. What do you get if you add up all of the output values?
|
||||
=#
|
||||
|
||||
infile = length(ARGS) > 0 ? ARGS[1] : "input.txt"
|
||||
println("infile = ", infile)
|
||||
|
||||
function charset(s)
|
||||
return Set(c for c in s)
|
||||
end
|
||||
|
||||
unique_sizes = Set([2, 4, 3, 7])
|
||||
size_map = Dict(2 => [1],
|
||||
3 => [7],
|
||||
4 => [4],
|
||||
5 => [2, 3, 5],
|
||||
6 => [0, 6, 9],
|
||||
7 => [8])
|
||||
|
||||
segment_map = Dict(0 => "abcefg",
|
||||
1 => "cf",
|
||||
2 => "acdeg",
|
||||
3 => "acdfg",
|
||||
4 => "bcdf",
|
||||
5 => "abdfg",
|
||||
6 => "abdefg",
|
||||
7 => "acf",
|
||||
8 => "abcdefg",
|
||||
9 => "abcdfg")
|
||||
|
||||
set_num_map = Dict(charset(v) => k for (k, v) in segment_map)
|
||||
|
||||
function get_map(inputs)
|
||||
segmap = Dict(c => charset("abcdefg") for c in charset("abcdefg"))
|
||||
by_len = Dict{Int, Vector{Set{Char}}}()
|
||||
for s in inputs
|
||||
len = length(s)
|
||||
if haskey(by_len, len)
|
||||
push!(by_len[len], charset(s))
|
||||
else
|
||||
by_len[len] = [charset(s)]
|
||||
end
|
||||
end
|
||||
# segment 'a' is the element in 7 and not 1
|
||||
segmap['a'] = setdiff(by_len[3][1], by_len[2][1])
|
||||
|
||||
# segment 'f' is the element in 1 that is also in all
|
||||
# of the len 6 numbers [0, 6, 9], and segment 'c' is the
|
||||
# other element in 1
|
||||
len6_intersect = intersect(by_len[6][1], by_len[6][2], by_len[6][3])
|
||||
segmap['f'] = intersect(by_len[2][1], len6_intersect)
|
||||
segmap['c'] = setdiff(by_len[2][1], segmap['f'])
|
||||
|
||||
# segment 'b' is the element in 4 other than c/f that is also in all of
|
||||
# the len 6 numbers, and 'd' is the other one
|
||||
segmap['b'] = intersect(setdiff(by_len[4][1], by_len[2][1]),
|
||||
len6_intersect)
|
||||
segmap['d'] = setdiff(setdiff(by_len[4][1], by_len[2][1]),
|
||||
segmap['b'])
|
||||
|
||||
# segment 'g' is the element in all len 6 numbers that is not any that
|
||||
# we have already found
|
||||
segmap['g'] = setdiff(len6_intersect,
|
||||
union(segmap['a'], segmap['f'], segmap['c'],
|
||||
segmap['b'], segmap['d']))
|
||||
segmap['e'] = setdiff(segmap['e'],
|
||||
union(segmap['a'], segmap['f'], segmap['c'],
|
||||
segmap['b'], segmap['d'], segmap['g']))
|
||||
|
||||
segs_to_num = Dict{Set{Char}, Int}()
|
||||
for (num, segs) in segment_map
|
||||
num_seg_set = Set{Char}()
|
||||
for c in segs
|
||||
union!(num_seg_set, segmap[c])
|
||||
end
|
||||
segs_to_num[num_seg_set] = num
|
||||
end
|
||||
return segs_to_num
|
||||
end
|
||||
|
||||
total = 0
|
||||
|
||||
for line in eachline(infile)
|
||||
global total
|
||||
inputs, outputs = [split(v) for v in split(line, " | ")]
|
||||
set_to_num = get_map(inputs)
|
||||
|
||||
v = 0
|
||||
nout = length(outputs)
|
||||
for (i, s) in enumerate(outputs)
|
||||
v += set_to_num[charset(s)] * 10^(nout - i)
|
||||
end
|
||||
total += v
|
||||
end
|
||||
|
||||
println("sum ", total)
|
||||
10
day8/test.txt
Normal file
10
day8/test.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb | fdgacbe cefdb cefbgd gcbe
|
||||
edbfga begcd cbg gc gcadebf fbgde acbgfd abcde gfcbed gfec | fcgedb cgb dgebacf gc
|
||||
fgaebd cg bdaec gdafb agbcfd gdcbef bgcad gfac gcb cdgabef | cg cg fdcagb cbg
|
||||
fbegcd cbd adcefb dageb afcb bc aefdc ecdab fgdeca fcdbega | efabcd cedba gadfec cb
|
||||
aecbfdg fbg gf bafeg dbefa fcge gcbea fcaegb dgceab fcbdga | gecf egdcabf bgf bfgea
|
||||
fgeab ca afcebg bdacfeg cfaedg gcfdb baec bfadeg bafgc acf | gebdcfa ecba ca fadegcb
|
||||
dbcfg fgd bdegcaf fgec aegbdf ecdfab fbedc dacgb gdcebf gf | cefg dcbef fcge gbcadfe
|
||||
bdfegc cbegaf gecbf dfcage bdacg ed bedf ced adcbefg gebcd | ed bcgafe cdgba cbgef
|
||||
egadfb cdbfeg cegd fecab cgb gbdefca cg fgcdab egfdb bfceg | gbdfcae bgc cg cgb
|
||||
gcafb gcf dcaebfg ecagb gf abcdeg gaef cafbge fdbac fegbdc | fgae cfgab fg bagce
|
||||
100
day9/input.txt
Normal file
100
day9/input.txt
Normal file
@@ -0,0 +1,100 @@
|
||||
5678998989432198943298876799876586789998999434988989999876544345789987654545678931299765458679432456
|
||||
4556987678949987899097655679767345678987898949876579899987432134889987653237889542989654323488921347
|
||||
3245696569298765678976544598653234567896567899885467799876598245678998542126898769878943212367892356
|
||||
7656789689019654569895432987743125678923456998764345689987987656789987632015789998767932103456789969
|
||||
8767898789298965698789321296532012789912367897651234993398999867897898543234567897657899344868999898
|
||||
9898909899987976987679434987432123567893458976543349892109234978935987654945678956545888956779019787
|
||||
2999312999876899876548976798543256798954578987985498789290125989124698878876789545434677897892197656
|
||||
1098929989765678987537897899674345689765989299876989687989234998995789989989895432123456998999976545
|
||||
2987898763234567895426899999965457999879999101999878576978949876789891096593987643034587899698765434
|
||||
3496987654345979971015678989876598943989898999998765465767898765678992987432398854234567934569876745
|
||||
4985499795659898965434599575989699432396797878999874323456789654567989989321239954346789123456987659
|
||||
9875324989798787896545789434798789101234986767899983212345896543459878976310198765457991014569999868
|
||||
9766219878989656789659897321679893212379875456798432101236998532598767895421239876678962195998941979
|
||||
9854398768878968998767896432567954523459864323987543212456897431999656789532345987789653979896532989
|
||||
8765498656867978979898998543459995745569876434598798654567896569898545678945478998999969867789549899
|
||||
9876986545457899656979999876598989856978987945679898767878987798767634599999567899019898656678998789
|
||||
9989978432345898789767899987987679997899199896789979878989498987656424989987678999198776547578999678
|
||||
8998764321256789995456789999976567989921099798996568989595349998843213577898789878998652123469996578
|
||||
7987653210127897654365898998765465678932989699765439995430157898632102356999896569986541012345987989
|
||||
6398865432238976543234567899901234567899876587996549899321298976543214467899954398997632123657898994
|
||||
5219976554349899432129698999893345678998765346789659798932349998654323578999876497989543234567899543
|
||||
4323987665656798921098989998789556989987654223878998687893499969875987679987988986578994547678978932
|
||||
9495999877897987892987878987678967898998743012567896546979987855987898789876599965489789659789459891
|
||||
8989892998999876789976567897567998957989432123456985434569756234598939898765498744345699967891298789
|
||||
7678793459998965678975456976456789549876543254569876324598542123459329999654399534234898898999987678
|
||||
6547689567987854769894345994347695434987656767878987765987643044678998998765987620123456789998976567
|
||||
2134578979876543456789234789234589323698787878989998987896543235799987989976798731245977898987894348
|
||||
1023456798989874767890125696345678934569898999598999199987654376789876476799987654356898987656791239
|
||||
2434587997898765889931459895456989545679999545457889298998775479898784345678998767487929876545789545
|
||||
3679679876779976789432345796587997656798789432345678987989986567987673234789769876598934987656899696
|
||||
4598798785669899996543456998798998787989699421234567896767897678976543125898654987679875898967998989
|
||||
5689987674357698999876567899899899899876568994365898965658998789985432016789543298789876789878987977
|
||||
6898998543234567899997878965956789921965434589456789984347899899876543123895432109897989898989995456
|
||||
7987899932123456789398989954345997899875325678987896543256789976987754544789549212976799997699984378
|
||||
8996998761014767891239997896456896898765456789198987632149899965599865765678998999765678986569876467
|
||||
9965987654145678910949876789587965789876567991019996543234999843479876878989576788974599875454987678
|
||||
7894398743234599999898765678998954678987878992398897755345898764567987989995435567893987654323499789
|
||||
6989219894346789987769989789989563867999989989987789976456789879679998999876323467932198763212599890
|
||||
5678923998456799976545699899876432345693299979876667896597999998998999998987212348942019874343989921
|
||||
4567894986568897895434567921987321276789129864985456997678999987897999987658301239953198765454678934
|
||||
3656789997679936789323989210995490987899298743234357898789878546786789876543212367893239887565789995
|
||||
2345678998789325679999898929876989898998987654101256789898765435545678987665325456789357997676799989
|
||||
3476789019995434569878787899989978789997987643214347999999854321236789898775434567895467898789989878
|
||||
4567892129876765698765676789998767679876987654765656789998763210145698769887655698986578969899878767
|
||||
5688943436987986997654565678987654598765498769876777899989954321234789656998767899987699656998766456
|
||||
7899894545698999876543234567896543987654329989987888989679895452345678946989878957899988997987654345
|
||||
9956789656789123987632123479987659876543213597898999876565789667458789239878989745987567689998765456
|
||||
2345678967894234696541014578898767998632101456789767987434578978569890198767897659986456578909876567
|
||||
3456789878954356986532123456789878996543212347890345898323467999998932987656789798765343469219989878
|
||||
4567896989996969897543234587990989987654323456921256789212356989897949876545678999973212458998596989
|
||||
8689965699989898798656945678991399898785434567892345892101249876786899865434567899865393567967445698
|
||||
8792123989876767679987898789789989789897545679953556789234598765645697654323798999976989998953234567
|
||||
9893439876565654567898989897678976567998976789767787896545987543234789763212346789899877899762146788
|
||||
1999598765434123456999878978567985445879987899879898987657998652101679654101235898765766794321017899
|
||||
2398987654321014567894569865379876234567898999989999698769876543213568969233346789654545689987634799
|
||||
9987898998432123798923498765456989445789999998993987569899989984623479598944567896543234568976545678
|
||||
8765659996543235689212349878579876556895698987894595456989999876534689457899698965432124589987678989
|
||||
5434347987656345678901656987689987667894987656889696579878989987678994345678969999943012679799889998
|
||||
6521236798965456789892997899893198789989876645678989698769878998789101234589456987899323569654999987
|
||||
7632345899878569898799789910999099895469985434569878997645568999893214456678967965678954578967998976
|
||||
6543656901989689987687678921398987994398996945698969876431467989965323467889899876789995678979876765
|
||||
7654567899798799976546469892987856789987889896987654986542349878976545578996791987899989789998765434
|
||||
8765678978669899865435346789896543899765678789998743297656598967897767899545932398999878999987654321
|
||||
9889889765554987654321234599787012987654885678987654198787987656899898989959893999989656798999766432
|
||||
6998999984323498765634345987652123498753234569988621019899876547999979569899789899877545987899997949
|
||||
5656789993212549876765678999543934995432123458976542323999987656898763456798656789765434876789989898
|
||||
3245678995301234987876789987659899876541012367897643434589998867897652367987545679654321345899876767
|
||||
5346889876418345698997895698998767987632123456789656565678939978998743489876434599865632346989765656
|
||||
6757895987567896789298994349898657898743246577898969996799320989987654578987645689976543559876534345
|
||||
7878934599678987892129789656798745569854768688957898789978999999898965789998786792987654667987621234
|
||||
8989321698789998943345678969959433498769879799346789698767688998789896999979897891099788789599732345
|
||||
9399934789899999874456989298743212349899989890123699545654567987678797899765998999988999995498543456
|
||||
1239895698999889965569890129654101257999897921234568932343879876587689999854349998877989654397654767
|
||||
3398789987898779897698789298765233398998765434345678921012998765434567899873234987765678965298765878
|
||||
5497679876789657789987678999874345479999876845456989532323459764323456789984749896584567994349896989
|
||||
9986545985698745678998545899985456567899987656567896543456798765436578999875698785323456789956997891
|
||||
8765439876789434567899676789876567898989998787898987654578899877748789854976987654212345679897989930
|
||||
9854323987896567678998787898998678959678999898999498767999901987659899765987998765401256798769878921
|
||||
9965214598987679789799898997679789344569989969894329898999892398767978979998999878919347987656767932
|
||||
9876329699998799897689999999569896103498767456789212959998789999898956798939389989898959876545658993
|
||||
3987898989899899954599987878979975212997656345892101345894569889999345987821278998767898765434545689
|
||||
2198987676799989323989765468998764329876543234789292456793298767893239876710367987656797654323434578
|
||||
3999997545989878939878954359899765478987832145678989667894987656789123985321459977645498967410123467
|
||||
9889998659878967899867895456789876569998953256899679998965976545993239876432598766532359978823234589
|
||||
9775879769767456798756789767898987878999866367996567899876987636789998987543679854321267898764345699
|
||||
8654569898659345986545678978987898989398765456789456999987898747898767987654678965432878929875456789
|
||||
8743453986545239875434567899876789999219876567894347898998999898989654599865679876543989434986578999
|
||||
9432012987432129854323456789765898998723987678989256987899298969678913479876789987767998745797989378
|
||||
6543123497654098765212345678954767896545698989879129876789197654599904567989894398979997656998990165
|
||||
8656934598843259876323467989653457997959789698768998695778986543489895989199965219497998797899989234
|
||||
9879899789654348985434578999942346789898994599545986434569999642376789891019876101356989989987678946
|
||||
9998778998765467897665689678799457898767895987439876545678998921235679792198989212349879978998567897
|
||||
9897669899989878998786789545678968998656789876545989656789987892346895679987654323498768967899456789
|
||||
8789456789999989659887895434567899876545457987676798767899876789457893568999876654599545656789577897
|
||||
7678345678999896543998976125678923965432346798787899879954965878967932477899987765987434346899789956
|
||||
8543234567898765432159876546789019876553456899898965989965954767898921276789998879876521235678993234
|
||||
8732145678969876643345987657893247987864579965999754296899843459899632345899879989987432346789210123
|
||||
9543234689654987754456799768985356798977678954987653124989752576789543656946965399876545457894321234
|
||||
7687657995323499767569894978976467899398789432098764349876641345689654568939873212999856567965432345
|
||||
8798767897435679898989943989989878910239896545139975668965432456799767899123982101298767878978644567
|
||||
70
day9/part1.jl
Normal file
70
day9/part1.jl
Normal file
@@ -0,0 +1,70 @@
|
||||
#!/usr/bin/env julia
|
||||
|
||||
#=
|
||||
--- Day 9: Smoke Basin ---
|
||||
|
||||
These caves seem to be lava tubes. Parts are even still volcanically active; small hydrothermal vents release smoke into the caves that slowly settles like rain.
|
||||
|
||||
If you can model how the smoke flows through the caves, you might be able to avoid it and be that much safer. The submarine generates a heightmap of the floor of the nearby caves for you (your puzzle input).
|
||||
|
||||
Smoke flows to the lowest point of the area it's in. For example, consider the following heightmap:
|
||||
|
||||
2199943210
|
||||
3987894921
|
||||
9856789892
|
||||
8767896789
|
||||
9899965678
|
||||
|
||||
Each number corresponds to the height of a particular location, where 9 is the highest and 0 is the lowest a location can be.
|
||||
|
||||
Your first goal is to find the low points - the locations that are lower than any of its adjacent locations. Most locations have four adjacent locations (up, down, left, and right); locations on the edge or corner of the map have three or two adjacent locations, respectively. (Diagonal locations do not count as adjacent.)
|
||||
|
||||
In the above example, there are four low points, all highlighted: two are in the first row (a 1 and a 0), one is in the third row (a 5), and one is in the bottom row (also a 5). All other locations on the heightmap have some lower adjacent location, and so are not low points.
|
||||
|
||||
The risk level of a low point is 1 plus its height. In the above example, the risk levels of the low points are 2, 1, 6, and 6. The sum of the risk levels of all low points in the heightmap is therefore 15.
|
||||
|
||||
Find all of the low points on your heightmap. What is the sum of the risk levels of all low points on your heightmap?
|
||||
=#
|
||||
|
||||
infile = length(ARGS) > 0 ? ARGS[1] : "input.txt"
|
||||
println("infile = ", infile)
|
||||
|
||||
depth_map = reduce(vcat, [parse.(Int, split(line, ""))'
|
||||
for line in eachline(infile)])
|
||||
|
||||
#display(depth_map)
|
||||
#println()
|
||||
|
||||
|
||||
function neighbors(m, y, x)
|
||||
nrows, ncols = size(m)
|
||||
v = eltype(m)[]
|
||||
if x > 1
|
||||
push!(v, m[y, x-1])
|
||||
end
|
||||
if x < ncols
|
||||
push!(v, m[y, x+1])
|
||||
end
|
||||
if y > 1
|
||||
push!(v, m[y-1, x])
|
||||
end
|
||||
if y < nrows
|
||||
push!(v, m[y+1, x])
|
||||
end
|
||||
return v
|
||||
end
|
||||
|
||||
function get_low_sum(m)
|
||||
total = 0
|
||||
nrows, ncols = size(m)
|
||||
for x in 1:ncols
|
||||
for y in 1:nrows
|
||||
if all(m[y, x] .< neighbors(m, y, x))
|
||||
total += m[y, x] + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
return total
|
||||
end
|
||||
|
||||
println(get_low_sum(depth_map))
|
||||
146
day9/part2.jl
Normal file
146
day9/part2.jl
Normal file
@@ -0,0 +1,146 @@
|
||||
#!/usr/bin/env julia
|
||||
|
||||
#= --- Part Two ---
|
||||
|
||||
Next, you need to find the largest basins so you know what areas are most important to avoid.
|
||||
|
||||
A basin is all locations that eventually flow downward to a single low point. Therefore, every low point has a basin, although some basins are very small. Locations of height 9 do not count as being in any basin, and all other locations will always be part of exactly one basin.
|
||||
|
||||
The size of a basin is the number of locations within the basin, including the low point. The example above has four basins.
|
||||
|
||||
The top-left basin, size 3:
|
||||
|
||||
2199943210
|
||||
3987894921
|
||||
9856789892
|
||||
8767896789
|
||||
9899965678
|
||||
|
||||
The top-right basin, size 9:
|
||||
|
||||
2199943210
|
||||
3987894921
|
||||
9856789892
|
||||
8767896789
|
||||
9899965678
|
||||
|
||||
The middle basin, size 14:
|
||||
|
||||
2199943210
|
||||
3987894921
|
||||
9856789892
|
||||
8767896789
|
||||
9899965678
|
||||
|
||||
The bottom-right basin, size 9:
|
||||
|
||||
2199943210
|
||||
3987894921
|
||||
9856789892
|
||||
8767896789
|
||||
9899965678
|
||||
|
||||
Find the three largest basins and multiply their sizes together. In the above example, this is 9 * 14 * 9 = 1134.
|
||||
|
||||
What do you get if you multiply together the sizes of the three largest basins?
|
||||
=#
|
||||
|
||||
infile = length(ARGS) > 0 ? ARGS[1] : "input.txt"
|
||||
println("infile = ", infile)
|
||||
|
||||
depth_map = reduce(vcat, [parse.(Int, split(line, ""))'
|
||||
for line in eachline(infile)])
|
||||
|
||||
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 neighbors(m, y, x)
|
||||
nrows, ncols = size(m)
|
||||
v = eltype(m)[]
|
||||
if x > 1
|
||||
push!(v, m[y, x-1])
|
||||
end
|
||||
if x < ncols
|
||||
push!(v, m[y, x+1])
|
||||
end
|
||||
if y > 1
|
||||
push!(v, m[y-1, x])
|
||||
end
|
||||
if y < nrows
|
||||
push!(v, m[y+1, x])
|
||||
end
|
||||
return v
|
||||
end
|
||||
|
||||
function flow_neighbors(m, y, x, flow_map, flow_count)
|
||||
nrows, ncols = size(m)
|
||||
nidx = neighbor_idx(nrows, ncols, y, x)
|
||||
v = m[y, x]
|
||||
if v == 9
|
||||
flow_count[y, x] = 0
|
||||
return
|
||||
end
|
||||
smallest_v = v
|
||||
flow_idx = flow_map[y, x]
|
||||
for idx in nidx
|
||||
if m[idx] < smallest_v
|
||||
smallest_v = m[idx]
|
||||
flow_idx = flow_map[idx]
|
||||
while flow_map[flow_idx] != flow_idx
|
||||
flow_idx = flow_map[flow_idx]
|
||||
end
|
||||
end
|
||||
end
|
||||
if smallest_v != v
|
||||
flow_count[flow_idx] += flow_count[y, x]
|
||||
flow_count[y, x] = 0
|
||||
flow_map[y, x] = flow_idx
|
||||
end
|
||||
#println(y, "x", x)
|
||||
#display(flow_count)
|
||||
#println()
|
||||
end
|
||||
|
||||
function flow_all(m)
|
||||
total = 0
|
||||
nrows, ncols = size(m)
|
||||
flow_count = ones(Int, nrows, ncols)
|
||||
flow_map = reshape([CartesianIndex(y, x)
|
||||
for x in 1:ncols
|
||||
for y in 1:nrows], nrows, ncols)
|
||||
for x in 1:ncols
|
||||
for y in 1:nrows
|
||||
flow_neighbors(m, y, x, flow_map, flow_count)
|
||||
end
|
||||
end
|
||||
return flow_map, flow_count
|
||||
end
|
||||
|
||||
map, count = flow_all(depth_map)
|
||||
|
||||
if (length(depth_map) < 100)
|
||||
display(count)
|
||||
println()
|
||||
end
|
||||
|
||||
basins = sort(filter(x -> x > 0, count))
|
||||
display(basins)
|
||||
println()
|
||||
|
||||
println("nbasins = ", length(basins))
|
||||
println("largest_prod = ", prod(basins[end-2:end]))
|
||||
5
day9/test.txt
Normal file
5
day9/test.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
2199943210
|
||||
3987894921
|
||||
9856789892
|
||||
8767896789
|
||||
9899965678
|
||||
Reference in New Issue
Block a user