-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Anup Kishore
committed
Feb 23, 2018
1 parent
2094b18
commit 0f8bf3b
Showing
2 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
{-# LANGUAGE NamedFieldPuns #-} | ||
|
||
module Main where | ||
|
||
import qualified Data.Sequence as Seq | ||
import qualified Data.Set as Set | ||
import Data.Set (Set) | ||
import Paths_aoc2017 | ||
import System.Environment (getArgs) | ||
import System.Exit (exitSuccess) | ||
|
||
file :: IO FilePath | ||
file = getDataFileName "data/6-1" | ||
|
||
getData :: IO [Int] | ||
getData = map read . words <$> (readFile =<< file) | ||
|
||
usage :: IO () | ||
usage = putStrLn "Usage: stack exec day-n -- [--two] [...args]" | ||
|
||
data St = St | ||
{ seen :: Set [Int] | ||
, jumps :: Int | ||
, blocks :: [Int] | ||
} | ||
|
||
initSt :: [Int] -> Either St St | ||
initSt blocks = Left St {seen = Set.singleton blocks, jumps = 0, blocks = blocks} | ||
|
||
maxWithIndex | ||
:: (Ord b) | ||
=> [b] -> (b, Int) | ||
maxWithIndex = maximum . flip zip [0 ..] | ||
|
||
intoList | ||
:: Foldable b | ||
=> b a -> [a] | ||
intoList = foldr (:) [] | ||
|
||
{- | ||
- List of numbers | ||
- set ix of max to 0 | ||
- update all by (div max 16) | ||
- update take (rem max 16) . drop ix $ cycle [0..16] | ||
- [0 1 2 3 4 5 6] | ||
- 6 | ||
- ix - (7 - 6) | ||
-} | ||
perform' :: St -> St | ||
perform' st@St {blocks} = | ||
let (m, ix) = maxWithIndex blocks | ||
(d, r) = m `divMod` 16 | ||
updater :: Int -> (Int -> Int) | ||
updater i | ||
| i == ix = const d | ||
| i > ix && i <= (ix + r) = (+) $d + 1 | ||
| i < ix && i <= ix - (16 - r) = (+) $d + 1 | ||
| otherwise = (+ d) | ||
in st {blocks = map (uncurry ($)) . flip zip blocks $ map updater [0 .. 16]} | ||
|
||
check :: St -> Either St St | ||
check St {seen, jumps, blocks} = | ||
if Set.member blocks seen | ||
then Right St {seen, blocks, jumps} | ||
else Left St {seen = Set.insert blocks seen, jumps = jumps + 1, blocks = blocks} | ||
|
||
perform :: Either St St -> Int | ||
perform = either (perform . check . perform') jumps | ||
|
||
doOne :: [String] -> IO Int | ||
doOne _ = perform . initSt <$> getData | ||
|
||
doTwo :: [String] -> IO Int | ||
doTwo _ = undefined <$> getData | ||
|
||
parse :: [String] -> IO () | ||
parse ("--two":xs) = print =<< doTwo xs | ||
parse ["--help"] = usage | ||
parse xs = print =<< doOne xs | ||
|
||
main :: IO () | ||
main = do | ||
args <- getArgs | ||
parse args | ||
exitSuccess |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,9 @@ executables: | |
day-5: | ||
<<: *fast-exe | ||
source-dirs: day5 | ||
day-6: | ||
<<: *base-exe | ||
source-dirs: day6 | ||
|
||
|
||
tests: | ||
|