diff options
Diffstat (limited to '2015')
-rw-r--r-- | 2015/day3.exs | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/2015/day3.exs b/2015/day3.exs new file mode 100644 index 0000000..129d019 --- /dev/null +++ b/2015/day3.exs @@ -0,0 +1,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)}") |