Skip to content

Instantly share code, notes, and snippets.

@jadon1979
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save jadon1979/86f5d7a5e571398c876f to your computer and use it in GitHub Desktop.

Select an option

Save jadon1979/86f5d7a5e571398c876f to your computer and use it in GitHub Desktop.
Alpha test
class GuessingGame
attr_reader :total_tricks, :trick_config, :results
def initialize(trick_config)
@total_tricks = trick_config.slice!(0).to_i
@trick_config = trick_config
@results = []
end
def perform
@trick_config.each_slice(10).each_with_index { |line, idx| build(line, idx) }
end
private
def build(l, idx)
tricks = get_card_grid(l)
@results << "Case #%s: %s" % [idx+1, validate(tricks)]
end
def validate(trick)
return 'Volunteer cheated!' if trick[0] === trick[1]
intersection = trick[0] & trick[1]
return intersection[0] if intersection.any?
'Bad magician!'
end
def get_card_grid(line)
2.times.map do |index|
start = index*5
range = (start+1..start+14)
line[range][(line[start].to_i)-1].split
end
end
end
class MagicTrick
attr_reader :tricks
def initialize(tricks, file_input = './magic_trick_input.txt')
@tricks = tricks
parse File.read(file_input)
end
def parse(contents)
tricks = @tricks.new contents.split("\n")
tricks.perform
puts tricks.results
end
end
magic_trick = MagicTrick.new(GuessingGame)
3
2
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
3
1 2 5 4
3 11 6 15
9 10 7 12
13 14 8 16
2
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
2
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
2
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
3
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment