-
Notifications
You must be signed in to change notification settings - Fork 0
/
9b.ex
executable file
·60 lines (51 loc) · 1.3 KB
/
9b.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
#! /usr/bin/env elixir
limit = 25
weak =
File.read!("9.csv")
|> String.split("\n", trim: true)
|> Enum.reduce_while([], fn num, list ->
num = String.to_integer(num)
if length(list) < limit do
{:cont, [num | list]}
else
sums = for(x <- list, y <- list, x != y, do: x + y) |> MapSet.new()
if MapSet.member?(sums, num) do
{:cont, Enum.take([num | list], limit)}
else
{:halt, num}
end
end
end)
leak =
File.read!("9.csv")
|> String.split("\n", trim: true)
|> Enum.reduce_while([], fn num, list ->
list = [String.to_integer(num) | list]
if length(list) < 2 do
{:cont, list}
else
case Enum.sum(list) do
^weak ->
{:halt, list}
too_small when too_small < weak ->
{:cont, list}
too_large when too_large > weak ->
rest =
Enum.reduce_while(list, list, fn _item, list ->
if Enum.sum(list) <= weak do
{:halt, list}
else
{:cont, Enum.take(list, length(list) - 1)}
end
end)
if Enum.sum(rest) == weak do
{:halt, rest}
else
{:cont, rest}
end
end
end
end)
[Enum.min(leak), Enum.max(leak)]
|> Enum.sum()
|> IO.inspect()