-
Notifications
You must be signed in to change notification settings - Fork 0
/
day12.rb
47 lines (41 loc) · 785 Bytes
/
day12.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
arr = File.read('input.txt').split
arr.map! do |str|
/(\D)(\d*)/ =~ str
[$1, $2.to_i]
end
s_ew = 0
s_ns= 0
ew = 10
ns = 1
curr_dir = 0
$DIR = %w[E N W S]
def process(cmd, n, ew, ns, curr_dir)
case cmd
when 'E' then ew += n
when 'W' then ew -= n
when 'N' then ns += n
when 'S' then ns -= n
# part 1
#when 'L' then curr_dir += n / 90
#when 'R' then curr_dir -= n / 90
when 'L'
(n/90).times do
ew, ns = -ns, ew
end
when 'R'
(n/90).times do
ew, ns = ns, -ew
end
end
return ew, ns, curr_dir
end
arr.each do |cmd, n|
if cmd == 'F'
s_ew += ew * n
s_ns += ns * n
else
ew, ns, curr_dir = process(cmd, n, ew, ns, curr_dir)
end
puts "waypoint: #{ew}, #{ns} ship: #{s_ew}, #{s_ns}"
end
puts s_ew.abs + s_ns.abs