From 36c1d3c758815b5f3a62d4b527bddbce8805101d Mon Sep 17 00:00:00 2001 From: Nat Lasseter Date: Fri, 6 Dec 2024 18:14:05 +0000 Subject: =?UTF-8?q?[Day=2002]=20Part=202:=20brute=20force,=20na=C3=AFve,?= =?UTF-8?q?=20slow.=20I'm=20sure=20it=20would=20get=20there=20eventually..?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day06/day06.hs | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) (limited to 'day06/day06.hs') diff --git a/day06/day06.hs b/day06/day06.hs index fa022f7..3f11fed 100644 --- a/day06/day06.hs +++ b/day06/day06.hs @@ -67,16 +67,42 @@ el ?: arr route :: Grid -> [Coo] -> Guard -> [Coo] route grid seen guard@(coo, dir) - | nguard == Nothing = coo ?: seen - | otherwise = route grid (coo ?: seen) (fromJust nguard) + | nguard == Nothing = coo ?: seen + | otherwise = route grid (coo ?: seen) (fromJust nguard) where nguard = step grid guard +route2 :: Grid -> [Guard] -> Bool +route2 grid seen@(guard:_) + | nguard == Nothing = False + | (fromJust nguard) `elem` seen = True + | otherwise = route2 grid ((fromJust nguard):seen) + where + nguard = step grid guard + +putObs :: Grid -> Coo -> Grid +putObs grid (r, c) = + let (br,tr:ar) = splitAt r grid + (bc,_:ac) = splitAt c tr in + br ++ + [bc ++ [Obs] ++ ac] ++ + ar + part1 :: Grid -> Guard -> Int -part1 grid guard = length $ route grid [] guard +part1 grid guard = + length $ route grid [] guard + +part2 :: Grid -> Guard -> Int +part2 grid guard = + length $ + filter (route2A . putObsA) $ + route grid [] guard + where + route2A = (flip route2) [guard] + putObsA = putObs grid main = do file <- readFile "day06.input" let (grid, guard) = consume file putStrLn ("Part 1: " ++ (show $ part1 grid guard)) - --putStrLn ("Part 2: " ++ (show $ part2 lns)) + putStrLn ("Part 2: " ++ (show $ part2 grid guard)) -- cgit v1.2.1