diff --git a/2023/day6/p1.rb b/2023/day6/p1.rb new file mode 100644 index 0000000..a3e799a --- /dev/null +++ b/2023/day6/p1.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +res = 1 +f = File.open('./2023/day6/data') +times = f.readline.scan(/\d+/).map(&:to_i) +distances = f.readline.scan(/\d+/).map(&:to_i) + +times.each_with_index do |time, i| + res *= (0..time).reduce(0) { |acc, j| distances[i] < (time - j) * j ? acc + 1 : acc } +end + +puts res diff --git a/2023/day6/p2.rb b/2023/day6/p2.rb new file mode 100644 index 0000000..ab4a945 --- /dev/null +++ b/2023/day6/p2.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +f = File.open('./2023/day6/data') +time = f.readline.split(':')[1].gsub(' ', '').to_i +distance = f.readline.split(':')[1].gsub(' ', '').to_i +puts (0..time).reduce(0) { |acc, j| distance < (time - j) * j ? acc + 1 : acc }