# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + active=""
# MIT License
#
# Copyright (c) 2020 <NAME>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# +
input = '''acc +49
jmp +274
acc +49
acc +49
jmp +476
jmp +409
jmp +269
jmp +1
acc -11
acc +5
acc +24
jmp +17
acc +50
acc +9
acc +37
nop +266
jmp +60
jmp +329
acc +18
jmp +327
acc +22
acc -14
jmp +281
nop +287
acc +6
acc -1
acc +22
jmp +302
acc +44
jmp +576
acc +10
acc +33
nop +219
jmp +534
jmp +89
jmp +523
acc +40
acc +22
jmp +53
acc +6
acc +39
acc +26
jmp +81
acc +18
acc +20
acc +31
acc +31
jmp +244
jmp +1
jmp +237
acc -5
acc +2
nop +209
jmp +222
acc -16
jmp +277
jmp +48
jmp +317
jmp +564
acc -5
acc +11
acc -10
acc +1
jmp -5
acc +46
acc +14
acc -3
jmp +393
acc +8
acc +21
acc +6
jmp +142
acc +22
jmp +188
nop +258
jmp +505
acc +27
acc +13
nop +428
acc -12
jmp +354
acc +0
acc +0
jmp +54
acc +11
acc +32
acc +17
nop -3
jmp +182
acc +24
jmp +18
acc +1
acc -4
acc +13
acc +36
jmp +118
acc +48
jmp +383
nop +101
jmp -94
jmp +181
acc +43
jmp +123
jmp +285
acc +10
acc +13
jmp +261
jmp +98
acc +0
acc -12
acc +28
nop -3
jmp -54
jmp +509
acc +34
acc +48
jmp +1
jmp +20
acc +13
acc +6
acc -5
nop +267
jmp +299
acc -13
acc -1
acc +2
jmp +349
jmp +294
acc +20
acc +8
acc +17
acc -1
jmp +242
acc -11
acc +45
acc -13
jmp +98
acc +44
jmp +61
jmp +471
jmp +344
acc +38
jmp +1
nop +490
acc +45
jmp +276
acc -8
jmp +20
acc +49
nop +170
acc +44
jmp +100
nop +236
jmp +209
jmp +45
jmp +1
nop +464
jmp +311
nop +238
nop +212
jmp +236
jmp +328
acc +20
acc +0
acc +46
acc +28
jmp +12
jmp +52
nop +300
nop +420
jmp +149
acc +38
acc +23
jmp +271
acc +21
acc +27
acc +24
jmp +371
acc +20
acc +4
acc -6
nop +24
jmp -54
acc -5
acc +47
jmp -180
jmp +384
acc +44
acc +22
nop +148
acc +32
jmp -107
acc +25
jmp +355
jmp +1
acc +14
acc +11
acc +36
jmp +15
nop +281
acc +48
acc +23
acc +23
jmp +35
jmp +82
acc +19
acc +30
jmp +319
acc +30
acc +41
nop -176
jmp +1
jmp +79
acc +29
acc +41
acc +32
jmp -199
acc -15
jmp +402
nop +91
jmp -156
acc +16
acc +26
acc +8
jmp +282
acc +12
acc +38
acc +37
acc +13
jmp -115
acc -12
acc -1
acc +44
jmp +347
jmp -133
nop +240
acc +27
jmp +321
acc +16
acc -9
jmp +1
jmp +348
jmp +166
acc -7
acc +7
jmp -238
acc +26
acc -5
acc -17
acc +30
jmp -16
acc +34
acc +0
jmp +66
acc +26
acc -7
acc +49
jmp +18
jmp -80
nop -131
jmp +59
acc -18
jmp +1
acc -6
acc +15
jmp -174
acc +50
acc +21
acc +10
jmp -185
acc +49
jmp +66
acc +42
acc +21
jmp +63
acc +38
acc +47
acc +2
jmp +342
acc +19
jmp -224
acc +0
jmp +356
acc +46
acc -17
jmp +82
nop +85
jmp +1
nop +108
jmp -255
jmp -218
acc +43
acc +22
jmp +227
acc +29
acc +25
jmp +155
acc +38
jmp +298
nop -74
acc +23
acc -13
jmp -77
acc -12
acc +22
acc +30
acc -10
jmp +225
acc +48
jmp +190
acc +24
jmp +1
acc +42
nop -10
jmp +226
acc +0
acc +40
acc +48
jmp -311
acc -6
jmp -168
jmp -70
jmp +1
acc -1
nop -210
jmp +186
acc +28
acc +15
jmp -191
jmp -158
nop +23
jmp +263
acc +7
acc +46
jmp -121
acc +37
jmp -272
jmp +1
acc +27
acc +23
acc +0
jmp -233
acc +2
acc -2
acc +34
jmp -75
acc +12
acc +39
jmp -196
nop -30
acc +42
acc +45
jmp -318
acc +15
acc +2
jmp +1
jmp -27
acc +14
jmp +1
acc +41
jmp -310
jmp -15
acc +43
acc -5
jmp -130
acc +44
jmp +85
acc -2
acc +19
jmp -164
jmp +26
nop -39
jmp +238
jmp -227
jmp +1
jmp -46
acc -1
jmp -305
acc +43
acc -4
acc -2
acc +30
jmp +251
jmp +1
acc -6
acc +47
nop +94
jmp -337
nop +80
acc +9
jmp -139
acc +17
acc +20
acc +0
acc +22
jmp -24
acc -19
acc +4
acc +19
jmp -21
acc +2
nop -337
acc -12
jmp -331
acc +21
jmp +46
acc +44
jmp -293
acc +30
acc +4
jmp -124
nop -101
acc -9
jmp +12
acc +0
acc +16
acc +16
acc -5
jmp -121
nop -267
jmp -110
acc +32
acc -11
jmp -283
jmp -95
acc +36
acc +24
nop -222
jmp -236
acc +0
acc +0
acc +0
acc +32
jmp +205
nop -176
acc -5
acc -5
nop -156
jmp +68
nop -367
acc -2
acc +9
acc +42
jmp -251
jmp +1
nop -409
acc -18
acc +30
jmp -372
acc -15
jmp +155
nop -353
acc +26
acc +28
jmp -434
acc +48
nop +33
acc +12
nop -303
jmp +21
acc +36
acc +40
acc +21
nop -101
jmp -421
acc +32
acc -10
jmp -254
acc -18
jmp -159
acc +3
nop -93
acc +13
nop -417
jmp -334
acc +36
nop -305
acc +30
jmp +102
jmp -160
acc +7
jmp +77
nop -345
jmp +65
acc +16
acc +42
jmp -450
acc -13
jmp +106
acc +11
acc +14
acc +37
acc +11
jmp -421
acc +2
acc +16
acc +29
acc +8
jmp -201
acc +48
jmp -112
acc -17
jmp +1
nop -460
nop +129
jmp -186
acc -12
jmp -340
jmp +1
acc +7
jmp -276
acc +49
acc +29
acc +1
acc +43
jmp -360
acc +24
nop -413
nop -378
jmp -68
nop +74
jmp +104
acc +38
acc +36
acc +3
jmp -117
acc -11
nop -153
acc -13
nop -125
jmp -126
jmp +79
acc -9
acc +39
jmp -373
acc +40
acc +46
nop -436
acc +38
jmp -347
acc -18
acc -4
acc -4
jmp -190
acc +36
acc +21
nop -482
jmp -286
acc -15
acc +10
acc +5
acc +17
jmp -509
acc +41
acc +0
acc -6
acc -2
jmp -344
acc +2
acc -8
acc +28
jmp -190
acc +5
acc -17
acc +0
nop -545
jmp -234
jmp -286
acc +32
jmp -21
nop +1
nop -1
acc +1
nop -465
jmp +68
acc +22
acc +46
nop +45
acc -3
jmp -309
acc +45
jmp +1
nop -377
jmp -261
acc +18
acc +17
acc +5
nop -336
jmp -327
nop -397
jmp -492
acc -11
acc +21
jmp -35
jmp -169
jmp -403
acc +40
acc -3
acc -3
acc -11
jmp -357
acc +11
jmp +1
acc -13
jmp -467
acc +3
jmp -198
nop -211
acc +0
jmp -481
acc -2
nop +27
acc +28
acc +21
jmp -247
acc -14
acc -12
acc +39
acc +4
jmp -61
jmp -274
jmp -299
nop -538
jmp -437
jmp -540
acc +38
acc +14
acc +16
jmp -572
acc +8
acc +21
acc +34
jmp +1
acc +3
jmp -488
acc -19
nop -375
jmp -126
acc +7
acc +46
jmp -308
jmp -52
acc +14
acc +23
acc -3
nop -375
jmp +1'''

lines = input.split('\n')

# +
# part 1
visited = [False] * len(lines)

lineno = 0
acc = 0

while lineno < len(lines):
    if visited[lineno]:
        break
    visited[lineno] = True
    inst = lines[lineno].split(' ')
    op = inst[0]
    arg = int(inst[1])
    if op == 'nop':
        lineno += 1
    elif op == 'acc':
        acc += arg
        lineno += 1
    elif op == 'jmp':
        lineno += arg

print(acc)


# +
# part 2

def run(instructions, change):
    visited = [False] * len(lines)
    lineno = 0
    acc = 0
    exits = True
    
    while lineno < len(lines):
        if visited[lineno]:
            exits = False
            break
        visited[lineno] = True
        inst = lines[lineno].split(' ')
        op = inst[0]
        arg = int(inst[1])
        if change == lineno:
            if op == 'nop':
                op = 'jmp'
            elif op == 'jmp':
                op = 'nop'
        if op == 'nop':
            lineno += 1
        elif op == 'acc':
            acc += arg
            lineno += 1
        elif op == 'jmp':
            lineno += arg
    
    return exits, acc

for i in range(len(lines)):
    if 'acc' in lines[i]:
        continue
    else:
        exits, acc = run(lines, i)
        if exits:
            print(i, lines[i], acc)
            break
# -



StarcoderdataJupyterScriptsDedupFiltered