summaryrefslogtreecommitdiffstats
path: root/src/day12.exs
blob: 522bb396042617ed768268e84d102348b770a940 (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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
defmodule Day12 do
  def part1(data) do
    grid = data
    |> String.split("\n", trim: true)
    |> Enum.map(&String.codepoints/1)

    grid = for {l, i} <- Enum.with_index(grid),
               {c, j} <- Enum.with_index(l) do
             {{i, j}, c}
    end
    |> Map.new()

    for pos <- Map.keys(grid), reduce: {MapSet.new(), 0} do
      {vis, res} -> solve1(vis, grid, pos)
      |> then(fn {vis, a, p} -> {vis, res + a*p} end)
    end
    |> elem(1)
  end

  defp solve1(vis, grid, pos) do
    if MapSet.member?(vis, pos) do
      {vis, 0, 0}
    else
      for npos <- dirs(pos), reduce: {MapSet.put(vis, pos), 1, 0} do
        {vis, a, p} ->
          case Map.fetch(grid, npos) do
            :error ->
              {vis, a, p+1}
            {_, v} ->
              if v == Map.fetch!(grid, pos) do
                {vis, na, np} = solve1(vis, grid, npos)
                {vis, a+na, p+np}
              else
                {vis, a, p+1}
              end
          end
      end
    end
  end

  defp dirs({i, j}) do
    [{i+1, j}, {i-1, j}, {i, j+1}, {i, j-1}]
  end

  def part2(data) do
    grid = data
    |> String.split("\n", trim: true)
    |> Enum.map(&String.codepoints/1)

    grid = for {l, i} <- Enum.with_index(grid),
               {c, j} <- Enum.with_index(l) do
             {{i, j}, c}
    end
    |> Map.new()

    for pos <- Map.keys(grid), reduce: {MapSet.new(), 0} do
      {vis, res} -> solve2(vis, grid, pos)
      |> then(fn {vis, a, walls} -> {vis, res + a*perim(walls)} end)
    end
    |> elem(1)
  end

  defp solve2(vis, grid, pos) do
    if MapSet.member?(vis, pos) do
      {vis, 0, []}
    else
      for {npos, dir} <- dirs2(pos), reduce: {MapSet.put(vis, pos), 1, []} do
        {vis, a, walls} ->
          case Map.fetch(grid, npos) do
            :error ->
              {vis, a, [{dir, npos} | walls]}
            {_, v} ->
              if v == Map.fetch!(grid, pos) do
                {vis, na, nwalls} = solve2(vis, grid, npos)
                {vis, a+na, walls ++ nwalls}
              else
                {vis, a, [{dir, npos} | walls]}
              end
          end
      end
    end
  end

  defp dirs2({i, j}) do
    [{{i+1, j}, :down},
     {{i-1, j}, :up},
     {{i, j+1}, :right},
     {{i, j-1}, :left}]
  end

  defp dist({a, b}, {c, d}), do: abs(a-c) + abs(b-d)
  defp dist(_, nil), do: nil
  defp dist(nil, _), do: nil

  defp perim([]), do: 0

  defp perim(walls) do
    gwalls = walls
    |> Enum.group_by(&elem(&1,0), &elem(&1,1))

    for {dir, walls} <- gwalls do
      swalls =
        cond do
          dir == :up or dir == :down ->
            Enum.sort(walls)
          true ->
            Enum.sort_by(walls, fn {i, j} -> {j, i} end)
        end

      for w <- swalls, reduce: {nil, 0} do
        {prev, acc} ->
          if dist(w, prev) == 1 do
            {w, acc}
          else
            {w, acc+1}
          end
      end
      |> elem(1)
    end
    |> Enum.sum()
  end
end

data = IO.read(:stdio, :eof)

{time1 , ans1} = :timer.tc(fn -> Day12.part1(data) end)
IO.puts("Time  : #{time1 / 1000000}")
IO.puts("Answer: #{ans1}")

{time2 , ans2} = :timer.tc(fn -> Day12.part2(data) end)
IO.puts("Time  : #{time2 / 1000000}")
IO.puts("Answer: #{ans2}")