this repo has no description

chore: cleanup 09.2022 v2

+70 -20
+70 -20
2022/day09.livemd
··· 34 34 moves = 35 35 puzzle_input 36 36 |> String.split("\n", trim: true) 37 - |> Enum.map(fn line -> 37 + |> Enum.flat_map(fn line -> 38 38 [dir, steps] = String.split(line) 39 39 40 - {dir, String.to_integer(steps)} 40 + deltas = 41 + case dir do 42 + "R" -> {-1, 0} 43 + "L" -> {+1, 0} 44 + "U" -> {0, +1} 45 + "D" -> {0, -1} 46 + end 47 + 48 + List.duplicate(deltas, String.to_integer(steps)) 41 49 end) 42 - |> Enum.flat_map(fn {dir, steps} -> List.duplicate(dir, steps) end) 43 50 ``` 44 51 45 52 <!-- livebook:{"output":true} --> 46 53 47 54 ``` 48 - ["L", "R", "L", "U", "R", "R", "U", "D", "D", "R", "R", "U", "D", "D", "U", "U", "L", "L", "D", "U", 49 - "D", "D", "L", "L", "D", "L", "L", "D", "D", "L", "U", "U", "R", "R", "L", "D", "D", "L", "L", "R", 50 - "R", "D", "D", "L", "U", "R", "U", "L", "D", "R", ...] 55 + [ 56 + {1, 0}, 57 + {-1, 0}, 58 + {1, 0}, 59 + {0, 1}, 60 + {-1, 0}, 61 + {-1, 0}, 62 + {0, 1}, 63 + {0, -1}, 64 + {0, -1}, 65 + {-1, 0}, 66 + {-1, 0}, 67 + {0, 1}, 68 + {0, -1}, 69 + {0, -1}, 70 + {0, 1}, 71 + {0, 1}, 72 + {1, 0}, 73 + {1, 0}, 74 + {0, -1}, 75 + {0, 1}, 76 + {0, -1}, 77 + {0, -1}, 78 + {1, 0}, 79 + {1, 0}, 80 + {0, -1}, 81 + {1, 0}, 82 + {1, 0}, 83 + {0, -1}, 84 + {0, -1}, 85 + {1, 0}, 86 + {0, 1}, 87 + {0, 1}, 88 + {-1, 0}, 89 + {-1, 0}, 90 + {1, 0}, 91 + {0, -1}, 92 + {0, -1}, 93 + {1, 0}, 94 + {1, 0}, 95 + {-1, 0}, 96 + {-1, 0}, 97 + {0, -1}, 98 + {0, -1}, 99 + {1, 0}, 100 + {0, 1}, 101 + {-1, 0}, 102 + {0, 1}, 103 + {1, 0}, 104 + {0, ...}, 105 + {...}, 106 + ... 107 + ] 51 108 ``` 52 109 53 110 ```elixir ··· 62 119 63 120 def run(rope, moves) do 64 121 {_rope, tail_positions} = 65 - Enum.reduce(moves, {rope, MapSet.new([Rope.last(rope)])}, fn dir, {rope, acc} -> 122 + Enum.reduce(moves, {rope, MapSet.new()}, fn dir, {rope, acc} -> 66 123 new_rope = Rope.move(rope, dir) 67 124 68 125 {new_rope, MapSet.put(acc, Rope.last(new_rope))} ··· 73 130 74 131 def last(%__MODULE__{segments: list}), do: List.last(list) 75 132 76 - def move(%__MODULE__{segments: [%{x: x, y: y} | tails]}, dir) do 77 - head = 78 - case dir do 79 - "L" -> %{x: x + 1, y: y} 80 - "R" -> %{x: x - 1, y: y} 81 - "U" -> %{x: x, y: y + 1} 82 - "D" -> %{x: x, y: y - 1} 83 - end 133 + def move(%__MODULE__{segments: [%{x: x, y: y} | tails]}, {dx, dy}) do 134 + head = %{x: x + dx, y: y + dy} 84 135 85 136 %__MODULE__{segments: move_tails([head | tails])} 86 137 end ··· 95 146 96 147 defp move_tails([head, tail | rest]) do 97 148 {dx, dy} = step(head, tail) 98 - [head | move_tails([%{x: head.x - dx, y: head.y - dy} | rest])] 149 + [head | move_tails([%{x: tail.x + dx, y: tail.y + dy} | rest])] 99 150 end 100 151 101 152 def sgn(0), do: 0 102 153 def sgn(n) when n < 0, do: -1 103 154 def sgn(_), do: 1 104 155 105 - defp step(%{x: x1, y: y1}, %{x: x2, y: y2}) do 106 - {sgn(div(x1 - x2, 2)), sgn(div(y1 - y2, 2))} 107 - end 156 + defp step(%{x: x1, y: y1}, %{x: x2, y: y2}), 157 + do: {sgn(x1 - x2), sgn(y1 - y2)} 108 158 end 109 159 ``` 110 160 111 161 <!-- livebook:{"output":true} --> 112 162 113 163 ``` 114 - {:module, Rope, <<70, 79, 82, 49, 0, 0, 23, ...>>, {:step, 2}} 164 + {:module, Rope, <<70, 79, 82, 49, 0, 0, 22, ...>>, {:step, 2}} 115 165 ``` 116 166 117 167 ## Task 1