Solution to Advent of Code 2018 Day 5 in Elixir

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
defmodule Day5 do
def scan(polymer) do
chars =
polymer
|> String.to_charlist()

res =
chars
|> Enum.reduce({:none, []}, fn
c, {:none, acc} ->
{:prev, c, acc}

c, {:prev, prev, acc} ->
if react?(c, prev) do
{:none, acc}
else
{:prev, c, [prev | acc]}
end
end)

reduced_polymer =
case res do
{_, acc} -> acc
{:prev, c, acc} -> [c | acc]
end
|> Enum.reverse()
|> to_string

if reduced_polymer == polymer do
polymer
else
scan(reduced_polymer)
end
end

def react?(c1, c2), do: abs(c1 - c2) == 32

@all_units Enum.zip(?a..?z, ?A..?Z) |> Enum.map(fn {c1, c2} -> ~r[#{[c1]}|#{[c2]}] end)
def smallest(polymer) do
@all_units
|> Enum.map(fn unit_to_be_removed ->
polymer
|> String.replace(unit_to_be_removed, "")
|> scan
|> String.length()
end)
|> Enum.min()
end
end

defmodule Day5Test do
use ExUnit.Case

import Day5

test "reduces 2 reacting units" do
assert scan("aA") == ""
end

test "reduces 2 non reacting units" do
assert scan("aB") == "aB"
assert scan("Ba") == "Ba"
end

test "reduces 3 non reacting units" do
assert scan("aBc") == "aBc"
assert scan("aBA") == "aBA"
assert scan("BaD") == "BaD"
end

test "reduces 3 reacting units" do
assert scan("aAB") == "B"
assert scan("abB") == "a"
assert scan("aBb") == "a"
assert scan("BaA") == "B"
end

test "reduces recursively" do
assert scan("baAB") == ""
end

test "large polymer" do
assert scan("dabAcCaCBAcCcaDA") == "dabCBAcaDA"
assert scan("abcdDCBA") == ""
end

test "input" do
assert File.read!("./input.txt") |> String.trim() |> scan |> String.length() == 0
end

test "smallest" do
assert smallest("dabAcCaCBAcCcaDA") == 4
end

test "smallest for input" do
assert File.read!("./input.txt") |> String.trim() |> smallest == 0
end
end

I am currently working on LiveForm which makes setting up contact forms on your website a breeze.