Advertisement
Guest User

Untitled

a guest
Dec 12th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.53 KB | None | 0 0
  1. INSTRUCTIONS = File.readlines("day12_input.txt").map { |line| [line.chomp[0], line.chomp[1..].to_i] }
  2.  
  3. # Part 1
  4. def move_ship1(ship, instructions)
  5.   instructions.each do |instr|
  6.     case instr[0]
  7.     when 'E'
  8.       ship[:pos][:x] += instr[1]
  9.     when 'N'
  10.       ship[:pos][:y] += instr[1]
  11.     when 'W'
  12.       ship[:pos][:x] -= instr[1]
  13.     when 'S'
  14.       ship[:pos][:y] -= instr[1]
  15.     when /[LR]/
  16.       sign = sign = instr[0] == 'L' ? 1 : -1
  17.       ship[:degrees] = (ship[:degrees] + sign * instr[1]) % 360
  18.     when 'F'
  19.       case ship[:degrees]
  20.       # Note: normally would do Math.cos/sin(degrees * Math.PI / 180 ) for x/y normally,
  21.       #       but easier this way since no need to deal with floats here, All
  22.       #       angles are given as integral multiples of 180 degrees.
  23.       when 0
  24.         # E
  25.         ship[:pos][:x] += instr[1]
  26.       when 90
  27.         # N
  28.         ship[:pos][:y] += instr[1]
  29.       when 180
  30.         # W
  31.         ship[:pos][:x] -= instr[1]
  32.       when 270
  33.         # S
  34.         ship[:pos][:y] -= instr[1]
  35.       end
  36.     end
  37.     #puts "instr: #{instr}, ship: #{ship}"
  38.   end
  39. end
  40.  
  41. ship1 = { pos: { x: 0, y: 0 }, degrees: 0 }
  42. move_ship1(ship1, INSTRUCTIONS)
  43.  
  44. puts "#{ ship1[:pos][:x].abs + ship1[:pos][:y].abs }"
  45.  
  46. # Part 2
  47. def move_ship2(ship, waypoint, instructions)
  48.   instructions.each do |instr|
  49.     case instr[0]
  50.     when 'E'
  51.       waypoint[:pos][:x] += instr[1]
  52.     when 'N'
  53.       waypoint[:pos][:y] += instr[1]
  54.     when 'W'
  55.       waypoint[:pos][:x] -= instr[1]
  56.     when 'S'
  57.       waypoint[:pos][:y] -= instr[1]
  58.     when /[LR]/
  59.       # Rotate waypoint around the ship to do stuff.
  60.       # Co-ordinates are given relative to ship, so just directly
  61.       # multiply with imaginary units or -1 as required.
  62.       sign = instr[0] == 'L' ? 1 : -1
  63.       angle = (sign * instr[1]) % 360
  64.    
  65.       complex_dir = case angle
  66.       when 90
  67.         'i'.to_c
  68.       when 180
  69.         '-1'.to_c
  70.       when 270
  71.         '-i'.to_c
  72.       end
  73.    
  74.       waypoint_c = Complex(waypoint[:pos][:x], waypoint[:pos][:y]) * complex_dir
  75.       waypoint[:pos][:x] = waypoint_c.real
  76.       waypoint[:pos][:y] = waypoint_c.imag
  77.     when 'F'
  78.       ship[:pos][:x] += instr[1] * waypoint[:pos][:x]
  79.       ship[:pos][:y] += instr[1] * waypoint[:pos][:y]
  80.     end
  81.     # puts "instr: #{instr}, ship: #{ship}, waypoint: #{waypoint}"
  82.   end
  83. end
  84.  
  85. ship2 = { pos: { x: 0, y: 0 }, degrees: 0 }
  86. waypoint = { pos: { x: 10, y: 1 } }
  87.  
  88. move_ship2(ship2, waypoint, INSTRUCTIONS)
  89. puts "#{ ship2[:pos][:x].abs + ship2[:pos][:y].abs }"
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement