Skip to content

Instantly share code, notes, and snippets.

@alexdunae
Last active December 2, 2021 19:51
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save alexdunae/a77c0274ef64dcb70afe29feb99a9a38 to your computer and use it in GitHub Desktop.
Advent of Code 2021 Ruby vs Crystal
file = "input.txt"
increases = 0
last_window = nil
a = nil
b = nil
c = nil
File.each_line(file) do |measurement|
a = b
b = c
c = measurement.to_i
next unless a && b && c
current_window = a + b + c
if last_window
if current_window > last_window
increases += 1
end
end
last_window = current_window
end
puts increases # 1248
file = "input.txt"
increases = 0
last_window = nil
a = nil
b = nil
c = nil
File.readlines(file).each do |measurement|
a = b
b = c
c = measurement.to_i
next unless a && b && c
current_window = a + b + c
if last_window
if current_window > last_window
increases += 1
end
end
last_window = current_window
end
puts increases # 1248
horizontal = 0
depth = 0
aim = 0
File.each_line("input.txt") do |direction|
dir, delta = direction.split(" ", 2)
delta = delta.to_i
case dir
when "forward"
horizontal += delta
depth += (aim * delta)
when "up"
aim -= delta
when "down"
aim += delta
else
raise "Unknown direction: #{dir}"
end
end
puts "#{horizontal}x#{depth} = #{horizontal*depth}"
horizontal = 0
depth = 0
aim = 0
File.readlines("input.txt").each do |direction|
dir, delta = direction.split(" ", 2)
delta = delta.to_i
case dir
when "forward"
horizontal += delta
depth += (aim * delta)
when "up"
aim -= delta
when "down"
aim += delta
else
raise "Unknown direction: #{dir}"
end
end
puts "#{horizontal}x#{depth} = #{horizontal*depth}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment