From ee4f6772c5edf48e083e2bedd05ea2403f1887ba Mon Sep 17 00:00:00 2001 From: Amneesh Singh Date: Fri, 20 Dec 2024 20:53:42 +0530 Subject: [PATCH] day 20 Signed-off-by: Amneesh Singh --- aoc2024.cabal | 6 ++++++ src/Day20.hs | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 src/Day20.hs diff --git a/aoc2024.cabal b/aoc2024.cabal index 49eeece..6d255b4 100644 --- a/aoc2024.cabal +++ b/aoc2024.cabal @@ -152,3 +152,9 @@ executable day19 , array , containers , libaoc + +executable day20 + import: common + hs-source-dirs: src + main-is: Day20.hs + build-depends: containers diff --git a/src/Day20.hs b/src/Day20.hs new file mode 100644 index 0000000..52fb330 --- /dev/null +++ b/src/Day20.hs @@ -0,0 +1,56 @@ +module Main where + +import qualified Data.IntMap as IM +import qualified Data.Map as M + +type Coord = (Int, Int) + +type Grid = M.Map Coord Char + +type Path = IM.IntMap Coord + +manhattan :: Coord -> Coord -> Int +manhattan (x1, y1) (x2, y2) = abs (x2 - x1) + abs (y2 - y1) + +neighbours :: Coord -> [Coord] +neighbours (x, y) = [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)] + +race :: Grid -> Coord -> Path +race grid = IM.fromList . zip [0 ..] . path (-1, -1) + where + path :: Coord -> Coord -> [Coord] + path prev c = case ch of + 'E' -> [c, next] + '.' -> c : path c next + where + [(next, ch)] = + [ (n, ch) + | n <- neighbours c, + n /= prev, + let Just ch = n `M.lookup` grid, + ch /= '#' + ] + +nCheats :: Path -> Int -> Int +nCheats path n = + let l = IM.size path + in length + [ () + | i <- [0 .. l - 1], + j <- [i + 100 .. l - 1], + let d = manhattan (path IM.! i) (path IM.! j), + d <= n, + 100 <= j - i - d + 1 + ] + +main :: IO () +main = + do + raw <- lines <$> readFile "./inputs/day20.in" + + let [start] = [(x, y) | (y, row) <- zip [0 ..] raw, (x, ch) <- zip [0 ..] row, ch == 'S'] + grid = M.fromList [((x, y), ch) | (y, row) <- zip [0 ..] raw, (x, ch) <- zip [0 ..] row] + path = race grid start + + putStr "Part 1: " >> print (nCheats path 2) + putStr "Part 2: " >> print (nCheats path 20)