-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathday_24.ex
45 lines (37 loc) · 1.22 KB
/
day_24.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
defmodule AdventOfCode.Y2020.Day24 do
@moduledoc """
--- Day 24: Lobby Layout ---
Problem Link: https://adventofcode.com/2020/day/24
"""
alias AdventOfCode.Helpers.{InputReader, Transformers}
require Integer
def input, do: InputReader.read_from_file(2020, 24)
def run(input \\ input()) do
input = parse(input)
{run_1(input), {:todo, 2}}
end
def run_1(input) do
input
|> flip()
|> black_tiles()
|> length()
end
@directions ~r/e|w|se|sw|ne|nw/
def parse(input) do
for line <- Transformers.lines(input) do
@directions
|> Regex.scan(line, capture: :first)
|> List.flatten()
end
end
defp walk("w", {x, y}), do: {x - 1, y}
defp walk("e", {x, y}), do: {x + 1, y}
defp walk("nw", {x, y}), do: {(Integer.is_even(y) && x) || x - 1, y - 1}
defp walk("ne", {x, y}), do: {(Integer.is_even(y) && x + 1) || x, y - 1}
defp walk("sw", {x, y}), do: {(Integer.is_even(y) && x) || x - 1, y + 1}
defp walk("se", {x, y}), do: {(Integer.is_even(y) && x + 1) || x, y + 1}
defp flip(tiles), do: Enum.map(tiles, fn sides -> Enum.reduce(sides, {0, 0}, &walk/2) end)
defp black_tiles(tiles) do
for {_, tile} <- Enum.frequencies(tiles), rem(tile, 2) == 1, do: tile
end
end