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
34
moves =
35
35
puzzle_input
36
36
|> String.split("\n", trim: true)
37
37
-
|> Enum.map(fn line ->
37
37
+
|> Enum.flat_map(fn line ->
38
38
[dir, steps] = String.split(line)
39
39
40
40
-
{dir, String.to_integer(steps)}
40
40
+
deltas =
41
41
+
case dir do
42
42
+
"R" -> {-1, 0}
43
43
+
"L" -> {+1, 0}
44
44
+
"U" -> {0, +1}
45
45
+
"D" -> {0, -1}
46
46
+
end
47
47
+
48
48
+
List.duplicate(deltas, String.to_integer(steps))
41
49
end)
42
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
48
-
["L", "R", "L", "U", "R", "R", "U", "D", "D", "R", "R", "U", "D", "D", "U", "U", "L", "L", "D", "U",
49
49
-
"D", "D", "L", "L", "D", "L", "L", "D", "D", "L", "U", "U", "R", "R", "L", "D", "D", "L", "L", "R",
50
50
-
"R", "D", "D", "L", "U", "R", "U", "L", "D", "R", ...]
55
55
+
[
56
56
+
{1, 0},
57
57
+
{-1, 0},
58
58
+
{1, 0},
59
59
+
{0, 1},
60
60
+
{-1, 0},
61
61
+
{-1, 0},
62
62
+
{0, 1},
63
63
+
{0, -1},
64
64
+
{0, -1},
65
65
+
{-1, 0},
66
66
+
{-1, 0},
67
67
+
{0, 1},
68
68
+
{0, -1},
69
69
+
{0, -1},
70
70
+
{0, 1},
71
71
+
{0, 1},
72
72
+
{1, 0},
73
73
+
{1, 0},
74
74
+
{0, -1},
75
75
+
{0, 1},
76
76
+
{0, -1},
77
77
+
{0, -1},
78
78
+
{1, 0},
79
79
+
{1, 0},
80
80
+
{0, -1},
81
81
+
{1, 0},
82
82
+
{1, 0},
83
83
+
{0, -1},
84
84
+
{0, -1},
85
85
+
{1, 0},
86
86
+
{0, 1},
87
87
+
{0, 1},
88
88
+
{-1, 0},
89
89
+
{-1, 0},
90
90
+
{1, 0},
91
91
+
{0, -1},
92
92
+
{0, -1},
93
93
+
{1, 0},
94
94
+
{1, 0},
95
95
+
{-1, 0},
96
96
+
{-1, 0},
97
97
+
{0, -1},
98
98
+
{0, -1},
99
99
+
{1, 0},
100
100
+
{0, 1},
101
101
+
{-1, 0},
102
102
+
{0, 1},
103
103
+
{1, 0},
104
104
+
{0, ...},
105
105
+
{...},
106
106
+
...
107
107
+
]
51
108
```
52
109
53
110
```elixir
···
62
119
63
120
def run(rope, moves) do
64
121
{_rope, tail_positions} =
65
65
-
Enum.reduce(moves, {rope, MapSet.new([Rope.last(rope)])}, fn dir, {rope, acc} ->
122
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
76
-
def move(%__MODULE__{segments: [%{x: x, y: y} | tails]}, dir) do
77
77
-
head =
78
78
-
case dir do
79
79
-
"L" -> %{x: x + 1, y: y}
80
80
-
"R" -> %{x: x - 1, y: y}
81
81
-
"U" -> %{x: x, y: y + 1}
82
82
-
"D" -> %{x: x, y: y - 1}
83
83
-
end
133
133
+
def move(%__MODULE__{segments: [%{x: x, y: y} | tails]}, {dx, dy}) do
134
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
98
-
[head | move_tails([%{x: head.x - dx, y: head.y - dy} | rest])]
149
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
105
-
defp step(%{x: x1, y: y1}, %{x: x2, y: y2}) do
106
106
-
{sgn(div(x1 - x2, 2)), sgn(div(y1 - y2, 2))}
107
107
-
end
156
156
+
defp step(%{x: x1, y: y1}, %{x: x2, y: y2}),
157
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
114
-
{:module, Rope, <<70, 79, 82, 49, 0, 0, 23, ...>>, {:step, 2}}
164
164
+
{:module, Rope, <<70, 79, 82, 49, 0, 0, 22, ...>>, {:step, 2}}
115
165
```
116
166
117
167
## Task 1