haskell: 2015 03
This commit is contained in:
parent
e79d03a7d9
commit
a9e4dc50dc
1
2015/.inputs/03.txt
Normal file
1
2015/.inputs/03.txt
Normal file
File diff suppressed because one or more lines are too long
|
@ -2,7 +2,7 @@ module Main where
|
||||||
|
|
||||||
import Text.Printf (printf)
|
import Text.Printf (printf)
|
||||||
import qualified Day01
|
import qualified Day01
|
||||||
|
import qualified Day03
|
||||||
|
|
||||||
readDay :: Int -> IO String
|
readDay :: Int -> IO String
|
||||||
readDay n = readFile $ getPath n
|
readDay n = readFile $ getPath n
|
||||||
|
@ -12,7 +12,7 @@ readDay n = readFile $ getPath n
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
content <- readDay 1
|
content <- readDay 3
|
||||||
let parsed = Day01.parse content
|
let parsed = Day03.parse content
|
||||||
print $ Day01.solve1 parsed
|
print $ Day03.solve1 parsed
|
||||||
print $ Day01.solve2 parsed
|
print $ Day03.solve2 parsed
|
||||||
|
|
|
@ -19,7 +19,9 @@ maintainer: maciej@kamoshi.org
|
||||||
extra-source-files: CHANGELOG.md
|
extra-source-files: CHANGELOG.md
|
||||||
|
|
||||||
library
|
library
|
||||||
exposed-modules: Day01
|
exposed-modules:
|
||||||
|
Day01
|
||||||
|
Day03
|
||||||
|
|
||||||
-- Modules included in this library but not exported.
|
-- Modules included in this library but not exported.
|
||||||
-- other-modules:
|
-- other-modules:
|
||||||
|
|
34
2015/haskell/src/Day03.hs
Normal file
34
2015/haskell/src/Day03.hs
Normal 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
|
|
@ -1,4 +0,0 @@
|
||||||
module MyLib (someFunc) where
|
|
||||||
|
|
||||||
someFunc :: IO ()
|
|
||||||
someFunc = putStrLn "someFunc"
|
|
Loading…
Reference in a new issue