summaryrefslogtreecommitdiffstats
path: root/src/day01.exs
blob: b9b346da3ed4dd237253fd337fd0cf35111e8129 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[l1, l2] = IO.read(:stdio, :eof)
|> String.split([" ", "\n"], trim: true)
|> Enum.map(&String.to_integer/1)
|> Enum.with_index()
|> Enum.split_with(fn {_v, i} -> rem(i, 2) == 0 end)
|> Tuple.to_list()
|> Enum.map(fn l -> Enum.map(l, fn {v, _i} -> v end) end)
|> Enum.map(&Enum.sort/1)

ans1 = Enum.zip(l1, l2)
|> Enum.map(fn {v1, v2} -> abs(v1 - v2) end)
|> Enum.sum()

IO.puts(ans1)

c2 = Enum.frequencies(l2)

ans2 = Enum.map(l1, &(&1 * Map.get(c2, &1, 0))) |> Enum.sum()

IO.puts(ans2)