-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathday_18.ex
More file actions
77 lines (66 loc) · 2.08 KB
/
day_18.ex
File metadata and controls
77 lines (66 loc) · 2.08 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
67
68
69
70
71
72
73
74
75
76
77
defmodule AdventOfCode.Y2015.Day18 do
@moduledoc """
--- Day 18: Like a GIF For Your Yard ---
Problem Link: https://adventofcode.com/2015/day/18
Difficulty: m
Tags: grid map simulation
"""
alias AdventOfCode.Helpers.{InputReader, Transformers}
@rows 100
@cols 100
@corners [{0, 0}, {0, 99}, {99, 0}, {99, 99}]
def input, do: InputReader.read_from_file(2015, 18)
def run(input \\ input()) do
on_set = parse(input)
task_1 = Task.async(fn -> solve(on_set, false) end)
task_2 = Task.async(fn -> solve(on_set, true) end)
{Task.await(task_1), Task.await(task_2)}
end
def parse(data) do
data
|> Transformers.lines()
|> Enum.with_index()
|> Enum.reduce(MapSet.new(), fn {line, r}, acc ->
line
|> String.graphemes()
|> Enum.with_index()
|> Enum.reduce(acc, fn {char, c}, inner_acc ->
if char == "#", do: MapSet.put(inner_acc, {r, c}), else: inner_acc
end)
end)
end
defp solve(on_set, corners_sticky?) do
on_set = if corners_sticky?, do: MapSet.union(on_set, MapSet.new(@corners)), else: on_set
1..100
|> Enum.reduce(on_set, fn _, current_on ->
# Step 1: Count neighbors for all "interesting" cells
neighbor_counts =
Enum.reduce(current_on, %{}, fn {r, c}, counts ->
Enum.reduce(neighbors(r, c), counts, fn pos, acc ->
Map.update(acc, pos, 1, &(&1 + 1))
end)
end)
# Step 2: Apply GOL rules
new_on =
Enum.reduce(neighbor_counts, MapSet.new(), fn {pos, count}, acc ->
cond do
count == 3 -> MapSet.put(acc, pos)
count == 2 and MapSet.member?(current_on, pos) -> MapSet.put(acc, pos)
true -> acc
end
end)
if corners_sticky?, do: MapSet.union(new_on, MapSet.new(@corners)), else: new_on
end)
|> MapSet.size()
end
defp neighbors(r, c) do
for dr <- -1..1,
dc <- -1..1,
not (dr == 0 and dc == 0),
nr = r + dr,
nc = c + dc,
nr >= 0 and nr < @rows and nc >= 0 and nc < @cols do
{nr, nc}
end
end
end