-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday9.rb
103 lines (94 loc) · 1.91 KB
/
day9.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
require 'set'
input = 'input/day9.txt'
f = open(input, 'r')
lines = f.readlines.map(&:strip)
f.close
hx = hy = tx = ty = 0
visited = Set.new([[tx, ty]])
lines.each do |line|
moves = line.split(' ')
dir = moves.first
step = moves.last.to_i
(1..step).each do
case dir
when 'L'
hx -= 1
if hx < tx - 1
tx = hx + 1
ty = hy
end
when 'R'
hx += 1
if hx > tx + 1
tx = hx - 1
ty = hy
end
when 'U'
hy += 1
if hy > ty + 1
tx = hx
ty = hy - 1
end
when 'D'
hy -= 1
if hy < ty - 1
tx = hx
ty = hy + 1
end
end
visited.add([tx, ty])
end
end
puts visited.length
# part 2
def follow(snake, n)
# makes segment at position n > 0 follow its predecessor
px = snake[n - 1].first
py = snake[n - 1].last
sx = snake[n].first
sy = snake[n].last
if sx == px
sy += 1 if py - sy == 2
sy -= 1 if py - sy == -2
elsif sy == py
sx += 1 if px - sx == 2
sx -= 1 if px - sx == -2
elsif (px - sx).abs == 2 || (py - sy).abs == 2 # diagonal move towards px,py
sx += (px - sx <=> 0)
sy += (py - sy <=> 0)
end
snake[n] = [sx, sy]
end
def print_map(snake)
map = Array.new(40) { Array.new(40) { '.' } }
snake.each_with_index do |seg, n|
map[25 - seg.last][15 + seg.first] = n.to_s
end
map.each { |row| puts row.join }
end
snake = Array.new(10) { [0, 0] }
visited = Set.new([[0, 0]])
lines.each do |line|
moves = line.split(' ')
dir = moves.first
step = moves.last.to_i
(1..step).each do
nx = snake[0].first
ny = snake[0].last
case dir
when 'L'
nx -= 1
when 'R'
nx += 1
when 'U'
ny += 1
when 'D'
ny -= 1
end
snake[0] = [nx, ny]
(1..snake.length - 1).each { |n| follow(snake, n) }
visited.add([snake.last.first, snake.last.last])
# print_map(snake)
end
end
puts visited.length