-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabb_train.ex
201 lines (177 loc) · 4.12 KB
/
labb_train.ex
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
defmodule Train do
### Hjälpfunktioner ###
def len([]) do 0 end
def len([t|h]) do
1+len(h)
end
def take(_, 0) do [] end
def take([], _) do [] end
def take([h|t], n) do
[h|take(t, n-1)]
end
# Ta bort första n vagnarna
def drop(train, 0) do train end
def drop([], _) do
:error # Borde nog bli error
end
def drop([t|h], n) do
if n>1 do
drop(h, n-1)
else
h
end
end
# train1 + train2 = train1 ++ train2 (OBS INGA ++)
def append([], train2) do train2 end
def append([h|t], train2) do
[h|append(t, train2)]
end
# Är y en vagn i train?
def member([], _) do
false
end
def member([h|t], y) do
if(h==y) do
true
else
member(t, y)
end
end
# 1 indexerade första positionen av y i tåget
# Anta att vagnen finns i tåget!!!
def position([h|t], y) do
if(y==h) do
1
else
1+position(t, y)
end
end
# Splitta på en vagn (inkludera inte vagnen i någon av tupelsidorna)
def split(train, y) do
split_pos = position(train, y)
len = len(train)
t1 = take(train, split_pos-1)
t2 = drop(train, split_pos)
{t1, t2}
end
# Retunerar en tuppel {k, remain, take}, n är antalet vagnar som behöver tas,
# remain är vagnarna som finns kvar, take är vagnarna som tas, k är antalet vagnar fler som
# behöver tas för att få n vagnar.
# {k, remain, take}
def main(train, n) do
case {train, n} do
{[], n} -> {n, [], []}
{t, 0} -> {0, t, []}
{[h|t], n} ->
#Rekurivt kall
if(n>0) do
{n_, r_, t_} = main(t, n)
if(n_<=0) do
{0, [h|r_], t_}
else
{n_ - 1, r_, [h|t_]} # ganska säker på -1
end
end
end
end
end
defmodule Moves do
def single({:one, n}, {main, one, two}) do
# n>=0 ==> main --> one
# n<0 ==> one --> main
if(n>=0) do
{k, r, t} = Train.main(main, n)
{r, Train.append(t, one), two}
else
n = -n
take = Train.take(one, n)
new_main = Train.append(main, take)
new_one = Train.drop(one, n)
{new_main, new_one, two}
end
end
def single({:two, n}, {main, one, two}) do
# n>=0 ==> main --> two
# n<0 ==> two --> main
if(n>=0) do
{k, r, t} = Train.main(main, n)
{r, one, Train.append(t, two)}
else
n = -n
take = Train.take(two, n)
new_main = Train.append(main, take)
new_two = Train.drop(two, n)
{new_main, one, new_two}
end
end
def sequence([], state) do state end
def sequence([move|t], state) do
new_state = single(move, state)
[state|sequence(t, new_state)]
end
# Transfrom xs --> ys
def find([], []) do [] end
def find(xs, ys) do
[y] = Train.take(ys, 1)
{hs, ts} = Train.split(xs, y)
ts_len = Train.len(ts)+1
hs_len = Train.len(hs)
moves = [{:one, ts_len}, {:two, hs_len}, {:one, -ts_len}, {:two, -hs_len}]
[_ | new_ys] = ys
new_xs = Train.append(ts, hs)
Train.append(moves, find(new_xs, new_ys))
end
# Transfrom xs --> ys
def few([], []) do [] end
#def find([x], [x]) do [] end
def few(xs, ys) do
# is next already in right position?
[xs_n|_] = xs
[ys_n|_] = ys
[y] = Train.take(ys, 1)
{hs, ts} = Train.split(xs, y)
ts_len = Train.len(ts)+1
hs_len = Train.len(hs)
moves = [{:one, ts_len}, {:two, hs_len}, {:one, -ts_len}, {:two, -hs_len}]
[_ | new_ys] = ys
new_xs = Train.append(ts, hs)
#moves
#IO.puts("xs = #{inspect(new_xs)}, ys = #{inspect(new_ys)}")
if(ys_n==xs_n) do
few(new_xs, new_ys)
else
Train.append(moves, few(new_xs, new_ys))
end
end
def rules([]) do [] end
def rules([h1]) do
{_, n} = h1
if(n == 0) do
[]
else
[h1]
end
end
def rules([h1, h2|t]) do
{dir1, n1} = h1
{dir2, n2} = h2
#Testa förs 0 fallet
if(n1==0) do
rules([h2|t])
else
if(dir1==dir2) do
[{dir1, n1+n2}|rules(t)]
else
[h1|rules([h2|t])]
end
end
end
def compress(ms) do
ns = rules(ms)
if(ns==ms) do
ms
else
compress(ns)
end
end
end