From 9b187ca14b71d88b72857ecc2948fb08f763ebd3 Mon Sep 17 00:00:00 2001 From: pingu Date: Tue, 10 Dec 2024 08:14:57 +0100 Subject: [PATCH] 10 part 1 done --- aoc.cabal | 1 + app/10.hs | 25 +++++++++++++++++++++---- inputs/10 | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 inputs/10 diff --git a/aoc.cabal b/aoc.cabal index 414fd52..003af77 100644 --- a/aoc.cabal +++ b/aoc.cabal @@ -112,6 +112,7 @@ executable 10 -- other-extensions: build-depends: base ^>=4.18.2.1 , split + , matrix hs-source-dirs: app default-language: GHC2021 executable 11 diff --git a/app/10.hs b/app/10.hs index 6aac836..20a7fba 100644 --- a/app/10.hs +++ b/app/10.hs @@ -2,14 +2,31 @@ module Main where import Data.Functor +import Data.Char +import Data.Matrix hiding (trace) +import Data.List -parse = undefined +parse :: String -> Matrix Int +parse = fromLists . ((digitToInt <$>) <$>) . lines -solve1 = undefined +getAllDigit :: Int -> Matrix Int -> [(Int,Int)] +getAllDigit c s = zip [1..] (toLists s) >>= \(y,l) -> zip [1..] l >>= \(x,c') -> + if c == c' then pure (y,x) else mempty + +getAbove :: (Int,Int) -> Matrix Int -> [(Int,Int)] +getAbove (y,x) s = let target = getElem y x s + 1 in + filter ((Just target ==) . flip (uncurry safeGet) s) [(y-1,x),(y+1,x),(y,x-1),(y,x+1)] + +travel :: (Int,Int) -> Matrix Int -> [(Int,Int)] +travel p s = let curr = uncurry getElem p s in + if curr == 9 then [p] else getAbove p s >>= (`travel` s) + +solve1 :: Matrix Int -> Int +solve1 s = length . concatMap (nub . (`travel` s)) $ getAllDigit 0 s solve2 = undefined main :: IO () main = readFile "inputs/10" <&> parse >>= \i -> - print (solve1 i) >> - print (solve2 i) + print (solve1 i) + -- >> print (solve2 i) diff --git a/inputs/10 b/inputs/10 new file mode 100644 index 0000000..262eee8 --- /dev/null +++ b/inputs/10 @@ -0,0 +1,52 @@ +5678543238765121087216787432128921016563210454323433 +4169654119654037892107894549017430457654782369412812 +3018767001843246543210787678186543368945691078908903 +2189678914980158934345698187098901275632100165001894 +3434589125678767891016521096167872984789321874112765 +7823678034569876780123434545252766543215476903233456 +6910167012423495679654321032341057630306985012945101 +5401986543410584508765678211352348921437894123876298 +4323877892543673210104569300235496530890123434565387 +3210967871652598789213075410145687432765210123432456 +0101454960701659654322186703456710101854321076501201 +1012343459898741023763099812769823478923212987655652 +4323012321101232212854210129876012569012503234234743 +5011001410765540101940110238787112876503414105109892 +6722340543894691001233221045698108976434565696589321 +9821059652023782317874536789587012885325676787678780 +9778968701110678498965945893496543494018989801210690 +8769878889231589567767893672109824323561234892309541 +1454399998342490430850104589678012012370348763458032 +0141287107654321521943213212565503123987659654567123 +1230236256565410101237870101419654598543210508948910 +2340145348978321253210965432308765687696501417654323 +3986501567899010344789876323219454766787432321078890 +5677432450987432455670189410012349845696501434569701 +0548901321976501964563208231987210036723109621435652 +1239678100823457877854010145676210129814918730898543 +4310369234710165756905423210122305610905825623567678 +3423456765600874543814554761201456723876734514410569 +2102109894321984562323669854398556894985232305320438 +3076521003965433871234778034567647895894101476761023 +4585412312870122930129872129867634456783230585872310 +5694303456781211123123401256798521065656187694965432 +6783201019892100054076540346543438980521090123456501 +0340112387653456963981639457832523671439876101219602 +1258903490144327870190128768901214512112725614308712 +2569876589231016983278787434980108903003014525456893 +1876767676546781074349896523076587654394523496327854 +0945778789035690165234765014123496543487621087018969 +1239869876124789234105676543287017012567634589829678 +9018756765453210785411085010196528921298565676543549 +8723605034301305696532398923483437430143476589762138 +7634514125212154387783437654589016521052187679854023 +6501323896521093210598568943078723632367098512763210 +7432454787436789765677678732189654045458963403892001 +8901343474345658894389589823476902102167872354301123 +7623676543216743001210438214567813458054961065689054 +5012389012109812123309321301054324569123452876548765 +4589438729001901014578010102344567078212556989231076 +3676567438782342145665430230133898167901665476102989 +2109652145699654398765421321212101237810174345603478 +1078743034328765219876323478903894376521089298714565 +2167634145410898701101210569816765489432100107623434