Skip to content

Commit

Permalink
use the new operator stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
glguy committed Dec 19, 2023
1 parent eb0d5ab commit 67a3cc0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
26 changes: 11 additions & 15 deletions solutions/src/2017/08.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import Data.Maybe (fromMaybe)
import Advent (format, maximumMaybe, stageTH)

data C = Cinc | Cdec deriving Show
data O = O_LT | O_GT | O_BANG_EQ | O_EQ_EQ | O_LT_EQ | O_GT_EQ

stageTH

Expand All @@ -43,36 +44,31 @@ stageTH
-- 5347
main :: IO ()
main =
do input <- [format|2017 8 (%s @C %d if %s %s %d%n)*|]
do input <- [format|2017 8 (%s @C %d if %s @O %d%n)*|]
let regmaxes = map (fromMaybe 0 . maximumMaybe) (scanl interpret mempty input)
print (last regmaxes)
print (maximum regmaxes)

-- | Given registers and a statement, compute the resulting registers.
interpret ::
Map String Int {- ^ incoming registers -} ->
(String, C, Int, String, String, Int) {- ^ statement -} ->
(String, C, Int, String, O, Int) {- ^ statement -} ->
Map String Int {- ^ outgoing registers -}
interpret regs (r1,op1,n1,r2,op2,n2)
| toCompare op2 (Map.findWithDefault 0 r2 regs) n2 =
Map.alter (Just . toArith op1 n1 . fromMaybe 0) r1 regs
| otherwise = regs

-- | Convert the string representation of a comparison to a function.
toCompare ::
String {- ^ name -} ->
(Int -> Int -> Bool) {- ^ function -}
toCompare "<" = (< )
toCompare ">" = (> )
toCompare ">=" = (>=)
toCompare "<=" = (<=)
toCompare "!=" = (/=)
toCompare "==" = (==)
toCompare name = error ("Bad comparison function: " ++ name)
toCompare :: O -> Int -> Int -> Bool
toCompare O_LT = (< )
toCompare O_GT = (> )
toCompare O_GT_EQ = (>=)
toCompare O_LT_EQ = (<=)
toCompare O_BANG_EQ = (/=)
toCompare O_EQ_EQ = (==)

-- | Convert the string representation of an arithmetic operation to a function.
toArith ::
C {- ^ name -} ->
(Int -> Int -> Int) {- ^ function -}
toArith :: C -> Int -> Int -> Int
toArith Cinc = (+)
toArith Cdec = subtract
4 changes: 2 additions & 2 deletions solutions/src/2023/19.hs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ accepted workflows xmas = 0 /= acceptedCount workflows (fmap one xmas)
acceptedCount :: Map String ([Rule], String) -> Part Ints -> Int
acceptedCount workflows = jump "in"
where
jump "A" = product . fmap size
jump "R" = const 0
jump "A" {- accept -} = product . fmap size
jump "R" {- reject -} = const 0
jump ((workflows Map.!) -> (rs, el)) = foldr rule (jump el) rs

rule (var, O_GT, n, tgt) continue p =
Expand Down

0 comments on commit 67a3cc0

Please sign in to comment.