[2024][ruby][2.x]

main
Alpha Chen 1 week ago
parent 0e6dc13fc5
commit 28db35af73
Signed by: alpha
SSH Key Fingerprint: SHA256:3fOT8fiYQG/aK9ntivV3Bqtg8AYQ7q4nV6ZgihOA20g

@ -0,0 +1,30 @@
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
Loading…
Cancel
Save