File tree Expand file tree Collapse file tree 3 files changed +668
-0
lines changed Expand file tree Collapse file tree 3 files changed +668
-0
lines changed Original file line number Diff line number Diff line change
1
+ nop +0
2
+ acc +1
3
+ jmp +4
4
+ acc +3
5
+ jmp -3
6
+ acc -99
7
+ acc +1
8
+ jmp -4
9
+ acc +6
Original file line number Diff line number Diff line change
1
+ #!/usr/bin/python3
2
+ def debug (code ):
3
+ for pc in range (0 , len (code )):
4
+ operation , argument = code [pc ]
5
+ if operation == 'nop' :
6
+ code [pc ][0 ] = 'jmp'
7
+ elif operation == 'jmp' :
8
+ code [pc ][0 ] = 'nop'
9
+ return_value = execute (code , halt = True , debug = True )
10
+ if return_value :
11
+ return return_value
12
+ else :
13
+ code [pc ][0 ] = operation
14
+
15
+ def execute (code , halt = False , debug = False ):
16
+ accumulator = 0
17
+ pc = 0
18
+ executed = set ()
19
+
20
+ while True :
21
+ if halt :
22
+ if pc in executed :
23
+ if debug :
24
+ return False
25
+ break
26
+ executed .add (pc )
27
+
28
+ if pc == len (code ):
29
+ break
30
+
31
+ operation , argument = code [pc ]
32
+
33
+ if operation == 'acc' :
34
+ accumulator += int (argument )
35
+ pc += 1
36
+ elif operation == 'jmp' :
37
+ pc += int (argument )
38
+ else :
39
+ pc += 1
40
+
41
+ return accumulator
42
+
43
+ def main ():
44
+ with open ('input' ) as f :
45
+ code = [line .strip ().split (' ' ) for line in f ]
46
+
47
+ print (execute (code , halt = True ))
48
+ print (debug (code ))
49
+
50
+ if __name__ == "__main__" :
51
+ main ()
You can’t perform that action at this time.
0 commit comments