Skip to content

Commit

Permalink
Add parameter to automatically track the runtime of a solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeilko committed Dec 9, 2023
1 parent 030e9ec commit 73694ec
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
4 changes: 1 addition & 3 deletions days/day05.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ def self.solve_part2(input)

groups = groups[1..-1].map{ |group| group.lines[1..-1].map{|line| line.scan(/\d+/).map(&:to_i) } }
i = 0
start_time = Time.now
while true
num = i
groups.reverse_each do |group|
Expand All @@ -59,7 +58,6 @@ def self.solve_part2(input)

seeds.each do |start, stop|
if num.between?(start, stop)
puts "Runtime: #{(Time.now-start_time).round}s"
return i
end
end
Expand All @@ -69,4 +67,4 @@ def self.solve_part2(input)
end
end

Day05.run
Day05.run true
14 changes: 8 additions & 6 deletions lib/aoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Day
# Instance variables
attr_accessor :input, :test_input, :test_input1, :test_input2

def run
def run(track_runtime = false)
attr_defaults
puts "--- Running day #{@num} ---".yellow

Expand All @@ -22,8 +22,8 @@ def run
return
end

run_part 1
run_part 2
run_part 1, track_runtime
run_part 2, track_runtime
end

def load_inputs
Expand Down Expand Up @@ -63,8 +63,7 @@ def load_inputs
end
end

def run_part(part)
# TODO: add the option to track the runtime of a solution
def run_part(part, track_runtime = false)
puts "--- Part #{part} ---".yellow

# Check if the solve_partX method is implemented
Expand All @@ -88,8 +87,11 @@ def run_part(part)
end

# Solve this part
start = Time.now
result = self.send("solve_part#{part}", @input)
puts "Result: #{result}"
stop = Time.now

puts "Result: #{result}#{(track_runtime ? "\t\t(Runtime: #{stop-start}s)" : "")}"
end

def attr_defaults
Expand Down

0 comments on commit 73694ec

Please sign in to comment.