diff --git a/2024/day02/day02a b/2024/day02/day02a new file mode 100755 index 00000000..d3a35f2d --- /dev/null +++ b/2024/day02/day02a @@ -0,0 +1,21 @@ +#!/usr/bin/env ruby + +def increasing(levels) = levels.eql?(levels.sort) + +def decreasing(levels) = levels.eql?(levels.sort.reverse) + +def max_diff(levels) + levels.each_cons(2).to_a.all? do |a, b| + diff = (a - b).abs + (1..3).cover?(diff) + end +end + +file = File.open(ARGV[0]) + +safe = file.readlines(chomp: true).count do |report| + levels = report.split.map(&:to_i) + (increasing(levels) || decreasing(levels)) && max_diff(levels) +end + +puts safe diff --git a/2024/day02/day02b b/2024/day02/day02b new file mode 100755 index 00000000..4e4c126e --- /dev/null +++ b/2024/day02/day02b @@ -0,0 +1,29 @@ +#!/usr/bin/env ruby + +def increasing(levels) = levels.eql?(levels.sort) + +def decreasing(levels) = levels.eql?(levels.sort.reverse) + +def max_diff(levels) + levels.each_cons(2).to_a.all? do |a, b| + diff = (a - b).abs + (1..3).cover?(diff) + end +end + +def are_safe(levels) = (increasing(levels) || decreasing(levels)) && max_diff(levels) + +def is_safe(report) + levels = report.split.map(&:to_i) + return true if are_safe(levels) + + levels.each_with_index.any? do |level, idx| + copy = levels.dup + copy.delete_at(idx) + are_safe(copy) + end +end + +file = File.open(ARGV[0]) + +puts file.readlines(chomp: true).count { is_safe(_1) }