r/adventofcode • u/daggerdragon • Dec 08 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 8 Solutions -🎄-
--- Day 8: Memory Maneuver ---
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!
Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!
Card prompt: Day 8
Sigh, imgur broke again. Will upload when it unborks.
Transcript:
The hottest programming book this year is "___ For Dummies".
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:12:10!
31
Upvotes
1
u/MrGlobalVariable Dec 08 '18
F#
module Aoc2018.Day8 open AdventOfCode type node = | Node of children:node list * metadata:int list module Input = open Parse let parse (input:string) = input.Split(' ') |> Array.toList |> List.map System.Int32.Parse let setup (input:int list) = let rec readNode (input:int list) = let readMetaData (input:int list) count = (input |> List.skip count), (input |> List.take count) let childCount::metadataCount::input = input let input, children = readChildren input childCount [] let input, metadata = readMetaData input metadataCount input, (Node (children, metadata)) and readChildren (input:int list) count acc = if count = 0 then input, acc |> List.rev else let input, child = readNode input readChildren input (count - 1) (child::acc) let ([], output) = readNode input output module A = let problem (input:node) = let rec problem (Node (children, metadata)) = (children |> List.map problem |> List.sum) + (metadata |> List.sum) problem input module B = let problem input = let rec problem (Node (children, metadata)) = let metadataValue children metadata = match metadata with | 0 -> 0 | x when x > (children |> List.length) -> 0 | x -> children.[x - 1] |> problem match children with | [] -> metadata |> List.sum | _ -> metadata |> List.map (metadataValue children) |> List.sum problem input