-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathday_12.ex
More file actions
54 lines (42 loc) · 1.42 KB
/
day_12.ex
File metadata and controls
54 lines (42 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
defmodule AdventOfCode.Y2021.Day12 do
@moduledoc """
--- Day 12: Passage Pathing ---
Problem Link: https://adventofcode.com/2021/day/12
Difficulty: m
Tags: graph pathfinding recursion traversal
"""
alias AdventOfCode.Helpers.{InputReader, Transformers}
def input, do: InputReader.read_from_file(2021, 12)
def run(input \\ input()) do
graph = parse(input)
p1 = count_paths(graph, "start", MapSet.new(), false)
p2 = count_paths(graph, "start", MapSet.new(), true)
{p1, p2}
end
defp count_paths(_graph, "end", _visited, _allow_double), do: 1
defp count_paths(graph, current, visited, allow_double) do
next_visited = if small?(current), do: MapSet.put(visited, current), else: visited
Yog.neighbors(graph, current)
|> Enum.reduce(0, fn {neighbor, _}, total ->
cond do
neighbor == "start" ->
total
not small?(neighbor) or not MapSet.member?(visited, neighbor) ->
total + count_paths(graph, neighbor, next_visited, allow_double)
allow_double ->
total + count_paths(graph, neighbor, next_visited, false)
true ->
total
end
end)
end
defp small?(node), do: String.downcase(node) == node
def parse(data \\ input()) do
data
|> Transformers.lines()
|> Enum.reduce(Yog.undirected(), fn line, graph ->
[u, v] = String.split(line, "-")
Yog.add_edge_ensure(graph, u, v, 1)
end)
end
end