blob: ef4fce12106a1bd8a4f082f69a3e0d5cfa345f6d (
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
code = []
File.readlines('input.txt').each do |line|
instruction, number = line.split
code.push [instruction, number.to_i]
end
visited = {}
current_line = 0
acc = 0
code.size.times do |i|
code_copy = code.map {|e| e.slice(0, 2)}
case code_copy[i][0]
when 'jmp'
code_copy[i][0] = 'nop'
when 'nop'
code_copy[i][0] = 'jmp'
end
visited = {}
current_line = 0
acc = 0
while !visited[current_line]
if current_line == code.size
puts acc
break
elsif current_line > code.size
puts 'incorrect run'
break
end
visited[current_line] = true
case code_copy[current_line][0]
when 'nop'
current_line += 1
when 'acc'
acc += code_copy[current_line][1]
current_line += 1
when 'jmp'
current_line += code_copy[current_line][1]
end
end
end
|