summaryrefslogtreecommitdiff
path: root/2015/day3.exs
blob: 129d0199b0ef2128d4cc881525db9d94258a36d0 (plain)
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
contents = File.read!("inputs/day3.txt")
directions = String.codepoints(contents)

move = fn
  "^", {x, y} -> {x, y - 1}
  "<", {x, y} -> {x - 1, y}
  ">", {x, y} -> {x + 1, y}
  "v", {x, y} -> {x, y + 1}
end

coordinates = Enum.scan(directions, {0, 0}, move)

unique_coordinates = Enum.uniq(coordinates)

IO.puts("Unique Households for Just Santa: #{length(unique_coordinates)}")

get_shared_coords = fn offset ->
  Enum.with_index(directions)
  |> Enum.filter(fn {_, i} -> rem(i, 2) == offset end)
  |> Enum.map(fn {val, _} -> val end)
  |> Enum.scan({0, 0}, move)
end

regular_santa_coordinates = get_shared_coords.(0)
robo_santa_coordinates = get_shared_coords.(1)
shared_coordinates = regular_santa_coordinates ++ robo_santa_coordinates
unique_shared_coordinates = Enum.uniq(shared_coordinates)

IO.puts("Unique Households for Santa and RoboSanta: #{length(unique_shared_coordinates)}")