-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9_part1.rb
60 lines (51 loc) · 1.15 KB
/
9_part1.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
require 'set'
head = [0, 0]
tail = [0, 0]
def update_tail(head, tail)
difference = [head[0]-tail[0],head[1]-tail[1]]
change_for_tail =Hash[
'2_1' => [1, 1],
'1_2' => [1, 1],
'2_0'=> [1, 0],
'2_-1'=> [1, -1],
'1_-2'=> [1, -1],
'0_-2'=> [0, -1],
'-1_-2'=> [-1, -1],
'-2_-1'=> [-1, -1],
'-2_0'=> [-1, 0],
'-2_1'=> [-1, 1],
'-1_2'=>[-1, 1],
'0_2'=>[0, 1],
'2_2'=> [1, 1],
'-2_-2'=> [-1, -1],
'-2_2'=> [-1, 1],
'2_-2'=> [1, -1]
]
x = change_for_tail.dig([difference[0],'_' ,difference[1] ].join) || [0, 0]
new_tail_pos = [tail[0] + x[0], tail[1] + x[1] ]
return new_tail_pos
end
def update_head(head, direction)
if direction == 'R'
head[1] += 1
elsif direction == 'L'
head[1] -= 1
elsif direction == 'U'
head[0] += 1
elsif direction == 'D'
head[0] -= 1
end
return head
end
tail_positions = Set[]
ARGF.each do | line |
num = line.split(" ")[1].to_i
dir = line.split(" ")[0]
while num > 0 do
head = update_head(head, dir)
num -= 1
tail = update_tail(head, tail)
tail_positions.add(tail)
end
end
puts tail_positions.size