summaryrefslogtreecommitdiffstats
path: root/src/day06.exs
blob: 8e339ff9b1d9e66e7185af74ddca40480c389696 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
defmodule Day06 do
  def parse_data(data) do
    grid = data
    |> String.split("\n", trim: true)

    guard = grid
    |> map_elements("^")
    |> hd()

    obstacles = grid
    |> map_elements("#")

    obstacles_vert = obstacles
    |> Enum.group_by(fn {i, _} -> i end, fn {_, j} -> j end)
    |> Enum.map(fn {k, v} -> {k, v |> :gb_sets.from_list()} end)
    |> Map.new()

    obstacles_hori = obstacles
    |> Enum.group_by(fn {_, j} -> j end, fn {i, _} -> i end)
    |> Enum.map(fn {k, v} -> {k, v |> :gb_sets.from_list()} end)
    |> Map.new()

    grid = grid
    |> Enum.map(& :array.from_list(String.codepoints(&1)))
    |> :array.from_list()

    {guard, grid, obstacles_vert, obstacles_hori}
  end

  def map_elements(grid, element) do
    for {line, i} <- Enum.with_index(grid),
        {v, j} <- Enum.with_index(String.codepoints(line)) do
      if v == element do
        {i, j}
      end
    end
    |> Enum.filter(& &1 != nil)
  end

  def part1({guard, grid, _, _}) do
    path(guard, :up, grid)
    |> Enum.map(fn {i, j, _} -> {i, j} end)
    |> Enum.uniq()
    |> Enum.count()
  end

  def path(acc \\ [], {i, j}, dir, grid) do
    acc = [{i, j, dir} | acc]

    {ni, nj, ndir} = next(i, j, dir)

    case check(ni, nj, grid) do
      :invalid ->
        acc
      :obstacle ->
        path(tl(acc), {i, j}, ndir, grid)
      :valid ->
        path(acc, {ni, nj}, dir, grid)
    end
  end

  def next(i, j, dir) do
    case dir do
      :up ->
        {i-1, j, :right}
      :right ->
        {i, j+1, :down}
      :down ->
        {i+1, j, :left}
      :left ->
        {i, j-1, :up}
    end
  end

  def check(i, j, grid, extra \\ nil) do
    cond do
      i < 0
      or i == :array.size(grid)
      or j < 0
      or j == :array.get(i, grid) |> :array.size() ->
        :invalid
      :array.get(j, :array.get(i, grid)) == "#"
      or {i, j} == extra ->
        :obstacle
      true ->
        :valid
    end
  end

  def part2({guard, grid, overt, ohori}) do
    path(guard, :up, grid)
    |> Enum.map(fn {i, j, _} -> {i, j} end)
    |> Enum.uniq()
    |> Enum.filter(& &1 != guard)
    |> Enum.map(fn {i, j} ->
      is_loop?(guard, :up, grid, update(overt, i, j), update(ohori, j, i))
    end)
    |> Enum.count(& &1)
  end

  def update(m, k, v) do
    Map.get_and_update(m, k, fn set ->
      case set do
        nil ->
          {set, :gb_sets.from_list([v])}
        set ->
          {set, :gb_sets.add(v, set)}
      end
    end)
    |> elem(1)
  end

  def next(i, j, dir, overt, ohori) do
    case dir do
      :up ->
        case :gb_sets.smaller(i, Map.get(ohori, j, :gb_sets.empty())) do
          :none ->
            :out
          {_, v} ->
            {v+1, j, :right}
        end
      :right ->
        case :gb_sets.larger(j, Map.get(overt, i, :gb_sets.empty())) do
          :none ->
            :out
          {_, v} ->
            {i, v-1, :down}
        end
      :down ->
        case :gb_sets.larger(i, Map.get(ohori, j, :gb_sets.empty())) do
          :none ->
            :out
          {_, v} ->
            {v-1, j, :left}
        end
      :left ->
        case :gb_sets.smaller(j, Map.get(overt, i, :gb_sets.empty())) do
          :none ->
            :out
          {_, v} ->
            {i, v+1, :up}
        end
    end
  end

  def is_loop?(cache \\ MapSet.new(), {i, j}, dir, grid, overt, ohori) do
    key = {i, j, dir}
    if MapSet.member?(cache, key) do
      true
    else
      case next(i, j, dir, overt, ohori) do
        :out ->
          false
        {ni, nj, ndir} ->
          is_loop?(MapSet.put(cache, key), {ni, nj}, ndir, grid, overt, ohori)
      end
    end
  end
end

data = IO.read(:stdio, :eof) |> Day06.parse_data()

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

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