-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_game.rb
More file actions
executable file
·59 lines (45 loc) · 1.36 KB
/
memory_game.rb
File metadata and controls
executable file
·59 lines (45 loc) · 1.36 KB
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
#!/usr/bin/env ruby
=begin
___ _ ___
| \ __ _ _ _ / | __|
| |) / _` | || | | |__ \
|___/\__,_|\_, | |_|___/
|__/
"Rambunctious Recitation"
Challenge:
Transportation elves play a memory game that is a recitation of numbers.
Starting from a given sequence (puzzle input), follow the rules and report the
2020th number in the sequence.
On a turn, the speaker considers the most recently spoken number, and:
- If the number had not been spoken before, the player says 0.
- Else, the player says how many turns ago that number was previously spoken.
=end
class MemoryGame
# Keep a running list of the number's we've said, in order
@@said = []
def do_turn()
last = @@said.last
# Make a list of turn numbers when we said the last number
lookback = @@said.reverse.each_with_index.filter_map { |x, i| i + 1 if x == last }
if lookback.length > 1
# Gap between turns
out = lookback[1] - lookback[0]
else
# New number
out = 0
end
puts "Ended turn #{@@said.length + 1}: #{last} -> #{out}"
@@said.push(out)
end
def start(input, count)
seq = input.split(',')
seq.each { |i| @@said.push(i.to_i) }
while @@said.length < count
do_turn
end
end
end
game = MemoryGame.new
game.start(File.read("memory_start.txt").strip, 2020)
# Part One solution:
# Ended turn 2020: 128 -> 662