{-# Language ImportQualifiedPost, QuasiQuotes #-}
module Main where
import Data.List (transpose)
import Data.Maybe (mapMaybe)
import Advent (chunks, counts, format, partialSums)
import Advent.Coord (Coord, origin, north, east, south, west, charToVec)
main :: IO ()
IO ()
main =
do String
input <- [format|2015 3 (^|v|<|>)*!%n|]
let directions :: [Coord]
directions = (Char -> Maybe Coord) -> String -> [Coord]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Char -> Maybe Coord
charToVec String
input
Int -> IO ()
forall a. Show a => a -> IO ()
print (Int -> [Coord] -> Int
countHouses Int
1 [Coord]
directions)
Int -> IO ()
forall a. Show a => a -> IO ()
print (Int -> [Coord] -> Int
countHouses Int
2 [Coord]
directions)
countHouses :: Int -> [Coord] -> Int
countHouses :: Int -> [Coord] -> Int
countHouses Int
n =
Map Coord Int -> Int
forall a. Map Coord a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (Map Coord Int -> Int)
-> ([Coord] -> Map Coord Int) -> [Coord] -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Coord] -> Map Coord Int
forall (f :: * -> *) a. (Foldable f, Ord a) => f a -> Map a Int
counts ([Coord] -> Map Coord Int)
-> ([Coord] -> [Coord]) -> [Coord] -> Map Coord Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Coord] -> [Coord]) -> [[Coord]] -> [Coord]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap [Coord] -> [Coord]
forall a. Num a => [a] -> [a]
partialSums ([[Coord]] -> [Coord])
-> ([Coord] -> [[Coord]]) -> [Coord] -> [Coord]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[Coord]] -> [[Coord]]
forall a. [[a]] -> [[a]]
transpose ([[Coord]] -> [[Coord]])
-> ([Coord] -> [[Coord]]) -> [Coord] -> [[Coord]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [Coord] -> [[Coord]]
forall a. Int -> [a] -> [[a]]
chunks Int
n