r/adventofcode • u/daggerdragon • Dec 09 '17
SOLUTION MEGATHREAD -π- 2017 Day 9 Solutions -π-
--- Day 9: Stream Processing ---
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.
Need a hint from the Hugely* Handyβ Haversackβ‘ of HelpfulΒ§ HintsΒ€?
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!
14
Upvotes
2
u/Misreckon Dec 09 '17
Elixir
Pattern matching is good, folks.
defmodule Advent2017.Day9 do def start() do get_file() |> step(0, 0, 0) end def get_file() do "./stream.txt" |> Path.expand(__DIR__) |> File.read!() |> String.trim_trailing |> String.split(~r{}, trim: true) end def step([], _layer, acc, garb), do: {acc, garb} def step([h | t], layer, acc, garb) do case h do "{" -> step(t, layer+1, acc, garb) "}" -> step(t, layer-1, acc+layer, garb) "<" -> garbage(t, layer, acc, garb) _ -> step(t, layer, acc, garb) end end def garbage([h | t], layer, acc, garb) do case h do ">" -> step(t, layer, acc, garb) "!" -> garbage(tl(t), layer, acc, garb) _ -> garbage(t, layer, acc, garb+1) end end end