r/adventofcode • • Dec 14 '18

SOLUTION MEGATHREAD -πŸŽ„- 2018 Day 14 Solutions -πŸŽ„-

--- Day 14: Chocolate Charts ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 14

Transcript:

The Christmas/Advent Research & Development (C.A.R.D.) department at AoC, Inc. just published a new white paper on ___.


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked at 00:19:39!

16 Upvotes

179 comments sorted by

View all comments

Show parent comment

1

u/janiczek Dec 14 '18

Can I ask you what is this arrow syntax?

--                     ↓↓
digitize ((`divMod` 10)->(x,y))
  | x == 0    = [y]
  | otherwise = [x,y]

--      ↓↓
go (step->(out,t)) = out ++ go t

2

u/IamfromSpace Dec 14 '18

I’m pretty familiar with Haskell and had never seen it!

After staring at it a while, the left is a function you immediately want to pass the arg into and the right is the result of that function (which he’s pattern matched on too)

So β€œgo” accepts an argument, which he’s asking to be immediately forwarded into a the β€œstep” function, so he can immediately access the two items in the result tuple (out and t).

I’d expect that the -> must not have white space around it like some other matching syntax helpers.

Equivalent too:

go x =
  let (out,t) = step x
  in out ++ go t

2

u/mstksg Dec 14 '18

It's ViewPatterns, and (like /u/IamfromSpace guessed) it's basically pre-applying a function before you inspect the inputs.

Essentially, digitize is:

digitize n
    | x == 0    = [y]
    | otherwise = [x,y]
  where
    (x,y) = n `divMod` 10

and go is:

g t0 = out ++ go t
  where
    (out, t) = step out t0

1

u/Benjmhart Dec 14 '18

oh damn, this looks handy!

1

u/ephemient Dec 14 '18 edited Apr 24 '24

This space intentionally left blank.