-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08_02.rb
More file actions
40 lines (30 loc) · 1.07 KB
/
08_02.rb
File metadata and controls
40 lines (30 loc) · 1.07 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
#!/usr/bin/env ruby
# frozen_string_literal: true
input_commands = File.open('input_08_01.txt').read.split("\n").map(&:split)
def get_acc(input_commands, acc=0, position=0)
@visited_position[position] += 1
cmd, arg = input_commands[position]
case cmd
when 'nop'
position += 1
when 'acc'
acc += arg.to_i
position += 1
when 'jmp'
position += arg.to_i
end
return acc if position == input_commands.length
return acc if @visited_position[position] > 30
get_acc(input_commands, acc, position)
end
def qwe(input_commands)
input_commands.each_index.select{|i| input_commands[i][0] == 'jmp' || input_commands[i][0] == 'nop'}.each do |fixed_position|
@visited_position = Hash.new(0)
ic = input_commands.clone
k = input_commands[fixed_position]
ic[fixed_position] = [k[0] == 'jmp' ? 'nop' : 'jmp', k[1]]
x = get_acc(ic)
puts "acc: #{x}" if @visited_position[input_commands.length-1] > 0
end
end
qwe(input_commands)