Skip to content

Commit

Permalink
refactor(2017.05-haskell): use Control.Zipper from zippers
Browse files Browse the repository at this point in the history
  • Loading branch information
yurrriq committed Jul 15, 2024
1 parent 6753288 commit 39f85ba
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 31 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2023.6.2.15
2023.6.2.16
3 changes: 2 additions & 1 deletion package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ dependencies:
- text
- trifecta
- unordered-containers
- zippers
default-extensions:
- TemplateHaskell
ghc-options:
Expand Down Expand Up @@ -69,6 +68,7 @@ library:
- utility-ht
- vector
- vector-sized
- zippers
_executable: &executable
ghc-options:
- -O0
Expand Down Expand Up @@ -213,6 +213,7 @@ executables:
main: AdventOfCode.Year2017.Day05
dependencies:
- extra
- zippers
aoc-2018-day01:
<<: *executable
main: AdventOfCode.Year2018.Day01
Expand Down
48 changes: 19 additions & 29 deletions src/AdventOfCode/Year2017/Day05.hs
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

0 comments on commit 39f85ba

Please sign in to comment.