-
-
Save hallski/69f77d1d416f16f048149c89324e275a to your computer and use it in GitHub Desktop.
Advent of Code: Day 10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule AdventOfCode.Day10 do | |
@data_dir Path.expand("../data", __DIR__) | |
def run() do | |
@data_dir | |
|> Path.join("day10.txt") | |
|> File.read!() | |
|> String.split("\n", trim: true) | |
|> Enum.map(&String.to_integer/1) | |
|> add_socket_and_device() | |
end | |
def add_socket_and_device(input), do: Enum.concat(input, [0, Enum.max(input) + 3]) | |
# ------ | |
# Part 1 | |
# ------ | |
def run1() do | |
run() | |
|> Enum.sort() | |
|> build() | |
|> checksum() | |
end | |
def checksum(%{jumps: %{1 => one, 3 => three}}), do: one * three | |
def build([socket | adapters]), do: build(%{seq: [socket], jumps: %{}}, adapters) | |
def build(%{} = result, []), do: %{result | seq: Enum.reverse(result.seq)} | |
def build(%{} = acc, [head | tail]) do | |
add_adapter(acc, head) | |
|> build(tail) | |
end | |
def add_adapter(%{seq: [last | _], jumps: jumps} = data, adapter) | |
when adapter - last <= 3 do | |
jumps = Map.update(jumps, adapter - last, 1, fn v -> v + 1 end) | |
%{data | seq: [adapter | data.seq], jumps: jumps} | |
end | |
# ------ | |
# Part 2 | |
# ------ | |
def run2() do | |
run() | |
|> Enum.sort(:desc) | |
|> variations() | |
end | |
def variations([head | tail]), do: variations(tail, [{head, 1}]) | |
# Return the accumulated value | |
def variations([], [{_, v} | _]), do: v | |
def variations([head | tail], collector) do | |
value = | |
collector | |
|> Enum.filter(fn {v, _} -> v - head <= 3 end) | |
|> Enum.reduce(0, fn {_, branches}, acc -> acc + branches end) | |
variations(tail, [{head, value} | collector]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment