Add Day 1, Puzzle 2 solution
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
|Day|Stars|Day|Stars|
|
|Day|Stars|Day|Stars|
|
||||||
|---|-----|---|-----|
|
|---|-----|---|-----|
|
||||||
|01 | |07 | |
|
|01 |★ ★ |07 | |
|
||||||
|02 | |08 | |
|
|02 | |08 | |
|
||||||
|03 | |09 | |
|
|03 | |09 | |
|
||||||
|04 | |10 | |
|
|04 | |10 | |
|
||||||
|
|||||||
34
src/Day01.hs
34
src/Day01.hs
@@ -1,22 +1,36 @@
|
|||||||
module Day01
|
module Day01
|
||||||
( day01_1,
|
( day01_1,
|
||||||
|
day01_2,
|
||||||
)
|
)
|
||||||
where
|
where
|
||||||
|
|
||||||
parseInput :: IO [String]
|
parseInput :: IO [String]
|
||||||
parseInput = do
|
parseInput = do
|
||||||
lines <$> readFile "input/day1.txt"
|
lines <$> readFile "input/day1.txt"
|
||||||
|
|
||||||
rotate :: Int -> String -> Int
|
rotate :: Int -> String -> Int
|
||||||
rotate n ('L':xs) = (n - read xs) `mod` 100
|
rotate n ('L' : xs) = (n - read xs) `mod` 100
|
||||||
rotate n ('R':xs) = (n + read xs) `mod` 100
|
rotate n ('R' : xs) = (n + read xs) `mod` 100
|
||||||
rotate n _ = n
|
rotate n _ = n
|
||||||
|
|
||||||
|
rotate' :: Int -> String -> (Int, Int)
|
||||||
|
rotate' 0 ('L' : xs) = (negate (read xs) `mod` 100, (read xs + 100) `div` 100 - 1)
|
||||||
|
rotate' n ('L' : xs) = ((n - read xs) `mod` 100, (read xs - n + 100) `div` 100)
|
||||||
|
rotate' n ('R' : xs) = ((n + read xs) `mod` 100, (read xs + n) `div` 100)
|
||||||
|
rotate' n _ = (n, 0)
|
||||||
|
|
||||||
getPassword :: Int -> Int -> [String] -> Int
|
getPassword :: Int -> Int -> [String] -> Int
|
||||||
getPassword pass 0 [] = pass + 1
|
getPassword pass 0 [] = pass + 1
|
||||||
getPassword pass _ [] = pass
|
getPassword pass _ [] = pass
|
||||||
getPassword pass 0 (x:xs) = getPassword (pass + 1) (rotate 0 x) xs
|
getPassword pass 0 (x : xs) = getPassword (pass + 1) (rotate 0 x) xs
|
||||||
getPassword pass curr (x:xs) = getPassword pass (rotate curr x) xs
|
getPassword pass curr (x : xs) = getPassword pass (rotate curr x) xs
|
||||||
|
|
||||||
|
getPassword' :: Int -> Int -> [String] -> Int
|
||||||
|
getPassword' pass _ [] = pass
|
||||||
|
getPassword' pass curr (x : xs) = getPassword' newPass newVal xs
|
||||||
|
where
|
||||||
|
(newVal, nZero) = rotate' curr x
|
||||||
|
newPass = pass + nZero
|
||||||
|
|
||||||
day01_1 :: IO ()
|
day01_1 :: IO ()
|
||||||
day01_1 = do
|
day01_1 = do
|
||||||
@@ -24,4 +38,12 @@ day01_1 = do
|
|||||||
let result = getPassword 0 50 input
|
let result = getPassword 0 50 input
|
||||||
putStrLn $
|
putStrLn $
|
||||||
"Day 1, Puzzle 1 solution: "
|
"Day 1, Puzzle 1 solution: "
|
||||||
++ show result
|
++ show result
|
||||||
|
|
||||||
|
day01_2 :: IO ()
|
||||||
|
day01_2 = do
|
||||||
|
input <- parseInput
|
||||||
|
let result = getPassword' 0 50 input
|
||||||
|
putStrLn $
|
||||||
|
"Day 1, Puzzle 2 solution: "
|
||||||
|
++ show result
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
module Main (main) where
|
module Main (main) where
|
||||||
|
|
||||||
import Day01 (day01_1)
|
import Day01 (day01_1, day01_2)
|
||||||
import System.Environment (getArgs)
|
import System.Environment (getArgs)
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
@@ -8,8 +8,11 @@ main = do
|
|||||||
args <- getArgs
|
args <- getArgs
|
||||||
case args of
|
case args of
|
||||||
"1" : "1" : _ -> day01_1
|
"1" : "1" : _ -> day01_1
|
||||||
|
"1" : "2" : _ -> day01_2
|
||||||
"1" : _ -> do
|
"1" : _ -> do
|
||||||
day01_1
|
day01_1
|
||||||
|
day01_2
|
||||||
"all" : _ -> do
|
"all" : _ -> do
|
||||||
day01_1
|
day01_1
|
||||||
|
day01_2
|
||||||
_ -> error "Not implemented"
|
_ -> error "Not implemented"
|
||||||
|
|||||||
Reference in New Issue
Block a user