diff options
author | Nat Lasseter <user@4574.co.uk> | 2024-12-10 18:17:47 +0000 |
---|---|---|
committer | Nat Lasseter <user@4574.co.uk> | 2024-12-10 18:17:47 +0000 |
commit | 312ca2cd3e9d307c114ec5cd362f493fcddbead4 (patch) | |
tree | a09ea674bd23d6437e792d9bcca1ec8b34504549 | |
parent | aacd49bf399c3cf1482252ec4e82ef4c19953167 (diff) |
[Day 10] Part 1: Done
-rw-r--r-- | day10/day10.hs | 80 | ||||
-rw-r--r-- | day10/day10.input | 52 |
2 files changed, 132 insertions, 0 deletions
diff --git a/day10/day10.hs b/day10/day10.hs new file mode 100644 index 0000000..5c970e5 --- /dev/null +++ b/day10/day10.hs @@ -0,0 +1,80 @@ +import Data.Char +import Data.List +import Data.Maybe + +type Height = Int +type Coo = (Int, Int) +type Map = [[Height]] + +data Trail = Point Coo Height Trail | Nowhere deriving Show + +(!?) :: [[a]] -> Coo -> Maybe a +mp !? (row, col) + | row < 0 = Nothing + | row >= length mp = Nothing + | col < 0 = Nothing + | col >= (length $ head mp) = Nothing + | otherwise = Just (mp !! row !! col) + +consume :: [Char] -> Map +consume file = + map (map (digitToInt)) $ lines file + +trailheads2 :: [Coo] -> Int -> Int -> [Height] -> [Coo] +trailheads2 acc _ _ [] = + reverse acc +trailheads2 acc r c (h:rest) + | h == 0 = trailheads2 ((r, c):acc) r (c + 1) rest + | otherwise = trailheads2 acc r (c + 1) rest + +trailheads1 :: [Coo] -> Int -> Map -> [Coo] +trailheads1 acc _ [] = + acc +trailheads1 acc r (row:rest) = + trailheads1 (acc ++ heads) (r + 1) rest + where heads = trailheads2 [] r 0 row + +trailheads :: Map -> [Coo] +trailheads = trailheads1 [] 0 + +trails2 :: Map -> Trail -> [Trail] +trails2 mp p@(Point (r,c) h _) = + map pointify $ + catMaybes $ + map oneup + [nxt (-1, 0), nxt (0, -1), nxt (0, 1), nxt (1, 0)] + where + pointify c = Point c (h+1) p + oneup (nr, nc, Just nh) = if nh == h + 1 then Just (nr, nc) else Nothing + oneup (_, _, Nothing) = Nothing + nxt (dr, dc) = (r+dr, c+dc, mp !? (r+dr, c+dc)) + +trails1 :: Map -> [Trail] -> [Trail] -> [Trail] +trails1 _ acc@((Point _ 9 _):_) [] = + acc +trails1 mp acc [] = + trails1 mp [] acc +trails1 mp acc (th@(Point _ 9 _):rest) = + trails1 mp (th:acc) rest +trails1 mp acc (th:rest) = + trails1 mp (acc ++ trails2 mp th) rest + +trails :: Map -> Coo -> [Trail] +trails mp th = + trails1 mp [] [Point th 0 Nowhere] + +score :: [Trail] -> Int +score th = + length $ nub $ map untrail th + where + untrail (Point c _ _) = c + +part1 :: Map -> Int +part1 mp = + sum $ map (score . (trails mp)) $ trailheads mp + +main = do + file <- readFile "day10.input" + let mp = consume file + putStrLn ("Part 1: " ++ (show $ part1 mp)) + --putStrLn ("Part 2: " ++ (show $ part2 mp)) diff --git a/day10/day10.input b/day10/day10.input new file mode 100644 index 0000000..ff85d75 --- /dev/null +++ b/day10/day10.input @@ -0,0 +1,52 @@ +0145677654325490845691012345621876560340100123012398 +1238988998216781932782121234010983401259230034563387 +4323589867205432801543210945125672378768341459654456 +1014678454102301765456901876034561769897652368776301 +6765521543201512350307872962120120850785677879789210 +0896430439810487461217965871298438941454980968654321 +1765012126723596543478014560167569032363211457256762 +2872121035434654302569123441455678121071102340145895 +3961230345985783211098710332334599432980098743232434 +4550145456676890100167000146721087641001217650141325 +9649056765489910651254121035890014550732309654210016 +8738769894676328762343236124301123669845498763227807 +7129787923565439651456347833210008778996787120156998 +1013496014567010340987656944782119211087898031343210 +1012345002198321232876501855693024302332196541034101 +1234567123023450901965432761054435678445087670123256 +0144898454910767845670301622169546589556764581214347 +4343732367867893034981243213678696765698873294309838 +1234351078754702121893214504589787894780987101456789 +0943765469843213210721303698921009683211074560167678 +7856874348765434785630410787652118701202983076298987 +6765989219210125698540521236543025654343892189347567 +5667867804321087582101638347432134509856782345456498 +1054876965691296443492789858901213212790101276012387 +2123965476780125356783498767654300103685230983401456 +3278954587821034219870567658967876234576541092560845 +2567543296930761006721498743478965987655612451076921 +1056230145945852345012345412661234554567803367987830 +2340167034876945123211296401780109693069954298756101 +7887658123985231034500787345691218782178769110343232 +6992349032100112985011216217885011071078978021232349 +5801239844301101676720105606976522362567987876541458 +4321023765432432369838234745983439453454376965430167 +3087610321396565458949549836112378321043105302301298 +2198565410187076327658678921001065439652234211017657 +3233478903216189014547664567632104508701230322928943 +4542369894103273223014523498549812012349821498834012 +7651423765764784132123210210038763676256734567765423 +8960314349845695041012396342121054985109875498656701 +4871005256736786780169487653434873014018766787567892 +5654196149821677893278565694565963223321051096450943 +6743287032120563034567684787696954101438142345321056 +7899180129061432125675893256787845698589233239885469 +3458098938778743210986710143876034787670132178596378 +2167347845609654331235430782932128236101056017687267 +6056256741012103220543521691047659145692347012570167 +7890165432343210110652434598798578036785498743456898 +6784567876758701328701223123623457629876901234347107 +5413218965869232499899810034510256510267892301298256 +4303409954978149581234745218760105100126765410789340 +3212567823019058670365634309451234981237898323870121 +4321016012108767621256565678321015676546767654965432 |