Skip to content

Commit b8fd540

Browse files
committed
Handle corner-case where first and last char are the same
(Folks on Slack ran into it. Test uses Evert's input.)
1 parent 55068f5 commit b8fd540

2 files changed

Lines changed: 138 additions & 4 deletions

File tree

2021/elixir/lib/advent_of_code/day_14.ex

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ defmodule AdventOfCode.Day14 do
4848

4949
def part2(args) do
5050
{start, key} = parse(args)
51+
first_last = {List.first(start), List.last(start)}
5152

5253
all_zeroes = Map.map(key, fn _ -> 0 end)
5354
counts = Enum.chunk_every(start, 2, 1, :discard)
@@ -56,7 +57,7 @@ defmodule AdventOfCode.Day14 do
5657
end)
5758

5859
{min, max} = faster_polymer(counts, key, 1..40)
59-
|> find_min_max()
60+
|> find_min_max(first_last)
6061

6162
max - min
6263
end
@@ -110,16 +111,37 @@ defmodule AdventOfCode.Day14 do
110111
Figures out the most and least common letters and how many
111112
iex> counts = %{{"N", "N"} => 0, {"N", "C"} => 1, {"C", "N"} => 1, {"N", "B"} => 1,
112113
...> {"B","C"} => 1, {"C","H"} => 1, {"H","B"} => 1, {"C", "B"} => 0}
113-
iex> AdventOfCode.Day14.find_min_max(counts)
114+
iex> AdventOfCode.Day14.find_min_max(counts, {"B", "C"})
114115
{1, 2}
116+
117+
iex> counts = %{{"N", "N"} => 0, {"N", "C"} => 1, {"C", "N"} => 1, {"N", "B"} => 1,
118+
...> {"B","C"} => 1, {"C","H"} => 1, {"H","B"} => 1, {"C", "B"} => 0}
119+
iex> AdventOfCode.Day14.find_min_max(counts, {"B", "B"})
120+
{1, 3}
115121
"""
116-
def find_min_max(counts) do
122+
def find_min_max(counts, first_last) do
117123
{{_, min}, {_, max}} = Enum.reduce(counts, %{}, fn {{first, second}, count}, total ->
118124
Map.update(total, first, count, &(&1 + count))
119125
|> Map.update(second, count, &(&1 + count))
120126
end)
121127
|> Map.map(fn {_, x} -> round(x / 2) end)
128+
|> handle_first_last(first_last)
122129
|> Enum.min_max_by(fn {_letter, quant} -> quant end)
123130
{min, max}
124131
end
132+
133+
@doc """
134+
iex> AdventOfCode.Day14.handle_first_last(%{"B" => 3, "C" => 4, "H" => 5}, {"B", "C"})
135+
%{"B" => 3, "C" => 4, "H" => 5}
136+
137+
iex> AdventOfCode.Day14.handle_first_last(%{"B" => 3, "C" => 4, "H" => 5}, {"B", "B"})
138+
%{"B" => 4, "C" => 4, "H" => 5}
139+
"""
140+
def handle_first_last(counts, {a, a}) do
141+
as = counts[a]
142+
Map.replace(counts, a, as + 1)
143+
end
144+
145+
def handle_first_last(counts, {_first, _last}), do: counts
146+
125147
end

2021/elixir/test/advent_of_code/day_14_test.exs

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule AdventOfCode.Day14Test do
33
doctest AdventOfCode.Day14
44

55
import AdventOfCode.Day14
6-
6+
@tag :skip
77
test "part1" do
88
input = """
99
NNCB
@@ -30,6 +30,7 @@ defmodule AdventOfCode.Day14Test do
3030
assert result == 1588
3131
end
3232

33+
@tag :skip
3334
test "part2" do
3435
input = """
3536
NNCB
@@ -55,4 +56,115 @@ defmodule AdventOfCode.Day14Test do
5556

5657
assert result == 2188189693529
5758
end
59+
60+
test "evert" do
61+
input = """
62+
PFVKOBSHPSPOOOCOOHBP
63+
64+
FV -> C
65+
CP -> K
66+
FS -> K
67+
VF -> N
68+
HN -> F
69+
FF -> N
70+
SS -> K
71+
VS -> V
72+
BV -> F
73+
HC -> K
74+
BP -> F
75+
OV -> N
76+
BF -> V
77+
VH -> V
78+
PF -> N
79+
FC -> S
80+
CS -> B
81+
FK -> N
82+
VK -> H
83+
FN -> P
84+
SH -> V
85+
CV -> K
86+
HP -> K
87+
HO -> C
88+
NO -> V
89+
CK -> C
90+
VB -> S
91+
OC -> N
92+
NS -> C
93+
NF -> H
94+
SF -> N
95+
NK -> S
96+
NP -> P
97+
OO -> S
98+
NH -> C
99+
BC -> H
100+
KS -> H
101+
PV -> O
102+
KO -> K
103+
OK -> H
104+
OH -> H
105+
BH -> F
106+
NB -> B
107+
FH -> N
108+
HV -> F
109+
BN -> S
110+
ON -> V
111+
CB -> V
112+
CF -> H
113+
FB -> S
114+
KF -> S
115+
PS -> P
116+
OB -> C
117+
NN -> K
118+
KV -> C
119+
BK -> H
120+
SN -> S
121+
NC -> H
122+
PK -> B
123+
PC -> H
124+
KN -> S
125+
VO -> V
126+
FO -> K
127+
CH -> B
128+
PH -> N
129+
SO -> C
130+
KH -> S
131+
HB -> V
132+
HH -> B
133+
BB -> H
134+
SC -> V
135+
HS -> K
136+
SP -> V
137+
KB -> N
138+
VN -> H
139+
HK -> H
140+
KP -> K
141+
OP -> F
142+
CO -> B
143+
VP -> H
144+
OS -> N
145+
OF -> H
146+
KK -> N
147+
CC -> K
148+
BS -> C
149+
VV -> O
150+
CN -> H
151+
PB -> P
152+
BO -> N
153+
SB -> H
154+
FP -> F
155+
SK -> F
156+
PO -> S
157+
KC -> H
158+
VC -> H
159+
NV -> N
160+
HF -> B
161+
PN -> F
162+
SV -> K
163+
PP -> K
164+
"""
165+
result = part2(input)
166+
167+
assert result == 3390034818249
168+
end
169+
58170
end

0 commit comments

Comments
 (0)