-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday-21-part2.rb
87 lines (64 loc) · 1.54 KB
/
day-21-part2.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
require 'benchmark'
require 'stringio'
require "enumerator"
file_content = File.read(File.join(__dir__, 'input.txt'))
start_a, start_b = file_content.scan(/\d+$/).map(&:to_i)
class Player
attr_accessor :score
def initialize(start, name)
@name = name
@space = start
@score = 0
end
def play(values)
@space = (@space + values.sum - 1) % Game::MAX_SPACE + 1
@score += @space
# not 0 based so shift forth and back the num
puts "#{@name}: #{@space} (#{@score})"
end
def wins?
@score >= Game::MAX_SCORE
end
end
class Game
MAX_SPACE = 10
MAX_SCORE = 1000
def initialize(start_a, start_b)
@die = DETERMINISTIC_DIE
@rounds = 0
@p_a = Player.new(start_a, "Player A")
@p_b = Player.new(start_b, 'Player B')
end
def play
until @p_a.wins? || @p_b.wins?
@rounds += 1
@p_a.play(3.times.collect{ @die.next[1] })
break if @p_a.wins?
@rounds += 1
@p_b.play(3.times.collect{ @die.next[1] })
break if @p_b.wins?
end
min_score = [@p_a.score, @p_b.score].min
rolls = @die.peek[0] - 1
result = min_score * rolls
puts "result #{result}"
end
end
def create_die(start_side = 0, start_rolls = 0)
Enumerator.new do |yielder|
side = start_side
rolls = start_rolls
loop do
rolls += 1
side = 0 if side >= 100
side += 1
yielder << {rolls: rolls, side: side, die: create_die(side, rolls)}
end
end
end
initial_die = create_die
a = [0, 0]
b = [0, 0]
dies = initial_die.take(3).to_a
die1 = dies[0][:die]
puts "ok"