blob: c0c0a5f1258de4766a56235d9f9cc8fc819cff23 (
plain)
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
|
#!/usr/bin/env ruby
input = gets.chomp
input =~ /(\d+) players; last marble is worth (\d+) points/
num_players = $1.to_i
num_marbles = $2.to_i
marbles = [0]
players = Array.new(num_players, 0)
current = 0
player = 0
num_marbles.times do |m|
marble = m + 1
if (marble % 23).zero?
players[player] += marble
d = (current - 7) % marbles.length
players[player] += marbles.delete_at(d)
current = d
else
nm = ((current + 1) % marbles.length) + 1
marbles.insert(nm, marble)
current = nm
end
player = (player + 1) % num_players
end
puts players.max
|