|
|
@ -1,25 +1,27 @@
|
|
|
|
input = DATA.readlines(chomp: true).map { _1.split(/\s+/).map(&:to_i) }
|
|
|
|
Report = Data.define(:levels) do
|
|
|
|
|
|
|
|
def safe?
|
|
|
|
def safe(report)
|
|
|
|
deltas = levels.each_cons(2).map { _1 - _2 }
|
|
|
|
deltas = report.each_cons(2).map { _1 - _2 }
|
|
|
|
|
|
|
|
(deltas.all?(&:negative?) || deltas.all?(&:positive?)) && (deltas.all? { (1..3).cover?(_1.abs) })
|
|
|
|
(deltas.all?(&:negative?) || deltas.all?(&:positive?)) && (deltas.all? { (1..3).cover?(_1.abs) })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def dampened(report)
|
|
|
|
def dampened
|
|
|
|
return enum_for(__method__, report) unless block_given?
|
|
|
|
return enum_for(__method__) unless block_given?
|
|
|
|
|
|
|
|
|
|
|
|
(0...report.length).each do |i|
|
|
|
|
(0...levels.length).each do |i|
|
|
|
|
yield report[0...i] + report[i+1..]
|
|
|
|
yield Report.new(levels[0...i] + levels[i+1..])
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
pp input.count {|report|
|
|
|
|
input = DATA.readlines(chomp: true)
|
|
|
|
|
|
|
|
.map { _1.scan(/\d+/).map(&:to_i) }
|
|
|
|
|
|
|
|
.map { Report.new(_1) }
|
|
|
|
|
|
|
|
|
|
|
|
# part one
|
|
|
|
# part one
|
|
|
|
# safe(report)
|
|
|
|
pp input.count(&:safe?)
|
|
|
|
|
|
|
|
|
|
|
|
# part two
|
|
|
|
# part two
|
|
|
|
safe(report) || dampened(report).any? { safe(_1) }
|
|
|
|
pp input.count { _1.safe? || _1.dampened.any?(&:safe?) }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
__END__
|
|
|
|
__END__
|
|
|
|
7 6 4 2 1
|
|
|
|
7 6 4 2 1
|
|
|
|