Skip to content

Instantly share code, notes, and snippets.

@carlwiedemann
Created December 6, 2023 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlwiedemann/3ffb731d4d88d4bd45621ff158840422 to your computer and use it in GitHub Desktop.
Save carlwiedemann/3ffb731d4d88d4bd45621ff158840422 to your computer and use it in GitHub Desktop.
Advent of Code 2023 day006.rb
require_relative "main"
module Day006
INPUT = File.read('INPUT.txt')
lines = INPUT.to_lines
ts = lines.shift.split_strip(":").last.split_strip(" ").map(&:to_i)
ds = lines.shift.split_strip(":").last.split_strip(" ").map(&:to_i)
data = ts.zip(ds)
##########
# Part 1 #
##########
ways = Array.new(data.count, 0)
data.each.with_index do |(t, d), j|
t.times.each do |i|
ways[j] += 1 if i * (t - i) > d
end
end
answer1 = ways.arith_prod
pp answer1
##########
# Part 2 #
##########
t2 = data.map(&:first).join.to_i
d2 = data.map(&:last).join.to_i
i = 0
j = t2 / 2
k = nil
while j - i > 1
k = (j - i) / 2 + i
(k * (t2 - k) > d2) ? j = k : i = k
end
min_t = (k * t2 <= d2) ? k + 1 : k
answer2 = t2 - ((min_t - 1) + min_t)
pp answer2
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment