day 6
parent
d1874405b3
commit
b65fb4bfc3
|
@ -0,0 +1,2 @@
|
|||
Time: 51 92 68 90
|
||||
Distance: 222 2031 1126 1225
|
|
@ -0,0 +1,27 @@
|
|||
#prepare the input which is a string containing new lines
|
||||
def parse(input)
|
||||
data = []
|
||||
input.each_line do |line|
|
||||
data << line.split(":").last.split(" ").map(&:to_i)
|
||||
end
|
||||
data
|
||||
end
|
||||
|
||||
# result should a single string or integer
|
||||
def calculate(data)
|
||||
result = []
|
||||
times,distances = data
|
||||
times.length.times do |i|
|
||||
time = times[i]
|
||||
distance = distances[i]
|
||||
win_count = 0
|
||||
(1..time).each do |duration|
|
||||
speed = duration
|
||||
time_left = time - duration
|
||||
travel_distance = speed * time_left
|
||||
win_count += 1 if travel_distance > distance
|
||||
end
|
||||
result << win_count
|
||||
end
|
||||
result.reduce(:*)
|
||||
end
|
|
@ -0,0 +1,23 @@
|
|||
#prepare the input which is a string containing new lines
|
||||
def parse(input)
|
||||
data = []
|
||||
input.each_line do |line|
|
||||
data << line.split(":").last.gsub(" ","").to_i
|
||||
puts data
|
||||
end
|
||||
data
|
||||
end
|
||||
|
||||
# result should a single string or integer
|
||||
def calculate(data)
|
||||
win_count = 0
|
||||
time,distance = data
|
||||
win_count = 0
|
||||
(1..time).each do |duration|
|
||||
speed = duration
|
||||
time_left = time - duration
|
||||
travel_distance = speed * time_left
|
||||
win_count += 1 if travel_distance > distance
|
||||
end
|
||||
win_count
|
||||
end
|
|
@ -0,0 +1,24 @@
|
|||
if ARGV[0] == "2"
|
||||
require_relative "impl_2"
|
||||
else
|
||||
require_relative "impl"
|
||||
end
|
||||
|
||||
|
||||
def print(result)
|
||||
if result.is_a? Array
|
||||
result.map! {|l| "#{l}\n" }
|
||||
end
|
||||
|
||||
puts result
|
||||
end
|
||||
|
||||
def run
|
||||
puts "running your implementation"
|
||||
input = File.new("day_6/data").read
|
||||
data = parse(input)
|
||||
result = calculate(data)
|
||||
print(result)
|
||||
end
|
||||
|
||||
run
|
|
@ -0,0 +1,28 @@
|
|||
if ARGV[0] == "2"
|
||||
require_relative "impl_2"
|
||||
RESULT = 71503
|
||||
else
|
||||
require_relative "impl"
|
||||
RESULT = 288
|
||||
end
|
||||
|
||||
|
||||
INPUT = <<~IN
|
||||
Time: 7 15 30
|
||||
Distance: 9 40 200
|
||||
IN
|
||||
|
||||
def test_example
|
||||
parsed = parse(INPUT)
|
||||
result = calculate(parsed)
|
||||
|
||||
if result == RESULT
|
||||
puts "Test successful. Now run with real input"
|
||||
puts "ruby day_6/run.rb #{ARGV[0]}"
|
||||
else
|
||||
puts "Test failed"
|
||||
puts "expected \"#{RESULT}\" got \"#{result}\""
|
||||
end
|
||||
end
|
||||
|
||||
test_example
|
Loading…
Reference in New Issue