Last active
August 29, 2015 14:01
-
-
Save jadon1979/86f5d7a5e571398c876f to your computer and use it in GitHub Desktop.
Alpha test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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