-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathday_14.ex
More file actions
66 lines (56 loc) · 1.6 KB
/
day_14.ex
File metadata and controls
66 lines (56 loc) · 1.6 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
55
56
57
58
59
60
61
62
63
64
65
66
defmodule AdventOfCode.Y2017.Day14 do
@moduledoc """
--- Day 14: Disk Defragmentation ---
Problem Link: https://adventofcode.com/2017/day/14
Difficulty: m
Tags: graph hash
"""
alias AdventOfCode.Helpers.InputReader
alias AdventOfCode.Y2017.Day10
def input, do: InputReader.read_from_file(2017, 14)
alias Yog.Connectivity
def run(input \\ input()) do
on_set = input |> parse() |> build_grid()
{run_1(on_set), run_2(on_set)}
end
def parse(data \\ input()), do: String.trim(data)
defp run_1(on_set), do: MapSet.size(on_set)
defp run_2(on_set) do
on_set
|> Enum.reduce(Yog.undirected(), fn {r, c} = pos, graph ->
neighbors =
[{r + 1, c}, {r, c + 1}]
|> Enum.filter(&MapSet.member?(on_set, &1))
Enum.reduce(neighbors, Yog.add_node(graph, pos, nil), fn neighbor, acc ->
Yog.add_edge_ensure(acc, pos, neighbor, 1)
end)
end)
|> Connectivity.connected_components()
|> length()
end
defp build_grid(input) do
0..127
|> Task.async_stream(fn r ->
"#{input}-#{r}"
|> Day10.compute_knot_hash()
|> to_bits()
|> Enum.with_index()
|> Enum.filter(fn {bit, _} -> bit == "1" end)
|> Enum.map(fn {_, c} -> {r, c} end)
end)
|> Enum.reduce(MapSet.new(), fn {:ok, row_points}, acc ->
Enum.reduce(row_points, acc, &MapSet.put(&2, &1))
end)
end
defp to_bits(hash) do
hash
|> String.graphemes()
|> Enum.flat_map(fn hex ->
hex
|> String.to_integer(16)
|> Integer.to_string(2)
|> String.pad_leading(4, "0")
|> String.graphemes()
end)
end
end