this post was submitted on 03 Dec 2024
23 points (96.0% liked)

Advent Of Code

985 readers
38 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2024

Solution Threads

M T W T F S S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 1 year ago
MODERATORS
 

Day 3: Mull It Over

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

you are viewing a single comment's thread
view the rest of the comments
[โ€“] [email protected] 4 points 3 weeks ago (3 children)

Haskell

Oof, a parsing problem :/ This is some nasty-ass code. step is almost the State monad written out explicitly.

Solution

import Control.Monad
import Data.Either
import Data.List
import Text.Parsec

data Ins = Mul !Int !Int | Do | Dont

readInput :: String -> [Ins]
readInput = fromRight undefined . parse input ""
  where
    input = many ins <* many anyChar
    ins =
      choice . map try $
        [ Mul <$> (string "mul(" *> arg) <*> (char ',' *> arg) <* char ')',
          Do <$ string "do()",
          Dont <$ string "don't()",
          anyChar *> ins
        ]
    arg = do
      s <- many1 digit
      guard $ length s <= 3
      return $ read s

run f = snd . foldl' step (True, 0)
  where
    step (e, a) i =
      case i of
        Mul x y -> (e, if f e then a + x * y else a)
        Do -> (True, a)
        Dont -> (False, a)

main = do
  input <- readInput <$> readFile "input03"
  print $ run (const True) input
  print $ run id input

[โ€“] [email protected] 2 points 3 weeks ago (2 children)

Love to see you chewing through this parsing problem in Haskell, I didn't dare use Parsec because I wasn't confident enough.
Why did you decide to have a strict definition of Mul !Int !Int?

[โ€“] [email protected] 3 points 3 weeks ago (1 children)

My guess is because a linter and/or HLS was suggesting it. I know HLS used to suggest making your fields strict in almost all cases. In this case I have a hunch that it slightly cuts down on memory usage because we use almost all Muls either way. So it does not need to keep the string it is parsed from in memory as part of the thunk.

But it probably makes a small/negligible difference here.

[โ€“] [email protected] 3 points 3 weeks ago

Yep, HLS suggested it, and I figured since I'm definitely going to be using all of the values (in part one, at least), why not?

Normally I ignore that kind of nitpicky suggestion though.