You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
600 B
31 lines
600 B
input = DATA.readlines(chomp: true).map { _1.split(/\s+/).map(&:to_i) }
|
|
|
|
def safe(report)
|
|
deltas = report.each_cons(2).map { _1 - _2 }
|
|
(deltas.all?(&:negative?) || deltas.all?(&:positive?)) && (deltas.all? { (1..3).cover?(_1.abs) })
|
|
end
|
|
|
|
def dampened(report)
|
|
return enum_for(__method__, report) unless block_given?
|
|
|
|
(0...report.length).each do |i|
|
|
yield report[0...i] + report[i+1..]
|
|
end
|
|
end
|
|
|
|
pp input.count {|report|
|
|
# part one
|
|
# safe(report)
|
|
|
|
# part two
|
|
safe(report) || dampened(report).any? { safe(_1) }
|
|
}
|
|
|
|
__END__
|
|
7 6 4 2 1
|
|
1 2 7 8 9
|
|
9 7 6 2 1
|
|
1 3 2 4 5
|
|
8 6 4 4 1
|
|
1 3 6 7 9
|