-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(2017.05-haskell): use Control.Zipper from zippers
- Loading branch information
Showing
3 changed files
with
22 additions
and
31 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 |
---|---|---|
@@ -1 +1 @@ | ||
2023.6.2.15 | ||
2023.6.2.16 |
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
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 |
---|---|---|
@@ -1,58 +1,48 @@ | ||
{-# LANGUAGE StrictData #-} | ||
{-# LANGUAGE TypeOperators #-} | ||
|
||
module AdventOfCode.Year2017.Day05 where | ||
|
||
import AdventOfCode.Input (parseInput) | ||
import AdventOfCode.TH (defaultMainMaybe, inputFilePath) | ||
import AdventOfCode.TH (defaultMain, inputFilePath) | ||
import Control.Lens (over, view) | ||
import Control.Monad (ap) | ||
import Control.Zipper | ||
import Data.List (unfoldr) | ||
import Data.Tuple.Extra (dupe) | ||
import Text.Trifecta (integer, some) | ||
|
||
data ListZipper a = ListZipper [a] a [a] | ||
deriving (Show) | ||
type CPU = Top :>> [Int] :>> Int | ||
|
||
main :: IO () | ||
main = $(defaultMainMaybe) | ||
main = $(defaultMain) | ||
|
||
getInput :: IO [Int] | ||
getInput = parseInput (some (fromInteger <$> integer)) $(inputFilePath) | ||
|
||
partOne :: [Int] -> Maybe Int | ||
example :: [Int] | ||
example = [0, 3, 0, 1, -3] | ||
|
||
partOne :: [Int] -> Int | ||
partOne = solve (+ 1) | ||
|
||
partTwo :: [Int] -> Maybe Int | ||
partTwo :: [Int] -> Int | ||
partTwo = solve updateOffset | ||
where | ||
updateOffset offset | ||
| offset >= 3 = offset - 1 | ||
| otherwise = offset + 1 | ||
|
||
solve :: (Int -> Int) -> [Int] -> Maybe Int | ||
solve updateOffset = fmap (length . iterateMaybe (step updateOffset)) . zipper | ||
solve :: (Int -> Int) -> [Int] -> Int | ||
solve f = length . iterateMaybe (step f) . (fromWithin traverse . zipper) | ||
|
||
example :: [Int] | ||
example = [0, 3, 0, 1, -3] | ||
step :: (Int -> Int) -> CPU -> Maybe CPU | ||
step = ap (move . view focus) . over focus | ||
|
||
step :: (Int -> Int) -> ListZipper Int -> Maybe (ListZipper Int) | ||
step f (ListZipper ls x rs) = move x (ListZipper ls (f x) rs) | ||
|
||
move :: Int -> ListZipper a -> Maybe (ListZipper a) | ||
move :: Int -> CPU -> Maybe CPU | ||
move n = case compare n 0 of | ||
LT -> (!! abs n) . iterate (moveLeft =<<) . Just | ||
LT -> jerks leftward (abs n) | ||
EQ -> Just | ||
GT -> (!! n) . iterate (moveRight =<<) . Just | ||
|
||
moveRight :: ListZipper a -> Maybe (ListZipper a) | ||
moveRight (ListZipper _ _ []) = Nothing | ||
moveRight (ListZipper ls x (r : rs)) = Just (ListZipper (x : ls) r rs) | ||
|
||
moveLeft :: ListZipper a -> Maybe (ListZipper a) | ||
moveLeft (ListZipper [] _ _) = Nothing | ||
moveLeft (ListZipper (l : ls) x rs) = Just (ListZipper ls l (x : rs)) | ||
|
||
zipper :: [a] -> Maybe (ListZipper a) | ||
zipper [] = Nothing | ||
zipper (x : rs) = Just (ListZipper [] x rs) | ||
GT -> jerks rightward n | ||
|
||
iterateMaybe :: (a -> Maybe a) -> a -> [a] | ||
iterateMaybe f x = x : unfoldr (fmap dupe . f) x |