From: Neil Smith Date: Thu, 10 Dec 2020 11:18:09 +0000 (+0000) Subject: Bits of tidying X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=dddb8c74a86a8497ba8fa5cf336820066d796c3a;p=advent-of-code-20.git Bits of tidying --- diff --git a/advent02/src/advent02.hs b/advent02/src/advent02.hs index ce0eeb7..b4a8e43 100644 --- a/advent02/src/advent02.hs +++ b/advent02/src/advent02.hs @@ -41,7 +41,7 @@ matches p = ((pw!!l) == c) /= ((pw!!u) == c) type Parser = Parsec Void Text sc :: Parser () -sc = L.space (skipSome spaceChar) CA.empty CA.empty +sc = L.space space1 CA.empty CA.empty -- sc = L.space (skipSome (char ' ')) CA.empty CA.empty lexeme = L.lexeme sc diff --git a/advent03/src/advent03.hs b/advent03/src/advent03.hs index 777cf7e..5c317a2 100644 --- a/advent03/src/advent03.hs +++ b/advent03/src/advent03.hs @@ -9,38 +9,30 @@ main :: IO () main = do text <- readFile "data/advent03.txt" let (trees, maxCorner) = readGrid text - -- print $ S.size trees - -- print trees - -- print $ maxCorner - -- print $ take 25 $ visitedPlaces (1, 3) maxCorner - -- print $ takeWhile (withinTrees maxCorner) $ visitedPlaces (1, 3) maxCorner + -- print $ trees == rg2 text print $ part1 trees maxCorner print $ part2 trees maxCorner readGrid :: String -> (Trees, Position) -readGrid input = ( S.fromList [ (r, c) - | r <- [0..maxR], c <- [0..maxC] - , (grid!!r)!!c == '#'] - , (maxR, maxC) - ) - where grid = lines input - maxC = (length $ head grid) - 1 - maxR = (length grid) - 1 - +readGrid input = (trees, (maxR, maxC)) + where trees = S.fromList $ concat + [ [(r, c) | (t, c) <- zip row [0..], t == '#'] + | (row, r) <- zip rows [0..] ] + rows = lines input + maxC = (length $ head rows) - 1 + maxR = (length rows) - 1 part1 trees maxCorner = countEncounteredTrees trees maxCorner (1, 3) -part2 trees maxCorner = foldr1 (*) $ map cet [(1, 1), (1, 3), (1, 5), (1, 7), (2, 1)] +part2 trees maxCorner = product $ map cet [(1, 1), (1, 3), (1, 5), (1, 7), (2, 1)] where cet = countEncounteredTrees trees maxCorner countEncounteredTrees trees maxCorner delta = S.size $ S.intersection trees visited where visited = S.fromList $ takeWhile (withinTrees maxCorner) $ visitedPlaces delta maxCorner - visitedPlaces :: Position -> Position -> [Position] visitedPlaces (dr, dc) (_maxR, maxC) = iterate wrappingStep (0, 0) where wrappingStep (r, c) = (r + dr, (c + dc) `mod` (maxC + 1)) withinTrees (maxR, maxC) (r, c) = r >= 0 && r <= maxR && c >= 0 && c <= maxC -