From 19047d8d6b457c8cc47d61cc7694cdd120e38def Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Thu, 8 Dec 2022 07:59:28 +0000 Subject: [PATCH] Done day 8 --- advent-of-code22.cabal | 5 +++ advent08/Main.hs | 83 +++++++++++++++++++++++++++++++++++ data/advent08.txt | 99 ++++++++++++++++++++++++++++++++++++++++++ data/advent08a.txt | 5 +++ 4 files changed, 192 insertions(+) create mode 100644 advent08/Main.hs create mode 100644 data/advent08.txt create mode 100644 data/advent08a.txt diff --git a/advent-of-code22.cabal b/advent-of-code22.cabal index 4ad8da0..0f7e6b5 100644 --- a/advent-of-code22.cabal +++ b/advent-of-code22.cabal @@ -136,3 +136,8 @@ executable advent07 import: common-extensions, build-directives main-is: advent07/Main.hs build-depends: text, attoparsec, containers, path-tree, rosezipper + +executable advent08 + import: common-extensions, build-directives + main-is: advent08/Main.hs + -- build-depends: text, attoparsec, containers, path-tree, rosezipper diff --git a/advent08/Main.hs b/advent08/Main.hs new file mode 100644 index 0000000..12f0b21 --- /dev/null +++ b/advent08/Main.hs @@ -0,0 +1,83 @@ +-- Writeup at https://work.njae.me.uk/2022/12/07/advent-of-code-2022-day-7/ + +import AoC +-- import Data.Char +import Data.List + + +data Tree = Tree Int Bool -- height, isVisible + deriving (Show, Eq) +type Forest = [[Tree]] + +main :: IO () +main = + do dataFileName <- getDataFileName + text <- readFile dataFileName + let forest = fmap (fmap readTree) $ lines text + -- print forest + -- print $ findVisibilityOrient forest + -- print $ findVisibilityForest forest + -- print $ countVisible $ findVisibilityForest forest + print $ part1 forest + print $ part2 forest + -- print $ part1 sizedTree + -- print $ part2 sizedTree + +part1 :: Forest -> Int +part1 = countVisible . findVisibilityForest +-- part1, part2 :: STree -> Integer +-- part1 = foldTree (\x xs -> sum (x:xs)) . fmap cancelLarge + +readTree :: Char -> Tree +readTree h = Tree (read [h]) False + +findVisibility :: [Tree] -> [Tree] +findVisibility row = snd $ foldl' vis (-1, []) row + where vis (highest, tagged) (Tree height visible) + | height > highest = (height, tagged ++ [Tree height True]) + | otherwise = (highest, tagged ++ [Tree height visible]) + +findVisibilityOrient :: Forest -> Forest +findVisibilityOrient = fmap findVisibility + +findVisibilityForest :: Forest -> Forest +findVisibilityForest forest = foldl' f forest [1..4] + where f trees _ = findVisibilityOrient (rotate trees) + rotate = (fmap reverse) . transpose + +countVisible :: Forest -> Int +countVisible forest = length $ filter isVisible $ foldl' (++) [] forest + +isVisible :: Tree -> Bool +isVisible (Tree _ v) = v + +treeHeight :: Tree -> Int +treeHeight (Tree h _) = h + +part2 :: Forest -> Int +part2 forest = maximum scores + where nrows = length forest + ncols = length $ head forest + scores = [scenicScore forest r c | r <- [0 .. (nrows - 1)], c <- [0 .. (ncols - 1)]] + +viewDistance :: Int -> [Tree] -> Int +viewDistance h trees = length $ takeUntil (< h) $ fmap treeHeight trees + +takeUntil :: (a -> Bool) -> [a] -> [a] +takeUntil f [] = [] +takeUntil f (x:xs) + | f x == True = x : (takeUntil f xs) + | otherwise = [x] + +tracks :: Forest -> Int -> Int -> [[Tree]] +tracks forest row col = [reverse l, drop 1 r, reverse u, drop 1 d] + where (l, r) = splitAt col (forest !! row) + -- r = drop 1 r' + (u, d) = splitAt row ((transpose forest) !! col) + -- d = drop 1 d' + +scenicScore :: Forest -> Int -> Int -> Int +scenicScore forest row col = foldl' (*) 1 $ fmap (viewDistance h) directions + where directions = tracks forest row col + h = treeHeight $ (forest!!row)!!col + diff --git a/data/advent08.txt b/data/advent08.txt new file mode 100644 index 0000000..d27a06c --- /dev/null +++ b/data/advent08.txt @@ -0,0 +1,99 @@ +202210010310302121322210423201220000314024242432211425434422230130411300321324302223011311211020120 +110110101310322320101034033124303343031143435122351113353455142421341123420341013300312303121102011 +022222013123323313404200430243020022232555432244334344444542122134130133413044342230233130022021112 +112111200212221204100121022244121453412414141145154551512445525421435232112124343223122023223002202 +010223032133232313122112411243113131542422453412413325334141424132125341004123403342031113000321021 +120203310013001113014232132353322341235134523424131353554135433255424414441323410043113010210021012 +001321033020303133033413032514323122112454135451341114535233523552111335315303411442413103332313030 +102233001100013210130402551555541521113335452154222626521451545151431543531541120332311401123233002 +000012220223113031313123321342142243351454565666666364326432622135251241345515402410441240003203231 +323120110213212212143353534544223242346225556633526325233365442515315454452135521422031032430131220 +010220310331203330225142431325531126553665336562254653354425352265653122233143242323103214302312331 +123101224243131422134414233423334544425633524646243233362664535663664554135351555324200233211032302 +100333303112401243331523125333225424454222662442543663456556432565564235231542334341022204140131010 +222320010441120114425514454524226565533325532623432265362636256446244663333511523325400304340133002 +001134131023140453514341254322262544543344626564433544326342336243252564355555514153411041322321023 +303032023104304131222142562326266256323662554334643363347342242532462534263352425422433133134401332 +020340310211345254242454444265454332565537636676554374476536357464242334364525332231411442341304333 +010424244214451151434435346345243633344375647763655474336663454733535632266334124355143530400222002 +020104234232554141451622326322264263647735447654367457344444465643453634336365231214525124433022444 +204201130004331123325262634265424363534754356365344543544653337444375665555552553542414222233041401 +231313304412112545143543446626577766674444573674633355336777737535543536546633462414421512204234400 +033341420245341341444352655236656744666536473763766674637573737455777566262423256254341313453322144 +440314332142235331442343546273646373345664657448647878767773563537357677642454354424141113554434301 +002021425352244536243253565466455346736677674884448677865868476765656674742534363462451133433430431 +312244242113113354564554337535376574767786648687855878644777578636677645574425532235315311343242411 +233122245155144565255556333557536764847587685667467874688477765747736733566463344232665353434242041 +022220452513455555322662755373537664785775876754758887458644778857853773647663236626545124422424320 +022230332331242525645255575733445554454765688768654845485674575556756764377537266433342513111411334 +120341111522144552663266354477644845848748677677644558776677456646677433474757754324325455254224404 +334433112533124466362636446457755674575647767878689757997654655557447467734646643623663324241123341 +044044511253635465446534354447476746444884889989857597797866787787888747443665545532456524224131143 +342122313235634465626773753655655586857767998568657675697899684787888655564663564432663444345124541 +300241415543242222674376454758578748859965896985789659878795778758468578856474655622353223553421340 +432555555434536525465544664745745888686778996656856565599757587746755745784533654644233622425515434 +004155142463225235434575577777764766596665569657879795765857577596467458644447634453564363532531433 +315114345132332442746733438647847847577597775567596889586878967997655654786546753744554456313434213 +323554311133626266375345655745865485788569878887677897756998589576556656784677663746632663332532151 +315144132232553537533354554654464598875898876799867967877875556975854576455736577735632354543155134 +222334432553634533666733477687556985579999867776999997898668878678867858466445453753652435255552353 +341334452524465435553736485655557568559699788977996667669999667955587886767744364375524653661522332 +413445246252544336676458575775678988988586687776978669888789889868875866467477435474464653535223233 +113143446226365337646448856475467575668778966797986666789967987866989544578754333667436355422314512 +323412424644366374665378846687555957879987766978769867869789986867696554454854656763756625325241145 +323421115545525643633547858847889999697776668979678976769897999899659875558684777565446323544324251 +432334334444542573375354656474858797778897676698987797799688968867795956877875445655545264424345232 +313421526625464654745784667555655877667866889997977998979899968958766968688884745767745342336522224 +122211264264562763544488484689975989588788879988798787779667786859558567878485674347334443246223344 +255242155636622443335485578658568685878777677999787778898767686987558788876846566435373364366424533 +413344156256433653574768654788878777978699969878998798887869869888598888664446534547775236363642425 +342124563455467737457658485456878998989988979899998897787987866875698669856575853463673655444534341 +232551266232426674334688468579688769898979978897788788899798868699975575444474737764445664236253252 +525112465464323755636656578776889996878689879787788998889987978986957695855668736377766252323353255 +335335445554653644563344554685858987978878788987977879778999967667869567488847763473767565562443553 +545321524224436644545685678886975585679878668999989988977889988665666777667584866437364423354233323 +111423266342443746543667554476875575666977668887778798797767868895568959486564657554473632445655344 +524212552355364477347365556488688656986668766897897789886896688988559599878748555366373562653454121 +131154335433422345634645655457568556886876787789779979987866669967997578577754745577462646226445314 +235252452335234564367678476887687995897898668678778786687699867598786667847747567563645652263343425 +223253143456366656767765746865557567566666767869789987989986887557956998685474637637366352326243542 +022325555334456567353758847454499775865697766886666678799897866988858868887447554444762544422112415 +343144416534333377537365468647569967688667888678877876698688967696987958488474576533553236464222515 +041455413553636645474356566857777797955967797767896696888698558569799657885773667663642263333432225 +314232125326436446553663846575645898685777676969798987998978856859686786577445377553744443245432243 +321243223546455364367743474758777659578658777997668897788989566685788888684476563345423344554443231 +415125254422442527666573645787574596577688978657789966586986685976985678486637347667436525212454543 +414235135545633363666356438757467669858969857687755898578857676858688644547753747345432366312551541 +440114242344345546336647565757454586855999757667866776678779875958488554786743465373523322335324440 +122344413244444335537455544748467554578788577997879979696958696984755857587433556336643245424414511 +404152532353256326373573456764878788486665786786686989885788858787846674744354647343463555324212243 +430052142415544522224767753334887658877858987557899587585858774486467688656755647656562452215444341 +340214451142655354335334667467484748684647966577756797796876564684745486774456744624442332555544214 +202003423413136555225554443743364668647485745965556658974868564455687766677646333424563244225312413 +124244252232546632354435547477575454458474888785648855588868547656747434644644562666356425513552234 +110004343225254552232245333753757468577454458667486664868686776847774656744433232645435354515511001 +114110243523312246646633667657446557887764577875444674586445668855646455464673426355246121241420242 +144004022115232335325233544457463354556577558686866645777668486456344336776563552654224535341204000 +421204313533552465364335665657756377478687485747568464457586578433533665377544334436324115345110242 +310213102455533546266535624345334376363684858775467657685555747353643644472546632522545411333203111 +114430325255224412546642432664757677554453367648667474866563366653547673545643556525445241221014044 +101202024124311141332246625344457736763773743567676364434654335437667673333633334533321141144401343 +022314412343141552545426345523734644465534437755555546366776455573555736346566554523431445312041141 +310102320115113413255662622463666645774475764474763576347553464576657544655232265213412332113321240 +103421443124221314341526656425352447567466347367455546655457665443752365626234642233144534440401232 +012242102421432341212413443326266462435353334443757554636776575352564544364326345342233211400021132 +222333320014022434154332424323323222343565375555437333746365455224462465255645335454253531041200430 +203301220423445445245343333566523444245653734664355375366356543225425453566551115535422214404241002 +302324443031122331441331256335344224253636533556756445434643354532564454235413132113211343002133211 +311312211414143323513134241534622563433242365626656422526345223354546364435315451551101023211412130 +222110343401032043553425111312244654332653464666435423342453322663424243153233322113032023202020223 +323230200411432130355525221321433355242633433225442453325522522622364253311533334354031100222010013 +310222312143433114242431355515315554643534324342445656666436353223444231435453241400213321330130010 +333003032313304033243534455111112214234262552344526635435332246231323535122413441024143242102302110 +113023202303413323324054115321135342223354425566256536556255312115424353351151533010221413210330113 +201002101132013010411214525353534241113423251253436544643553152152432455455322331331310031222312310 +010000023323114442204233214145222453523254543111213112525444545451354241122320331344023330301010000 +020221201301204043241340212025231451554512545355544454355334235544331435121232034423404333303220120 +211200133121002321411044312343241534545415435535214551522523534521342510432012433211020132222223110 +001010111333021232114033432123014243143421131445211232544521554534542030434313341020033312233321020 +110102200233103010320200140144034422131352541143533213514525422131220112004103004000011012120121220 \ No newline at end of file diff --git a/data/advent08a.txt b/data/advent08a.txt new file mode 100644 index 0000000..6557024 --- /dev/null +++ b/data/advent08a.txt @@ -0,0 +1,5 @@ +30373 +25512 +65332 +33549 +35390 \ No newline at end of file -- 2.34.1