Day 10
# Note: when making the next template, something like this works well:
# `cat day04.livemd | sed 's/10/04/' > day04.livemd`
# When inspecting lists of numbers, use "charlists: :as_lists"
#
Mix.install([
# Join the string so a copy of dayN to dayM doesn't destroy it.
{:kino, "~> 0.1" <> "4.2"}
])
# Join the string so a copy of dayN to dayM doesn't destroy it.
IEx.Helpers.c("/Users/johnb/dev/2" <> "0" <> "2" <> "4adventOfCode/advent_of_code.ex")
alias AdventOfCode, as: AOC
alias Kino.Input
Installation and Data
input_example = Kino.Input.textarea("Example Data", monospace: true)
input_puzzleInput = Kino.Input.textarea("Puzzle Input", monospace: true)
input_source_select =
Kino.Input.select("Source", [{:example, "example"}, {:puzzle_input, "puzzle input"}])
data = fn ->
(Kino.Input.read(input_source_select) == :example &&
Kino.Input.read(input_example)) ||
Kino.Input.read(input_puzzleInput)
end
Solution
defmodule Day10 do
@start_of_trail 0
def parse(text) do
text
|> AOC.as_grid_of_digits()
end
def original_trailheads(grid, next_value \\ @start_of_trail) do
AOC.grid_cells(grid)
|> Enum.filter(fn cell -> grid[cell] == next_value end)
end
def solve1(text) do
grid = parse(text)
starter_lists = original_trailheads(grid)
|> Enum.map(fn zero -> [zero] end)
part2 = 1..9
|> Enum.reduce(starter_lists, fn next_val, paths ->
paths
|> Enum.reduce([], fn [cell | _rest] = path, new_paths ->
longer_paths = AOC.neighbors4(grid, cell)
|> Enum.filter(fn neighbor -> grid[neighbor] == next_val end)
|> Enum.map(fn neighbor -> [neighbor | path] end)
new_paths ++ longer_paths
end)
end)
IO.puts("Part 2 answer: #{Enum.count(part2)}")
part2
|> Enum.map(fn list -> [List.last(list), List.first(list)] end)
|> Enum.uniq()
|> Enum.count()
end
def solve2(text) do
parse(text)
end
end
# Example:
data.()
|> Day10.solve1()
|> IO.inspect(label: "\n*** Part 1 solution (example: 36)")
# 629
# data.()
# |> Day10.solve2()
# |> IO.inspect(label: "\n*** Part 2 solution (example: 81)")
# 1242