Skip to content

Commit 73694ec

Browse files
committed
Add parameter to automatically track the runtime of a solution
1 parent 030e9ec commit 73694ec

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

days/day05.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ def self.solve_part2(input)
4545

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

6059
seeds.each do |start, stop|
6160
if num.between?(start, stop)
62-
puts "Runtime: #{(Time.now-start_time).round}s"
6361
return i
6462
end
6563
end
@@ -69,4 +67,4 @@ def self.solve_part2(input)
6967
end
7068
end
7169

72-
Day05.run
70+
Day05.run true

lib/aoc.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module Day
1111
# Instance variables
1212
attr_accessor :input, :test_input, :test_input1, :test_input2
1313

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

@@ -22,8 +22,8 @@ def run
2222
return
2323
end
2424

25-
run_part 1
26-
run_part 2
25+
run_part 1, track_runtime
26+
run_part 2, track_runtime
2727
end
2828

2929
def load_inputs
@@ -63,8 +63,7 @@ def load_inputs
6363
end
6464
end
6565

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

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

9089
# Solve this part
90+
start = Time.now
9191
result = self.send("solve_part#{part}", @input)
92-
puts "Result: #{result}"
92+
stop = Time.now
93+
94+
puts "Result: #{result}#{(track_runtime ? "\t\t(Runtime: #{stop-start}s)" : "")}"
9395
end
9496

9597
def attr_defaults

0 commit comments

Comments
 (0)