Advent of Code 2024 - Day 2: Red-Nosed Reports
Day 2 of the Advent of Code coding challenge.
Restated problems:
- You are given a list of "records". Each record is a list of numbers (called "levels"). How many of the records follow both these conditions?
- Monotonic (always increasing or always decreasing)
- Levels change within a range 1-3
- Same as 1. but you can remove one number from the record before checking the conditions.
Solution:
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) }
}