Skip to content

Instantly share code, notes, and snippets.

@penguoir
Created December 2, 2024 23:25
Show Gist options
  • Save penguoir/473d6da37a8c1cc76207f582630476af to your computer and use it in GitHub Desktop.
Save penguoir/473d6da37a8c1cc76207f582630476af to your computer and use it in GitHub Desktop.
Advent of code
def each_way_to_remove_one_level(original_arr)
original_arr.map.with_index do |_, index|
original_arr.dup.tap { _1.delete_at(index) }
end
end
def monotonic?(record)
record.each_cons(2).map { |a, b| a <=> b }.uniq.count == 1
end
def small_distances?(record)
record.each_cons(2).all? do |a, b|
(a - b).abs.between?(1, 3)
end
end
def safe?(record)
monotonic?(record) && small_distances?(record)
end
def read_records_from_stdin
ARGF.read.lines.map do |line|
line.split.map(&:to_i)
end
end
# Level 1
puts read_records_from_stdin.count { |record| safe?(record) }
# Level 2
puts read_records_from_stdin.count { |record|
each_way_to_remove_one_level(record).any? { |record| safe?(record) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment