haskell: 2015 03

This commit is contained in:
Maciej Jur 2023-10-11 18:56:36 +02:00
parent e79d03a7d9
commit a9e4dc50dc
Signed by: kamov
GPG key ID: 191CBFF5F72ECAFD
5 changed files with 43 additions and 10 deletions

1
2015/.inputs/03.txt Normal file

File diff suppressed because one or more lines are too long

View file

@ -2,7 +2,7 @@ module Main where
import Text.Printf (printf)
import qualified Day01
import qualified Day03
readDay :: Int -> IO String
readDay n = readFile $ getPath n
@ -12,7 +12,7 @@ readDay n = readFile $ getPath n
main :: IO ()
main = do
content <- readDay 1
let parsed = Day01.parse content
print $ Day01.solve1 parsed
print $ Day01.solve2 parsed
content <- readDay 3
let parsed = Day03.parse content
print $ Day03.solve1 parsed
print $ Day03.solve2 parsed

View file

@ -19,7 +19,9 @@ maintainer: maciej@kamoshi.org
extra-source-files: CHANGELOG.md
library
exposed-modules: Day01
exposed-modules:
Day01
Day03
-- Modules included in this library but not exported.
-- other-modules:

34
2015/haskell/src/Day03.hs Normal file
View file

@ -0,0 +1,34 @@
module Day03 (parse, solve1, solve2) where
import Data.List (nub, partition)
data Move = L | R | U | D deriving Show
parse :: String -> [Move]
parse = map mapper
where
mapper c = case c of
'<' -> L
'>' -> R
'^' -> U
'v' -> D
findPath :: [Move] -> [(Int, Int)]
findPath = scanl move (0, 0)
where
move (x, y) dir = case dir of
L -> (x-1, y)
R -> (x+1, y)
U -> (x, y+1)
D -> (x, y-1)
solve1 :: [Move] -> Int
solve1 = length . nub . findPath
solve2 :: [Move] -> Int
solve2 moves =
let (xs, ys) = partition (even . fst) $ zip [0..] moves
in length . nub $ findPath' xs <> findPath' ys
where
findPath' = findPath . map snd

View file

@ -1,4 +0,0 @@
module MyLib (someFunc) where
someFunc :: IO ()
someFunc = putStrLn "someFunc"