-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday09.ex
83 lines (64 loc) · 2.26 KB
/
day09.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
defmodule Y2016.Day09 do
use Advent.Day, no: 9
@doc """
iex> Day09.decompress("ADVENT")
"ADVENT"
iex> Day09.decompress("A(1x5)BC")
"ABBBBBC"
iex> Day09.decompress("(3x3)XYZ")
"XYZXYZXYZ"
iex> Day09.decompress("A(2x2)BCD(2x2)EFG")
"ABCBCDEFEFG"
iex> Day09.decompress("(6x1)(1x3)A")
"(1x3)A"
iex> Day09.decompress("X(8x2)(3x3)ABCY")
"X(3x3)ABC(3x3)ABCY"
"""
def decompress(string) do
case String.split(string, "(", parts: 2) do
[string] -> string
[content, string] -> content <> duplicate_via_marker(string)
end
end
@doc """
iex> Day09.advanced_decompress_length("(3x3)XYZ")
9 # XYZXYZYZ
iex> Day09.advanced_decompress_length("X(8x2)(3x3)ABCY")
20 # -> XABCABCABCABCABCABCY
iex> Day09.advanced_decompress_length("(27x12)(20x12)(13x14)(7x10)(1x12)A")
241920 # "A" * 241920
iex> Day09.advanced_decompress_length("(25x3)(3x3)ABC(2x3)XY(5x2)PQRSTX(18x9)(3x2)TWO(5x7)SEVEN")
445
"""
def advanced_decompress_length(string) do
case String.split(string, "(", parts: 2) do
[string] ->
String.length(string)
[prefix, string] ->
String.length(prefix) + length_via_nested_marker_expansion("(" <> string)
end
end
defp length_via_nested_marker_expansion(string) do
case Regex.named_captures(~r/\((?P<length>\d+)x(?P<count>\d+)\)(?P<content>.*)/, string) do
nil ->
String.length(string)
%{"length" => length, "count" => count, "content" => content} ->
{content_to_dup, rest} = String.split_at(content, String.to_integer(length))
String.to_integer(count) * length_via_nested_marker_expansion(content_to_dup) +
advanced_decompress_length(rest)
end
end
defp duplicate_via_marker(string) do
case String.split(string, ")", parts: 2) do
[string] ->
string
[marker, string] ->
[dup_length, dup_count] = decode_marker(marker)
{content_to_dup, rest} = String.split_at(string, dup_length)
String.duplicate(content_to_dup, dup_count) <> decompress(rest)
end
end
defp decode_marker(marker), do: String.split(marker, "x") |> Enum.map(&String.to_integer/1)
def part1_verify, do: input() |> decompress() |> String.length()
def part2_verify, do: input() |> advanced_decompress_length()
end