-
-
Save tfwright/014e4778790777507624f001d513989d to your computer and use it in GitHub Desktop.
AOC 2022 10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
System.argv() | |
|> List.first() | |
|> File.stream!() | |
|> Enum.reduce({{0, 0}, [{0, 0}]}, fn move, acc -> | |
[dir, length] = | |
~r/(\w)\s(\d{1,2})/ | |
|> Regex.run(move, capture: :all_but_first) | |
for _ <- 1..String.to_integer(length), reduce: acc do | |
{{head_x, head_y}, history = [{tail_x, tail_y} | _]} -> | |
new_head_pos = {new_head_x, new_head_y} = | |
case dir do | |
"U" -> {head_x, head_y + 1} | |
"R" -> {head_x + 1, head_y} | |
"D" -> {head_x, head_y - 1} | |
"L" -> {head_x - 1, head_y} | |
end | |
diff_x = new_head_x - tail_x | |
diff_y = new_head_y - tail_y | |
dist_x = abs(diff_x) | |
dist_y = abs(diff_y) | |
x_move = dist_x == 2 || (dist_x == 1 and dist_y == 2) | |
y_move = dist_y == 2 || (dist_y == 1 and dist_x == 2) | |
new_tail_x = if x_move, do: tail_x + (1 * Integer.floor_div(diff_x, dist_x)), else: tail_x | |
new_tail_y = if y_move, do: tail_y + (1 * Integer.floor_div(diff_y, dist_y)), else: tail_y | |
{new_head_pos, [{new_tail_x, new_tail_y} | history]} | |
end | |
end) | |
|> elem(1) | |
|> Enum.uniq() | |
|> Enum.count() | |
|> IO.inspect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
System.argv() | |
|> List.first() | |
|> File.stream!() | |
|> Enum.reduce({List.duplicate({0, 0}, 9), [{0, 0}]}, fn move, acc -> | |
[dir, length] = | |
~r/(\w)\s(\d{1,2})/ | |
|> Regex.run(move, capture: :all_but_first) | |
for _ <- 1..String.to_integer(length), reduce: acc do | |
{segments, history = [tail | _]} -> | |
[new_tail | new_segments] = | |
segments | |
|> Kernel.++([tail]) | |
|> Enum.reduce([], fn | |
{head_x, head_y}, segments = [] -> | |
new_head = | |
case dir do | |
"U" -> {head_x, head_y + 1} | |
"R" -> {head_x + 1, head_y} | |
"D" -> {head_x, head_y - 1} | |
"L" -> {head_x - 1, head_y} | |
end | |
[new_head | segments] | |
{tail_x, tail_y}, segments = [{new_head_x, new_head_y} | _] -> | |
diff_x = new_head_x - tail_x | |
diff_y = new_head_y - tail_y | |
dist_x = abs(diff_x) | |
dist_y = abs(diff_y) | |
x_move = dist_x == 2 || (dist_x == 1 and dist_y == 2) | |
y_move = dist_y == 2 || (dist_y == 1 and dist_x == 2) | |
new_tail_x = if x_move, do: tail_x + (1 * Integer.floor_div(diff_x, dist_x)), else: tail_x | |
new_tail_y = if y_move, do: tail_y + (1 * Integer.floor_div(diff_y, dist_y)), else: tail_y | |
[{new_tail_x, new_tail_y} | segments] | |
end) | |
{Enum.reverse(new_segments), [new_tail | history]} |> IO.inspect | |
end | |
end) | |
|> elem(1) | |
|> Enum.uniq() | |
|> Enum.count() | |
|> IO.inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment