-
Notifications
You must be signed in to change notification settings - Fork 0
/
day08.rb
51 lines (40 loc) · 960 Bytes
/
day08.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
input_arr = File.open('input.txt').read.split("\n")
parsed_arr = []
input_arr.each do |str|
/(.*) (.*)/ =~ str
parsed_arr << [$1, $2.to_i]
end
def test_if_end(arr)
acc = 0
i = 0
visited_i = []
loop do
return false if visited_i.include? i
return acc if i == arr.length
visited_i << i
ins = arr[i]
cmd = ins[0]
num = ins[1]
i += 1 if cmd == "nop"
i += num if cmd == "jmp"
if cmd == "acc"
acc += num
i += 1
end
end
end
parsed_arr.each_with_index do |item, index|
cmd = item[0]
next if cmd == "acc"
# awkward way to avoid shallow copy problem
parsed_arr = []
input_arr.each do |str|
/(.*) (.*)/ =~ str
parsed_arr << [$1, $2.to_i]
end
test_arr = parsed_arr # why does this changed parsed array as well?!
test_arr[index][0] = "jmp" if cmd == "nop"
test_arr[index][0] = "nop" if cmd == "jmp"
ret = test_if_end(test_arr)
ret == false ? next : print(ret)
end