From 73694ec203dbfc9c3018ef822653be6dc656e04c Mon Sep 17 00:00:00 2001 From: Aeilko Bos Date: Sat, 9 Dec 2023 15:07:38 +0100 Subject: [PATCH] Add parameter to automatically track the runtime of a solution --- days/day05.rb | 4 +--- lib/aoc.rb | 14 ++++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/days/day05.rb b/days/day05.rb index 18586aa..6cb99e1 100644 --- a/days/day05.rb +++ b/days/day05.rb @@ -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| @@ -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 @@ -69,4 +67,4 @@ def self.solve_part2(input) end end -Day05.run +Day05.run true diff --git a/lib/aoc.rb b/lib/aoc.rb index 1585fe0..0fe8456 100644 --- a/lib/aoc.rb +++ b/lib/aoc.rb @@ -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 @@ -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 @@ -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 @@ -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