Skip to content

Instantly share code, notes, and snippets.

@jqr
Last active December 13, 2023 02:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jqr/bfcd90332ecdcacbd43b46da436feb3c to your computer and use it in GitHub Desktop.
Save jqr/bfcd90332ecdcacbd43b46da436feb3c to your computer and use it in GitHub Desktop.
Ruby code to help navigate the game of Iron Lung
# Ruby code to help navigate the game of Iron Lung.
class Numeric
def sign
value = (self / self.abs)
if value.nan?
1
else
value
end
end
end
class Vector
attr_reader :x, :y
def self.[](x, y)
new(x, y)
end
def self.north
Vector.new(0, 1)
end
def initialize(x, y)
@x, @y = x.to_f, y.to_f
end
def to_s
"<#{x.to_i},#{y.to_i}>"
end
def angle_from_origin
# north is 0
north = self.class.north
angle = (Math.acos(dot(north) / magnitude * north.magnitude) * 180 / Math::PI).round(3)
if angle.nan?
0.0
else
angle * x.sign
end
end
def angle_between(other)
(other - self).angle_from_origin
end
def magnitude
Math.sqrt(x**2 + y**2)
end
def dot(other)
(x * other.x) + (y * other.y)
end
def cross(other)
(x * other.y) - (y * other.x)
end
def unit
mag = magnitude
Vector.new(x / mag, y / mag)
end
def *(scalar)
Vector.new(x * scalar, y * scalar)
end
def /(scalar)
Vector.new(x / scalar, y / scalar)
end
def +(other)
Vector.new(x + other.x, y + other.y)
end
def -(other)
Vector.new(x - other.x, y - other.y)
end
end
course = [
Vector[182, 116],
Vector[187, 187],
Vector[225, 200],
Vector[322, 186],
"PHOTO 1 @ 033",
Vector[322, 225],
Vector[378, 263],
"PHOTO 2 @ 050",
Vector[300, 325],
Vector[259, 406],
"PHOTO 3 @ 296",
Vector[300, 435],
Vector[360, 425],
Vector[400, 325],
Vector[560, 277],
"PHOTO 4 @ 043",
Vector[613, 197],
"PHOTO 5 @ 055",
Vector[750, 260],
Vector[835, 280],
Vector[864, 258],
"PHOTO 6 @ 209",
Vector[835, 280],
Vector[750, 260],
Vector[610, 310],
Vector[560, 425],
Vector[623, 520],
"PHOTO 7 @ 063",
Vector[375, 520],
Vector[375, 625],
Vector[250, 660],
Vector[200, 660],
Vector[180, 610],
Vector[180, 576],
"PHOTO 8 @ 184",
Vector[180, 610],
Vector[200, 660],
Vector[300, 660],
Vector[325, 741],
"PHOTO 9 @ 018",
Vector[475, 690],
Vector[540, 690],
Vector[675, 792],
Vector[675, 828],
"PHOTO 10 @ 295",
Vector[675, 770],
Vector[712, 770],
Vector[790, 675],
]
position = course.shift
while next_thing = course.shift
if next_thing.is_a?(Vector)
heading = position.angle_between(next_thing)
puts "HEAD #{(heading % 360).round(2)} until #{next_thing} (distance #{(position - next_thing).magnitude.round(2)})"
position = next_thing
else
puts next_thing
puts
end
end
HEAD 4.03 until <187,187> (distance 71.18)
HEAD 71.11 until <225,200> (distance 40.16)
HEAD 98.21 until <322,186> (distance 98.01)
PHOTO 1 @ 033
HEAD 0.0 until <322,225> (distance 39.0)
HEAD 55.84 until <378,263> (distance 67.68)
PHOTO 2 @ 050
HEAD 308.48 until <300,325> (distance 99.64)
HEAD 333.15 until <259,406> (distance 90.79)
PHOTO 3 @ 296
HEAD 54.73 until <300,435> (distance 50.22)
HEAD 99.46 until <360,425> (distance 60.83)
HEAD 158.2 until <400,325> (distance 107.7)
HEAD 106.7 until <560,277> (distance 167.04)
PHOTO 4 @ 043
HEAD 146.48 until <613,197> (distance 95.96)
PHOTO 5 @ 055
HEAD 65.3 until <750,260> (distance 150.79)
HEAD 76.76 until <835,280> (distance 87.32)
HEAD 127.19 until <864,258> (distance 36.4)
PHOTO 6 @ 209
HEAD 307.19 until <835,280> (distance 36.4)
HEAD 256.76 until <750,260> (distance 87.32)
HEAD 289.65 until <610,310> (distance 148.66)
HEAD 336.5 until <560,425> (distance 125.4)
HEAD 33.55 until <623,520> (distance 113.99)
PHOTO 7 @ 063
HEAD 270.0 until <375,520> (distance 248.0)
HEAD 0.0 until <375,625> (distance 105.0)
HEAD 285.64 until <250,660> (distance 129.81)
HEAD 270.0 until <200,660> (distance 50.0)
HEAD 201.8 until <180,610> (distance 53.85)
HEAD 180.0 until <180,576> (distance 34.0)
PHOTO 8 @ 184
HEAD 0.0 until <180,610> (distance 34.0)
HEAD 21.8 until <200,660> (distance 53.85)
HEAD 90.0 until <300,660> (distance 100.0)
HEAD 17.15 until <325,741> (distance 84.77)
PHOTO 9 @ 018
HEAD 108.78 until <475,690> (distance 158.43)
HEAD 90.0 until <540,690> (distance 65.0)
HEAD 52.93 until <675,792> (distance 169.2)
HEAD 0.0 until <675,828> (distance 36.0)
PHOTO 10 @ 295
HEAD 180.0 until <675,770> (distance 58.0)
HEAD 90.0 until <712,770> (distance 37.0)
HEAD 140.61 until <790,675> (distance 122.92)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment