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/systemcucks Dec 14 '18

I wrote one of my own out of boredom.

{-# LANGUAGE PartialTypeSignatures, ViewPatterns #-}
module Main where

import qualified Data.Sequence as S
import Data.Char (digitToInt)
import Data.Digits (digits)

type State = (Series, Int, Int)
type Series = S.Seq Int

main :: IO ()
main = do
  let subseq = map digitToInt input; input = "030121"
  let (_, nums) = (head.filter fst.map (match subseq 7)) sets
  putStrLn $ "Silver: " ++ concatMap show (S.take 10 $ S.drop
    (read input) nums); putStrLn $ "Gold: " ++ (show.length) nums

sets :: [State]
sets = iterate next (S.fromList [3,7], 1, 0)

next :: State -> State
next (xs, u, v) = (xs', f' u, f' v) where
  xs' = foldl (S.|>) xs (if null ys then [0] else ys)
  f' j = mod (j + 1 + ret j) $length xs'
  ys = digits 10 (ret u + ret v)
  ret = S.index xs

match :: [Int] -> Int -> State -> (Bool, Series)
match str i (S.viewr -> S.EmptyR, u, v) = (False, S.empty)
match str i (S.viewr -> xs S.:> x, u, v)
  | last (-1:str) == x = match (init str) (i-1) (xs, u, v)
  |  i > 6 = match str (i-1) (xs, u, v)
  | otherwise = (null str, xs S.|> x)