-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmemory.rb
101 lines (92 loc) · 2.19 KB
/
memory.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Memory is a pointy-topped hexagonal grid which contains one integer value for
# each edge. That is, the data structure is the line graph of an infinite
# hexagonal grid. Edges are indexed by the axial coordinates of the (westward)
# adjacent hexagon, and a symbol :NE, :E, :SE indicating which of the three
# edges is meant (where the direction is taken from the hexagon to the edge).
# The memory pointer includes another flag which indicates whether the MP
# is currently pointing in the clockwise or counter-clockwise directoin (in
# relation to the hexagon used for indexing).
class Memory
def initialize
@memory = Hash.new { 0 }
@mp = [0, 0, :E]
@cw = false
end
def reverse
@cw = !@cw
end
def move_left
*@mp, @cw = left_index
end
def move_right
*@mp, @cw = right_index
end
def set value
@memory[@mp] = value
end
def get
@memory[@mp]
end
def get_left
*mp_left, cw = left_index
@memory[mp_left]
end
def get_right
*mp_right, cw = right_index
@memory[mp_right]
end
def left_index
q, r, e = @mp
cw = @cw
case [e, cw]
when [:NE, false]
r -= 1
e = :SE
cw = true
when [:NE, true]
q += 1
r -= 1
e = :SE
cw = false
when [:E, false]
e = :NE
when [:E, true]
r += 1
e = :NE
when [:SE, false]
e = :E
when [:SE, true]
q -= 1
r += 1
e = :E
end
[q, r, e, cw]
end
def right_index
q, r, e = @mp
cw = @cw
case [e, cw]
when [:NE, false]
r -= 1
e = :E
when [:NE, true]
e = :E
when [:E, false]
q += 1
r -= 1
e = :SE
when [:E, true]
e = :SE
when [:SE, false]
r += 1
e = :NE
cw = true
when [:SE, true]
q -= 1
r += 1
e = :NE
cw = false
end
[q, r, e, cw]
end
end