import sys
import numpy as np

from anytree import Node, RenderTree, AsciiStyle, Walker, PreOrderIter

import copy

infilename = sys.argv[1]
infile = open(infilename,'r')

monkeys = []
monkey_num = -1

# Part A
for line in infile:
    if line.find("Monkey")>=0:
        monkey_num = int(line.split()[1].split(':')[0])
        monkey = {monkey_num: {}}
        monkey['ninspections'] = 0
        monkeys.append(copy.deepcopy(monkey))
    elif line.find("Starting")>=0:
        items = np.array(line.split(':')[1].split(',')).astype(int).tolist()
        monkeys[monkey_num]['items'] = items
    elif line.find("Operation")>=0:
        items = line.split()[-2:]
        monkeys[monkey_num]['operation'] = items
    elif line.find("divisible")>=0:
        items = line.split()[-1]
        monkeys[monkey_num]['divisibleby'] = int(items)
    elif line.find("true")>=0:
        items = line.split()[-1]
        monkeys[monkey_num]['true'] = int(items)
    elif line.find("false")>=0:
        items = line.split()[-1]
        monkeys[monkey_num]['false'] = int(items)


#print(monkeys)
for monkey in monkeys:
    print(monkey)
print()

# Process
# Part A
'''
nrounds = 20
nmonkeys = len(monkeys)
for i in range(nrounds):
    for imon in range(nmonkeys):
        monkey = monkeys[imon]
        nitems = len(monkey['items'])
        for n in range(nitems):
            # Do operation
            item = monkey['items'].pop()
            print(f"{imon}  INSPECTING {item}")
            operation = monkey['operation']
            if operation[-1] == 'old':
                item *= item
            elif operation[0] == '+':
                item += int(operation[1])
            elif operation[0] == '*':
                item *= int(operation[1])

            item = int(np.floor(item/3))

            test = item%monkey['divisibleby']==0

            idx = -1
            if test is True:
                idx = monkey['true']
            else:
                idx = monkey['false']

            monkeys[idx]['items'].insert(0,item)

            monkey['ninspections'] += 1


print()
#print(monkeys)
for monkey in monkeys:
    print(monkey)




nrounds = 20
nmonkeys = len(monkeys)
for i in range(nrounds):
    for imon in range(nmonkeys):
        monkey = monkeys[imon]
        nitems = len(monkey['items'])
        for n in range(nitems):
            # Do operation
            item = monkey['items'].pop()
            print(f"{imon}  INSPECTING {item}")
            operation = monkey['operation']
            if operation[-1] == 'old':
                item *= item
            elif operation[0] == '+':
                item += int(operation[1])
            elif operation[0] == '*':
                item *= int(operation[1])

            item = int(np.floor(item/3))

            test = item%monkey['divisibleby']==0

            idx = -1
            if test is True:
                idx = monkey['true']
            else:
                idx = monkey['false']

            monkeys[idx]['items'].insert(0,item)

            monkey['ninspections'] += 1


print()
#print(monkeys)
for monkey in monkeys:
    print(monkey)
'''

# Part B

product = 19 * 3 * 11 * 17 * 5 * 2 * 13 * 7
#product = 23 * 19 * 13 * 17
print(f"product: {product}")

#exit()

#nrounds = 20
nrounds = 10000
#nrounds = 1
nmonkeys = len(monkeys)
for i in range(nrounds):
    if i%10==0:
        print(i)
        for monkey in monkeys:
            print(monkey)
    for imon in range(nmonkeys):
        monkey = monkeys[imon]
        nitems = len(monkey['items'])
        for n in range(nitems):
            # Do operation
            item = monkey['items'].pop()
            #print(f"{imon}  INSPECTING {item}")
            operation = monkey['operation']
            if operation[-1] == 'old':
                item *= item
                #item = [item,item]
            elif operation[0] == '+':
                item += int(operation[1])
            elif operation[0] == '*':
                item *= int(operation[1])

            #item = int(np.floor(item/3))

            test = item%monkey['divisibleby']==0

            idx = -1
            if test is True:
                idx = monkey['true']
                #print("TRUEEEEEEEEEEEEEEEEEEEEEE")
            else:
                idx = monkey['false']

            # new item
            if item > product:
                item = product + item%product
            monkeys[idx]['items'].insert(0,item)

            monkey['ninspections'] += 1


print()
print("Final results: ---------------------")
#print(monkeys)
for monkey in monkeys:
    print(monkey)





6cad45b6e0783a56116b846e2277e7145a18b510
<mask token>

<mask token>
for line in infile:
    if line.find('Monkey') >= 0:
        monkey_num = int(line.split()[1].split(':')[0])
        monkey = {monkey_num: {}}
        monkey['ninspections'] = 0
        monkeys.append(copy.deepcopy(monkey))
    elif line.find('Starting') >= 0:
        items = np.array(line.split(':')[1].split(',')).astype(int).tolist()
        monkeys[monkey_num]['items'] = items
    elif line.find('Operation') >= 0:
        items = line.split()[-2:]
        monkeys[monkey_num]['operation'] = items
    elif line.find('divisible') >= 0:
        items = line.split()[-1]
        monkeys[monkey_num]['divisibleby'] = int(items)
    elif line.find('true') >= 0:
        items = line.split()[-1]
        monkeys[monkey_num]['true'] = int(items)
    elif line.find('false') >= 0:
        items = line.split()[-1]
        monkeys[monkey_num]['false'] = int(items)
for monkey in monkeys:
    print(monkey)
print()
<mask token>
print(f'product: {product}')
<mask token>
for i in range(nrounds):
    if i % 10 == 0:
        print(i)
        for monkey in monkeys:
            print(monkey)
    for imon in range(nmonkeys):
        monkey = monkeys[imon]
        nitems = len(monkey['items'])
        for n in range(nitems):
            item = monkey['items'].pop()
            operation = monkey['operation']
            if operation[-1] == 'old':
                item *= item
            elif operation[0] == '+':
                item += int(operation[1])
            elif operation[0] == '*':
                item *= int(operation[1])
            test = item % monkey['divisibleby'] == 0
            idx = -1
            if test is True:
                idx = monkey['true']
            else:
                idx = monkey['false']
            if item > product:
                item = product + item % product
            monkeys[idx]['items'].insert(0, item)
            monkey['ninspections'] += 1
print()
print('Final results: ---------------------')
for monkey in monkeys:
    print(monkey)

<mask token>
infilename = sys.argv[1]
infile = open(infilename, 'r')
monkeys = []
monkey_num = -1
for line in infile:
    if line.find('Monkey') >= 0:
        monkey_num = int(line.split()[1].split(':')[0])
        monkey = {monkey_num: {}}
        monkey['ninspections'] = 0
        monkeys.append(copy.deepcopy(monkey))
    elif line.find('Starting') >= 0:
        items = np.array(line.split(':')[1].split(',')).astype(int).tolist()
        monkeys[monkey_num]['items'] = items
    elif line.find('Operation') >= 0:
        items = line.split()[-2:]
        monkeys[monkey_num]['operation'] = items
    elif line.find('divisible') >= 0:
        items = line.split()[-1]
        monkeys[monkey_num]['divisibleby'] = int(items)
    elif line.find('true') >= 0:
        items = line.split()[-1]
        monkeys[monkey_num]['true'] = int(items)
    elif line.find('false') >= 0:
        items = line.split()[-1]
        monkeys[monkey_num]['false'] = int(items)
for monkey in monkeys:
    print(monkey)
print()
<mask token>
product = 19 * 3 * 11 * 17 * 5 * 2 * 13 * 7
print(f'product: {product}')
nrounds = 10000
nmonkeys = len(monkeys)
for i in range(nrounds):
    if i % 10 == 0:
        print(i)
        for monkey in monkeys:
            print(monkey)
    for imon in range(nmonkeys):
        monkey = monkeys[imon]
        nitems = len(monkey['items'])
        for n in range(nitems):
            item = monkey['items'].pop()
            operation = monkey['operation']
            if operation[-1] == 'old':
                item *= item
            elif operation[0] == '+':
                item += int(operation[1])
            elif operation[0] == '*':
                item *= int(operation[1])
            test = item % monkey['divisibleby'] == 0
            idx = -1
            if test is True:
                idx = monkey['true']
            else:
                idx = monkey['false']
            if item > product:
                item = product + item % product
            monkeys[idx]['items'].insert(0, item)
            monkey['ninspections'] += 1
print()
print('Final results: ---------------------')
for monkey in monkeys:
    print(monkey)

import sys
import numpy as np
from anytree import Node, RenderTree, AsciiStyle, Walker, PreOrderIter
import copy
infilename = sys.argv[1]
infile = open(infilename, 'r')
monkeys = []
monkey_num = -1
for line in infile:
    if line.find('Monkey') >= 0:
        monkey_num = int(line.split()[1].split(':')[0])
        monkey = {monkey_num: {}}
        monkey['ninspections'] = 0
        monkeys.append(copy.deepcopy(monkey))
    elif line.find('Starting') >= 0:
        items = np.array(line.split(':')[1].split(',')).astype(int).tolist()
        monkeys[monkey_num]['items'] = items
    elif line.find('Operation') >= 0:
        items = line.split()[-2:]
        monkeys[monkey_num]['operation'] = items
    elif line.find('divisible') >= 0:
        items = line.split()[-1]
        monkeys[monkey_num]['divisibleby'] = int(items)
    elif line.find('true') >= 0:
        items = line.split()[-1]
        monkeys[monkey_num]['true'] = int(items)
    elif line.find('false') >= 0:
        items = line.split()[-1]
        monkeys[monkey_num]['false'] = int(items)
for monkey in monkeys:
    print(monkey)
print()
<mask token>
product = 19 * 3 * 11 * 17 * 5 * 2 * 13 * 7
print(f'product: {product}')
nrounds = 10000
nmonkeys = len(monkeys)
for i in range(nrounds):
    if i % 10 == 0:
        print(i)
        for monkey in monkeys:
            print(monkey)
    for imon in range(nmonkeys):
        monkey = monkeys[imon]
        nitems = len(monkey['items'])
        for n in range(nitems):
            item = monkey['items'].pop()
            operation = monkey['operation']
            if operation[-1] == 'old':
                item *= item
            elif operation[0] == '+':
                item += int(operation[1])
            elif operation[0] == '*':
                item *= int(operation[1])
            test = item % monkey['divisibleby'] == 0
            idx = -1
            if test is True:
                idx = monkey['true']
            else:
                idx = monkey['false']
            if item > product:
                item = product + item % product
            monkeys[idx]['items'].insert(0, item)
            monkey['ninspections'] += 1
print()
print('Final results: ---------------------')
for monkey in monkeys:
    print(monkey)

import sys
import numpy as np

from anytree import Node, RenderTree, AsciiStyle, Walker, PreOrderIter

import copy

infilename = sys.argv[1]
infile = open(infilename,'r')

monkeys = []
monkey_num = -1

# Part A
for line in infile:
    if line.find("Monkey")>=0:
        monkey_num = int(line.split()[1].split(':')[0])
        monkey = {monkey_num: {}}
        monkey['ninspections'] = 0
        monkeys.append(copy.deepcopy(monkey))
    elif line.find("Starting")>=0:
        items = np.array(line.split(':')[1].split(',')).astype(int).tolist()
        monkeys[monkey_num]['items'] = items
    elif line.find("Operation")>=0:
        items = line.split()[-2:]
        monkeys[monkey_num]['operation'] = items
    elif line.find("divisible")>=0:
        items = line.split()[-1]
        monkeys[monkey_num]['divisibleby'] = int(items)
    elif line.find("true")>=0:
        items = line.split()[-1]
        monkeys[monkey_num]['true'] = int(items)
    elif line.find("false")>=0:
        items = line.split()[-1]
        monkeys[monkey_num]['false'] = int(items)


#print(monkeys)
for monkey in monkeys:
    print(monkey)
print()

# Process
# Part A
'''
nrounds = 20
nmonkeys = len(monkeys)
for i in range(nrounds):
    for imon in range(nmonkeys):
        monkey = monkeys[imon]
        nitems = len(monkey['items'])
        for n in range(nitems):
            # Do operation
            item = monkey['items'].pop()
            print(f"{imon}  INSPECTING {item}")
            operation = monkey['operation']
            if operation[-1] == 'old':
                item *= item
            elif operation[0] == '+':
                item += int(operation[1])
            elif operation[0] == '*':
                item *= int(operation[1])

            item = int(np.floor(item/3))

            test = item%monkey['divisibleby']==0

            idx = -1
            if test is True:
                idx = monkey['true']
            else:
                idx = monkey['false']

            monkeys[idx]['items'].insert(0,item)

            monkey['ninspections'] += 1


print()
#print(monkeys)
for monkey in monkeys:
    print(monkey)




nrounds = 20
nmonkeys = len(monkeys)
for i in range(nrounds):
    for imon in range(nmonkeys):
        monkey = monkeys[imon]
        nitems = len(monkey['items'])
        for n in range(nitems):
            # Do operation
            item = monkey['items'].pop()
            print(f"{imon}  INSPECTING {item}")
            operation = monkey['operation']
            if operation[-1] == 'old':
                item *= item
            elif operation[0] == '+':
                item += int(operation[1])
            elif operation[0] == '*':
                item *= int(operation[1])

            item = int(np.floor(item/3))

            test = item%monkey['divisibleby']==0

            idx = -1
            if test is True:
                idx = monkey['true']
            else:
                idx = monkey['false']

            monkeys[idx]['items'].insert(0,item)

            monkey['ninspections'] += 1


print()
#print(monkeys)
for monkey in monkeys:
    print(monkey)
'''

# Part B

product = 19 * 3 * 11 * 17 * 5 * 2 * 13 * 7
#product = 23 * 19 * 13 * 17
print(f"product: {product}")

#exit()

#nrounds = 20
nrounds = 10000
#nrounds = 1
nmonkeys = len(monkeys)
for i in range(nrounds):
    if i%10==0:
        print(i)
        for monkey in monkeys:
            print(monkey)
    for imon in range(nmonkeys):
        monkey = monkeys[imon]
        nitems = len(monkey['items'])
        for n in range(nitems):
            # Do operation
            item = monkey['items'].pop()
            #print(f"{imon}  INSPECTING {item}")
            operation = monkey['operation']
            if operation[-1] == 'old':
                item *= item
                #item = [item,item]
            elif operation[0] == '+':
                item += int(operation[1])
            elif operation[0] == '*':
                item *= int(operation[1])

            #item = int(np.floor(item/3))

            test = item%monkey['divisibleby']==0

            idx = -1
            if test is True:
                idx = monkey['true']
                #print("TRUEEEEEEEEEEEEEEEEEEEEEE")
            else:
                idx = monkey['false']

            # new item
            if item > product:
                item = product + item%product
            monkeys[idx]['items'].insert(0,item)

            monkey['ninspections'] += 1


print()
print("Final results: ---------------------")
#print(monkeys)
for monkey in monkeys:
    print(monkey)




