-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
14 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,28 +22,28 @@ Maintainer : [email protected] | |
81 | ||
-} | ||
module Main where | ||
module Main (main) where | ||
|
||
import Advent (arrIx, getInputArray) | ||
import Advent (arrIx, getInputArray, ordNub) | ||
import Advent.Coord (Coord, cardinal) | ||
import Advent.Search (dfs) | ||
import Data.Array.Unboxed (UArray, (!), assocs) | ||
import Control.Monad (guard) | ||
|
||
-- | >>> :main | ||
-- 778 | ||
-- 1925 | ||
main :: IO () | ||
main = | ||
do input <- getInputArray 2024 10 | ||
print (part1 input) | ||
print (part2 input) | ||
let paths = [pathsFrom input start | (start, '0') <- assocs input] | ||
print (length (concatMap ordNub paths)) | ||
print (length (concat paths)) | ||
|
||
part1 :: UArray Coord Char -> Int | ||
part1 a = length [() | (start, '0') <- assocs a, end <- dfs step start, a!end == '9'] | ||
where | ||
step x = [y | y <- cardinal x, h <- arrIx a y, succ (a ! x) == h] | ||
|
||
part2 :: UArray Coord Char -> Int | ||
part2 a = length [() | (start, '0') <- assocs a, (end, _) <- dfs step (start, []), a!end == '9'] | ||
where | ||
step (x, xs) = [(y, x:xs) | y <- cardinal x, h <- arrIx a y, succ (a!x) == h] | ||
pathsFrom :: UArray Coord Char -> Coord -> [Coord] | ||
pathsFrom a i | ||
| a!i == '9' = [i] | ||
| otherwise = do j <- cardinal i | ||
h <- arrIx a j | ||
guard (succ (a!i) == h) | ||
pathsFrom a j | ||
|