Skip to content

Instantly share code, notes, and snippets.

@adhrinae
Last active February 5, 2017 23:42
Show Gist options
  • Save adhrinae/b812e4c0229aeac813550e62496b7688 to your computer and use it in GitHub Desktop.
Save adhrinae/b812e4c0229aeac813550e62496b7688 to your computer and use it in GitHub Desktop.
class Game
def initialize
@rounds = []
end
def set(round)
@rounds << round
end
def fastest_round
fastest_round_length = @rounds.map { |round| round.length }.min
@rounds.find { |round| round.length == fastest_round_length }
end
def fastest_round_info
{
order: @rounds.index(fastest_round) + 1,
length: fastest_round.length
}
end
end
class Round
attr_reader :max, :duration
def initialize(max, duration)
@max = max
@duration = duration
@all_numbers = [*1..@max]
end
def min_winning_num
@all_numbers[(max-1) % (1+duration) - 1]
end
def winning_numbers
@all_numbers.select do |number|
(number - min_winning_num) % (1 + duration) == 0
end
end
def length
winning_numbers.length * 2
end
end
@game = Game.new
total_rounds = gets.chomp.to_i
total_rounds.times do
round_info = gets.chomp.split(' ')
@game.set(Round.new(round_info[0].to_i, round_info[1].to_i))
end
puts "#{@game.fastest_round_info[:order]} #{@game.fastest_round_info[:length]}"
require 'minitest/autorun'
require_relative 'beskin'
class TestGame < Minitest::Test
def setup
@game = Game.new
@r1 = Round.new(31, 3)
@r2 = Round.new(20, 6)
@r3 = Round.new(14, 2)
@game.set(@r1)
@game.set(@r2)
@game.set(@r3)
end
def test_can_create_each_round
@round = Round.new(31, 3)
assert_equal 31, @round.max
assert_equal 3, @round.duration
end
def test_find_minimum_wining_number_of_round
@round = Round.new(31, 3)
assert_equal 2, @round.min_winning_num
end
def test_find_winning_numbers_of_round
@round = Round.new(31, 3)
assert_equal [2, 6, 10, 14, 18, 22, 26, 30], @round.winning_numbers
end
def test_return_length_of_round
@round = Round.new(31, 3)
assert_equal 16, @round.length
end
def test_game_takes_round
assert_equal 3, @game.rounds.length
end
def test_find_fastest_round_of_game
assert_equal @r2, @game.fastest_round
end
def test_return_info_of_the_fastest_round_of_the_game
assert_equal 2, @game.fastest_round_info[:order]
assert_equal 6, @game.fastest_round_info[:length]
end
def test_can_find_correct_info_with_many_rounds
@game = Game.new
@game.set(Round.new(79, 3))
@game.set(Round.new(156, 7))
@game.set(Round.new(24, 1))
@game.set(Round.new(1053, 1052))
@game.set(Round.new(3942, 12))
@game.set(Round.new(152, 4))
@game.set(Round.new(13, 5))
@game.set(Round.new(2435, 241))
@game.set(Round.new(32, 2))
assert_equal 4, @game.fastest_round_info[:order]
assert_equal 2, @game.fastest_round_info[:length]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment