tangled
alpha
login
or
join now
hauleth.dev
/
advent-of-code
3
fork
atom
this repo has no description
3
fork
atom
overview
issues
pulls
pipelines
chore: cleanup 09.2022 v2
hauleth.dev
3 years ago
e6de0b33
270ba4fc
+70
-20
1 changed file
expand all
collapse all
unified
split
2022
day09.livemd
+70
-20
2022/day09.livemd
···
34
moves =
35
puzzle_input
36
|> String.split("\n", trim: true)
37
-
|> Enum.map(fn line ->
38
[dir, steps] = String.split(line)
39
40
-
{dir, String.to_integer(steps)}
0
0
0
0
0
0
0
0
41
end)
42
-
|> Enum.flat_map(fn {dir, steps} -> List.duplicate(dir, steps) end)
43
```
44
45
<!-- livebook:{"output":true} -->
46
47
```
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", ...]
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
51
```
52
53
```elixir
···
62
63
def run(rope, moves) do
64
{_rope, tail_positions} =
65
-
Enum.reduce(moves, {rope, MapSet.new([Rope.last(rope)])}, fn dir, {rope, acc} ->
66
new_rope = Rope.move(rope, dir)
67
68
{new_rope, MapSet.put(acc, Rope.last(new_rope))}
···
73
74
def last(%__MODULE__{segments: list}), do: List.last(list)
75
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
84
85
%__MODULE__{segments: move_tails([head | tails])}
86
end
···
95
96
defp move_tails([head, tail | rest]) do
97
{dx, dy} = step(head, tail)
98
-
[head | move_tails([%{x: head.x - dx, y: head.y - dy} | rest])]
99
end
100
101
def sgn(0), do: 0
102
def sgn(n) when n < 0, do: -1
103
def sgn(_), do: 1
104
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
108
end
109
```
110
111
<!-- livebook:{"output":true} -->
112
113
```
114
-
{:module, Rope, <<70, 79, 82, 49, 0, 0, 23, ...>>, {:step, 2}}
115
```
116
117
## Task 1
···
34
moves =
35
puzzle_input
36
|> String.split("\n", trim: true)
37
+
|> Enum.flat_map(fn line ->
38
[dir, steps] = String.split(line)
39
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))
49
end)
0
50
```
51
52
<!-- livebook:{"output":true} -->
53
54
```
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
+
]
108
```
109
110
```elixir
···
119
120
def run(rope, moves) do
121
{_rope, tail_positions} =
122
+
Enum.reduce(moves, {rope, MapSet.new()}, fn dir, {rope, acc} ->
123
new_rope = Rope.move(rope, dir)
124
125
{new_rope, MapSet.put(acc, Rope.last(new_rope))}
···
130
131
def last(%__MODULE__{segments: list}), do: List.last(list)
132
133
+
def move(%__MODULE__{segments: [%{x: x, y: y} | tails]}, {dx, dy}) do
134
+
head = %{x: x + dx, y: y + dy}
0
0
0
0
0
0
135
136
%__MODULE__{segments: move_tails([head | tails])}
137
end
···
146
147
defp move_tails([head, tail | rest]) do
148
{dx, dy} = step(head, tail)
149
+
[head | move_tails([%{x: tail.x + dx, y: tail.y + dy} | rest])]
150
end
151
152
def sgn(0), do: 0
153
def sgn(n) when n < 0, do: -1
154
def sgn(_), do: 1
155
156
+
defp step(%{x: x1, y: y1}, %{x: x2, y: y2}),
157
+
do: {sgn(x1 - x2), sgn(y1 - y2)}
0
158
end
159
```
160
161
<!-- livebook:{"output":true} -->
162
163
```
164
+
{:module, Rope, <<70, 79, 82, 49, 0, 0, 22, ...>>, {:step, 2}}
165
```
166
167
## Task 1