-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7.ex
62 lines (50 loc) · 1.53 KB
/
7.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
defmodule AoC7 do
def split_hypernet(list) when is_list(list) do
no_hypernet = Enum.take_every(list, 2)
hypernet = Enum.drop_every(list, 2)
{no_hypernet, hypernet}
end
def is_supporting_tls({no_hypernet, hypernet}) do
Enum.any?(no_hypernet, &any_abba/1) and not Enum.any?(hypernet, &any_abba/1)
end
def any_abba(in_string) do
in_string
|> String.graphemes
|> Enum.chunk(4,1)
|> Enum.any?(fn x -> is_abba(x) end)
end
def is_abba([a, b, b, a]) when a != b, do: true
def is_abba(_), do: false
def run1 do
File.read!("input7.txt")
|> String.split()
|> Enum.map(&String.split(&1,~r/\[|\]/))
|> Enum.map(&split_hypernet/1)
|> Enum.filter(&is_supporting_tls/1)
|> Enum.count
end
# Part Two -------------------------------------------------------------------
def is_supporting_ssl({no_hypernet, hypernet}) do
abas = Enum.flat_map(no_hypernet, &find_aba/1)
babs = Enum.map(abas, fn [a, b, a] -> "#{b}#{a}#{b}" end)
hypernet = hypernet
|> Enum.filter(&Enum.any?(babs, fn bab -> String.contains?(&1, bab) end))
length(hypernet) > 0
end
def find_aba(in_string) do
in_string
|> String.graphemes
|> Enum.chunk(3,1)
|> Enum.filter(fn x -> is_aba(x) end)
end
def is_aba([a, b, a]) when a != b, do: true
def is_aba(_), do: false
def run2 do
File.read!("input7.txt")
|> String.split()
|> Enum.map(&String.split(&1,~r/\[|\]/))
|> Enum.map(&split_hypernet/1)
|> Enum.filter(&is_supporting_ssl/1)
|> Enum.count
end
end