{-# Language QuasiQuotes #-}
module Main (main) where
import Advent (format)
import Data.Char (digitToInt)
main :: IO ()
IO ()
main =
do [Int]
xs <- (Char -> Int) -> [Char] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map Char -> Int
digitToInt ([Char] -> [Int]) -> IO [Char] -> IO [Int]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [format|2017 1 %s%n|]
Int -> IO ()
forall a. Show a => a -> IO ()
print ([Int] -> Int
part1 [Int]
xs)
Int -> IO ()
forall a. Show a => a -> IO ()
print ([Int] -> Int
part2 [Int]
xs)
parseInput :: String -> [Int]
parseInput :: [Char] -> [Int]
parseInput = (Char -> Int) -> [Char] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map Char -> Int
digitToInt ([Char] -> [Int]) -> ([Char] -> [Char]) -> [Char] -> [Int]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[Char]] -> [Char]
forall a. HasCallStack => [a] -> a
head ([[Char]] -> [Char]) -> ([Char] -> [[Char]]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [[Char]]
lines
part1 :: [Int] -> Int
part1 :: [Int] -> Int
part1 = Int -> [Int] -> Int
solve Int
1
part2 :: [Int] -> Int
part2 :: [Int] -> Int
part2 [Int]
xs = Int -> [Int] -> Int
solve ([Int] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Int]
xs Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2) [Int]
xs
solve :: Int -> [Int] -> Int
solve :: Int -> [Int] -> Int
solve Int
n [Int]
xs = [Int] -> Int
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [ Int
x | (Int
x,Int
y) <- [Int]
xs [Int] -> [Int] -> [(Int, Int)]
forall a b. [a] -> [b] -> [(a, b)]
`zip` Int -> [Int] -> [Int]
forall a. Int -> [a] -> [a]
drop Int
n ([Int] -> [Int]
forall a. HasCallStack => [a] -> [a]
cycle [Int]
xs), Int
x Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
y ]