about summary refs log tree commit diff
path: root/day2.exs
blob: 6aa5ff0da7a4e0b3c76a4a2dd8de35246cdc2348 (plain) (blame)
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
defmodule Day2 do
  def input() do
    File.stream!("input/day2.in")
    |> Stream.map(&String.trim/1)
  end

  def find_subcount(count) do
    input()
    |> Enum.reduce(0, fn x, acc ->
      if x
         |> String.graphemes()
         |> Enum.sort()
         |> Enum.uniq()
         |> Enum.any?(fn y ->
           Enum.count(String.graphemes(x), &(&1 == y)) == count
         end) do
        acc + 1
      else
        acc
      end
    end)
  end

  def find_common_chars() do
    input()
    |> Enum.reduce(%MapSet{}, fn x, acc ->
      x
      |> String.graphemes()
      |> Enum.with_index()
      |> Enum.reduce(acc, fn {_c, i}, seen ->
        pair = {i, String.slice(x, 0, i) <> String.slice(x, (i + 1)..-1)}
        if MapSet.member?(seen, pair), do: IO.puts(elem(pair, 1))
        MapSet.put(seen, pair)
      end)
    end)
  end
end

IO.puts(Day2.find_subcount(2) * Day2.find_subcount(3))
Day2.find_common_chars()