Last active
December 2, 2021 19:51
-
-
Save alexdunae/a77c0274ef64dcb70afe29feb99a9a38 to your computer and use it in GitHub Desktop.
Advent of Code 2021 Ruby vs Crystal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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