Day 08
This commit is contained in:
parent
80fc798274
commit
4fd90e2a70
113
day08/default.nix
Normal file
113
day08/default.nix
Normal file
@ -0,0 +1,113 @@
|
||||
{ pkgs, lib }:
|
||||
|
||||
with lib;
|
||||
|
||||
let
|
||||
calculateTreeVisibilityForLine = line: let
|
||||
updateState = { currentMax ? (-1), trees ? [] }: tree:
|
||||
if tree > currentMax then { currentMax = tree; trees = trees ++ [true]; }
|
||||
else { inherit currentMax; trees = trees ++ [false]; };
|
||||
forwards = (foldl updateState { } line).trees;
|
||||
backwards = reverseList (foldr (flip updateState) { } line).trees;
|
||||
in zipListsWith or forwards backwards;
|
||||
|
||||
transpose = grid:
|
||||
genList (n: map ((flip elemAt) n) grid) (length grid);
|
||||
|
||||
combineGridsWith = f: grid1: grid2: let
|
||||
height = length grid1;
|
||||
width = length (elemAt grid1 0);
|
||||
elemAt' = row: col: grid: elemAt (elemAt grid row) col;
|
||||
generator = row: col: f (elemAt' row col grid1) (elemAt' row col grid2);
|
||||
in genList (row: genList (col: generator row col) width) height;
|
||||
|
||||
trees = pipe ./input.txt [
|
||||
fileContents
|
||||
(splitString "\n")
|
||||
(map (splitString ""))
|
||||
(map tail)
|
||||
(map init)
|
||||
(map (map toInt))
|
||||
];
|
||||
|
||||
treeVisibleGrid = pipe trees [
|
||||
(lines: { horizontal = lines; vertical = transpose lines; })
|
||||
(mapAttrs (_: map calculateTreeVisibilityForLine))
|
||||
({horizontal, vertical}: { inherit horizontal; vertical = transpose vertical; })
|
||||
({horizontal, vertical}: combineGridsWith or horizontal vertical)
|
||||
];
|
||||
|
||||
visualization = let
|
||||
genColor = name: command: builtins.readFile
|
||||
(pkgs.runCommand name {
|
||||
buildInputs = [ pkgs.ncurses ];
|
||||
} "tput ${command} > $out");
|
||||
red = genColor "red" "setaf 1";
|
||||
green = genColor "green" "setaf 2";
|
||||
clear = genColor "clear" "sgr0";
|
||||
|
||||
greenRedTree = visible: tree:
|
||||
if visible then "${green}${tree}${clear}"
|
||||
else "${red}${tree}${clear}";
|
||||
|
||||
in pipe trees [
|
||||
(map (map toString))
|
||||
(combineGridsWith greenRedTree treeVisibleGrid)
|
||||
(map (concatStringsSep ""))
|
||||
(concatStringsSep "\n")
|
||||
];
|
||||
|
||||
answer1 = pipe treeVisibleGrid [
|
||||
(map (count id))
|
||||
(foldr add 0)
|
||||
toString
|
||||
];
|
||||
|
||||
countUntil = stopPred: xs:
|
||||
if xs == [] then 0
|
||||
else if stopPred (head xs) then 1
|
||||
else 1 + (countUntil stopPred (tail xs));
|
||||
|
||||
visibleDistanceBackwards = line: let
|
||||
measure = trees: size: let
|
||||
newTree = {
|
||||
inherit size;
|
||||
viewDistance = countUntil (x: x.size >= size) trees;
|
||||
};
|
||||
in [newTree] ++ trees;
|
||||
in pipe line [
|
||||
(foldl measure [])
|
||||
(map (x: x.viewDistance))
|
||||
reverseList
|
||||
];
|
||||
|
||||
visibleDistanceHorizontal = line: let
|
||||
backwards = visibleDistanceBackwards line;
|
||||
forwards = pipe line [
|
||||
reverseList
|
||||
visibleDistanceBackwards
|
||||
reverseList
|
||||
];
|
||||
in zipListsWith (x: y: x * y) forwards backwards;
|
||||
|
||||
answer2 = pipe trees [
|
||||
(lines: { horizontal = lines; vertical = transpose lines; })
|
||||
(mapAttrs (_: map visibleDistanceHorizontal))
|
||||
({horizontal, vertical}: { inherit horizontal; vertical = transpose vertical; })
|
||||
({horizontal, vertical}: combineGridsWith (x: y: x * y) horizontal vertical)
|
||||
(map (foldr max 0))
|
||||
(foldr max 0)
|
||||
toString
|
||||
];
|
||||
|
||||
in pkgs.writeText "answers" ''
|
||||
Task1:
|
||||
${answer1}
|
||||
|
||||
Visualization:
|
||||
|
||||
${visualization}
|
||||
|
||||
Task2:
|
||||
${answer2}
|
||||
''
|
99
day08/input.txt
Normal file
99
day08/input.txt
Normal file
@ -0,0 +1,99 @@
|
||||
020111211022103023211142403044034010412103202221251542233334311102303044440313120021223120332211221
|
||||
121001110111201102213124201444431204213221413222211111444444412110041144131210002230130121313101010
|
||||
112010221202200121420032021412024323255214355122345552225213252324112400440302313420323102103100221
|
||||
211022232121222123201224234031435144114512425232345144523531533222344004011342234110131312300332202
|
||||
202103101023303021233104321411251553151424242531344221312225231225524421412434024103021300123300121
|
||||
202122200002211022342141123051132352344252152315525324551424132421225143201002114204223302333232221
|
||||
011310312223223421431113235421121321152124551441143435255214322514544552225412313011414423301132102
|
||||
121331322013001133311000223433443341113554412556562564315233144532455221433431002003332002203013201
|
||||
003201332004241133340023541532333131413153266664363423353232622124454254554345422200200433100333102
|
||||
230030131344203011040552414515334144165664524445366334422626244444323115333252423443232113432003211
|
||||
020300112334333142144323414353551436334563654426436625356343222253643212532215422020214344013310331
|
||||
220002231221003314452445344223355655546325233543653632544634656464243551334233433413423044300230210
|
||||
111022100242320043435324555421362243363346366346456252353246655455262455415331454531201000441410231
|
||||
101232013434204241242451442362524554225656254252456532663634445256345634451144555513214431300213021
|
||||
021113211441142541244515256653533353262654644343347336234522332254544443346322422154143020040021110
|
||||
002032342223245141521455555365463263562446777664546776637635443223664266645633333231151132112140113
|
||||
002033421301452414544444345232465536465366346553375665564756366435442422632435555111445311104203133
|
||||
332303032232445231354236322622423634433537753546335477544636344436526324435436522445155410213401230
|
||||
214021124335354212514535634245552337436663757533364556376776733565333462254562365531354214423412431
|
||||
231123230423124441525624265443647673566756453545563655373357453547537663433453433212213223101140424
|
||||
100211320333154123522263362425466444634667564437776546465446345667366445453365366551352122420231230
|
||||
024400401554531424535342424657437377754456437376767656436366374765557746355562226364213315421212320
|
||||
214442223135544515264335435467756354735433346664764464468465443746334373746425422542525455354340110
|
||||
141333001441254326655252563743544735555378755475567457777687677357576573543344653346551442221242312
|
||||
022033123224325536562464253467675635635678458757477754757874576653435356676234462564555551451230022
|
||||
444402313413432333426636637636453646648557685556587678886878777785454364533433543362622525453201222
|
||||
031024134513414333456224636475357556855564445765756678575486648885765563445546566235352311454320030
|
||||
341331341415512452344636337436467675565575756658588888477584775646657736366434365523664142235144413
|
||||
333344214132152543325563654447567486457556845676545687457688885466457333476777342344625323533242333
|
||||
203124152315365546566375565336778768544768846599686578655665448478657834657533335544343231212251101
|
||||
200144235323636235437557635373578555545475587655886585897998654658858755534374677344254563233421200
|
||||
433243323452635426237436445738684446475489658878699879887988684656474746764334543334452565153341241
|
||||
340411231113223465344733555465744685569658998876796578688888887678657675875533766464362432543531231
|
||||
132155112416452662274535754677457567869597559977756878779755568868854644586734466764422245333151550
|
||||
321153454153653654743477667888756477986578656976558879999589975695487876766664653466625262642234421
|
||||
414524133155254243564664555445778475666688565697596679558768656758547564888437343772346336642515514
|
||||
321251321324554554436434474754674597576658986766997798688766897876884764444653674674326233445555243
|
||||
031553412663524646375654566887688857698655696699699989968699659665774657875735457664452362352111522
|
||||
121541141642244257534355645887786995858959886778688769889987779898696567777567344373632655621545545
|
||||
334343242432423553675774445756455759676757789677879778999988695976997447856786357655446656351514552
|
||||
125145532643224566754564678556569669557567799868776897666898866898879866784847777744353546366314141
|
||||
045114213352452647446647465888875868689699789677897977779999699895556776458746737674746244442322442
|
||||
132343555663565436365447746478789879798676878686777999789966769777759954677478745673552645533545344
|
||||
045253354653642556466557766588567888887978866878777776986676768968695857655674556463732652626443441
|
||||
324411423536354677366485874488885578788769789998797978797888777868996895476778847746764226464355343
|
||||
355421453642352476436787475855785669696986976979989787787998669696679989555875747556435562425323311
|
||||
225422326356526465747344444546866698678986799879779799999876866969999686664477767757535635652555421
|
||||
215234242322226376376776764668889755898766677787987779877967696689565966757675463574434246652622343
|
||||
214252444222333767567367778585695867987669688798987987789888698888858597545545475573737453632634414
|
||||
435144324646623745547785547465689576878976877888878988989877866686976879488454664357347554642541254
|
||||
424224446333664566656688545685586987987699788897978888788999878967887869445474863534753362524653532
|
||||
234521532264544735666677658457578679997886798878788998987976677895559685784445835563745433436535545
|
||||
541253562333326733576345574569866788667878789897999998887996866685657897848485475437744246636412312
|
||||
531515163362265375476387485646556857878999799999999989879989886696789765664474863474462523344554532
|
||||
134323536435565375644658845476675857999867789999988888799986676978987577885467643356656525623333421
|
||||
432434326635433776557758756889896978587689688777989999797796888999765958586488873373565325435254453
|
||||
352344243543664654777655867556767767686778796989888798998697976755779565488844544677343523234525134
|
||||
023522455223435563774634466685769765877876776996699877886876668865856964846675357575675235445525211
|
||||
452211123455454466574537744568597875596879779869788776669767986968956557448486336534544244456115451
|
||||
154255352645344566574476875484685575595898898697799799688669879869766768688476473673634645556554324
|
||||
153224532366353377543578485765465665577767776999977978876879865565957587758448456773452265664315213
|
||||
212223354662352476456344644856876756566976677698968768897897866997679566845464663374352444344522513
|
||||
333134124655526377555445475778655559695856687767878889687866969787796886748547665457422543622531314
|
||||
114114243655626456537464558857564557866777897897898988696885586978887448448543375665233363532515252
|
||||
022123225426235336675765546686448888566865666569876966556979869667956845877753374733454555422452344
|
||||
105112231544246463366336777688875488697685796985999888976956985978664485756743456372244565641412542
|
||||
243421525526425322655767646868764584599579555769857975877669799998745668566534743565233224245452434
|
||||
310412235133463356343754335447454467578555798898788997569756589585876868467475435524433556255213522
|
||||
122253434533466554567356664688567676567976599967878669868967997845484546736534545734455626245154423
|
||||
313334534424352263526466637778587787868565658999559897596678677574858776343543534432632333531241224
|
||||
001031252124265545567437743765668445564487997555965858678954577555545545743366457636433553523254344
|
||||
044305452425444342246365565576568644874888888677955886697487686854588744354437555336664241413533111
|
||||
110113123534526242542645447354547688467557884774478677887477777586654656653436654455626314334324023
|
||||
403311355254242426345425533745657486754657454674877454647844866774463676664344325662334351444144012
|
||||
114043453225323265365363357353773386544675474785655464585675566555543364665665355442555535443420344
|
||||
234021355512324262426566274773756436677647664566764646854655875476556464746532226425352124354233001
|
||||
104234323224222234534254555373773666356744475886877868685764755374634533664662562645321154355101301
|
||||
413312132345313224565423553463467666456365585778746647466885776356666677636556254365513311121044324
|
||||
010140223145323354554344523347476477547564757555585887484666633663433654743326266555221322431333120
|
||||
023311233225342421354662654347733666374666336373663635367447753577353743232632663243333251551031321
|
||||
143043334422453221246324462653756444767736544367544755375437335646557334655533644454121143303220002
|
||||
101212344011254432246625336226267546745356655577456676377355677475746633565634244145133154032123242
|
||||
233244204414154431434433222525444454344635736634774576466473546373725623264634363324244114303014300
|
||||
010103422332213512154554324533443662535535356636747655647737636354525554536436123454141421414030443
|
||||
203123300244432323224135253642444433566335637575443374634433336553444225543564152215332533001133230
|
||||
213003230312403552352142145445635666323653353353657647435752235322633623453542112525124011312421020
|
||||
120004434213011143254452233425222536464236356544634635324242345464353322232141135221424123200431123
|
||||
331000214011314431233211121223425334456246345226324246526344536556636556453143153253323114441131132
|
||||
303123142412333143322451153243464566344362342525524265432552566442626262524431454151443133021301210
|
||||
220100102032004440315125323552226234566265233246334556355333643662354455122325544112124232340231031
|
||||
112122312031202424345533512342151435534325334444254264542526645645533111234131131223411304322200110
|
||||
213031212133401330114433121533455425225634422363334633454623522242353342455454511303033230433202120
|
||||
223232022333401311421344332255311133421254642562465526534445512134533413153142410113101444330132130
|
||||
020133013100324344342244215442123125243314222446342424421353134352515124333133430431401122202211032
|
||||
102300310001014311443112031115242425132234255215315523551534154524134344433432201022200432133000302
|
||||
001233311232134334100401400151532231322435145412132433235254313414243334103312232304403111313321210
|
||||
110213022233310011220103432114343112114222424442543155552411511144115122143322304023012132213011210
|
||||
000220123220323122321103444304134551331233141552114422121151224211122042434324042030120133230110110
|
||||
020120232301110331441234422110132302233413123211331413433254521355004032312110222131010330301222211
|
5
day08/test.txt
Normal file
5
day08/test.txt
Normal file
@ -0,0 +1,5 @@
|
||||
30373
|
||||
25512
|
||||
65332
|
||||
33549
|
||||
35390
|
Loading…
Reference in New Issue
Block a user