Skip to content

Instantly share code, notes, and snippets.

@sasa1977
Last active December 13, 2017 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sasa1977/bf9a50d2caeb2534c4e557d37ca5e08a to your computer and use it in GitHub Desktop.
Save sasa1977/bf9a50d2caeb2534c4e557d37ca5e08a to your computer and use it in GitHub Desktop.
defmodule Day13 do
def part1(), do:
scanners() |> severities(0) |> Enum.sum()
def part2(), do:
0
|> Stream.iterate(&(&1+1))
|> Stream.transform(scanners(), &{[severities(&2, &1)], &2})
|> Stream.take_while(&(not Enum.empty?(&1)))
|> Enum.count()
def scanners(), do:
"input.txt"
|> File.stream!()
|> Stream.map(&String.trim/1)
|> Stream.map(fn row -> row |> String.split(": ") |> Enum.map(&String.to_integer/1) end)
|> Enum.map(fn [pos, depth] -> %{pos: pos, depth: depth, interval: 2 * (depth - 1)} end)
def severities(scanners, start_time), do:
scanners
|> Stream.filter(&(rem(start_time + &1.pos, &1.interval) == 0))
|> Stream.map(&(&1.depth * &1.pos))
end
Day13.part1() |> IO.inspect
Day13.part2() |> IO.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment