/
part1.ex
65 lines (58 loc) · 1.35 KB
/
part1.ex
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
defmodule Advent.Y2020.Day24.Part1 do
def run(puzzle) do
puzzle
|> parse()
|> Enum.reduce(%{}, &follow_path/2)
|> count(:black)
end
def parse(puzzle) do
for line <- String.split(puzzle, "\n") do
line
|> String.graphemes()
|> Enum.chunk_while(
"",
fn
c, acc when c in ~w(e w) -> {:cont, acc <> c, ""}
c, acc when c in ~w(s n) -> {:cont, acc <> c}
end,
fn acc -> {:cont, acc} end
)
end
end
def follow_path(path, tiles) do
position =
Enum.reduce(path, {0, 0}, fn dir, {x, y} ->
{move_x, move_y} = moves() |> Map.get(dir)
{x + move_x, y + move_y}
end)
flip_tile(tiles, position)
end
def moves do
%{
"e" => {2, 0},
"w" => {-2, 0},
"se" => {1, -1},
"sw" => {-1, -1},
"ne" => {1, 1},
"nw" => {-1, 1}
}
end
def flip_tile(tiles, position) do
tiles
|> Map.update(position, :black, fn
:black -> :white
:white -> :black
end)
|> init_neighbors(position)
end
def init_neighbors(tiles, {x, y}) do
moves()
|> Map.values()
|> Enum.reduce(tiles, fn {shift_x, shift_y}, tiles ->
Map.put_new(tiles, {x + shift_x, y + shift_y}, :white)
end)
end
def count(tiles, color) do
tiles |> Map.values() |> Enum.count(&(&1 == color))
end
end