m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/12/a.rb
blob: e47babe0c3808f6d067b7cfc5c2a8f2b512fd323 (plain)
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
instructions = File.readlines('input.txt').map do |line|
  [line.slice(0, 1), line.slice(1, line.length).to_i]
end

n = 0
e = 0
direction = 0

instructions.each do |instruction|
  case instruction[0]
  when 'N'
    n += instruction[1]
  when 'S'
    n -= instruction[1]
  when 'E'
    e += instruction[1]
  when 'W'
    e -= instruction[1]
  when 'F'
    case direction
    when 0
      e += instruction[1]
    when 90
      n += instruction[1]
    when 180
      e -= instruction[1]
    when 270
      n -= instruction[1]
    end
  when 'L'
    direction += instruction[1]
    direction %= 360
  when 'R'
    direction -= instruction[1]
    direction %= 360
  end
end

puts n.abs + e.abs