|
| 1 | +with open('test-input') as t: |
| 2 | + data = t.read() |
| 3 | + |
| 4 | +def list_of_lists(length): |
| 5 | + list = [] |
| 6 | + for x in range(0, length): |
| 7 | + list.append([]) |
| 8 | + return list |
| 9 | + |
| 10 | +monkeys = [] |
| 11 | +for monkey in data.split('\n\n'): |
| 12 | + monkeys.append(monkey.split('\n')) |
| 13 | + |
| 14 | + |
| 15 | +rounds = 20 |
| 16 | +inspections = [] |
| 17 | +for x in range(0, len(monkeys)): |
| 18 | + inspections.append(0) |
| 19 | +for r in range(0, rounds): |
| 20 | + mks = list_of_lists(len(monkeys)) |
| 21 | + for monkey in monkeys: |
| 22 | + m = monkey[2].split(' ') |
| 23 | + inspections[int(monkey[0].split(' ')[-1].strip(':'))] += len(monkey[1].split(':')[1].split(', ')) |
| 24 | + if monkey[1].split(':')[1].split(', ') != ['']: |
| 25 | + for item in monkey[1].split(':')[1].split(', '): |
| 26 | + if m[-1].isdigit() and m[-3] == 'old': |
| 27 | + if m[-2] == '+': |
| 28 | + worry = int(item)+int(m[-1]) |
| 29 | + elif m[-2] == '*': |
| 30 | + worry = int(item)*int(m[-1]) |
| 31 | + elif m[-1] == 'old' and m[-3] == 'old': |
| 32 | + worry = int(item)*int(item) |
| 33 | + # worry = round(worry/3) |
| 34 | + worry = int(str(worry/3).split('.')[0]) |
| 35 | + """ |
| 36 | + if worry%int(monkey[3].split(' ')[-1]) == 0: |
| 37 | + if monkeys[int(monkey[4].split(' ')[-1])][1].split(':')[1] == '': |
| 38 | + monkeys[int(monkey[4].split(' ')[-1])][1] += str(worry) |
| 39 | + else: |
| 40 | + monkeys[int(monkey[4].split(' ')[-1])][1] +=', '+str(worry) |
| 41 | + else: |
| 42 | + if monkeys[int(monkey[5].split(' ')[-1])][1].split(':')[1] == '': |
| 43 | + monkeys[int(monkey[5].split(' ')[-1])][1] += str(worry) |
| 44 | + else: |
| 45 | + monkeys[int(monkey[5].split(' ')[-1])][1] +=', '+str(worry) |
| 46 | + """ |
| 47 | + if worry%int(monkey[3].split(' ')[-1]) == 0: |
| 48 | + mks[int(monkey[4].split(' ')[-1])].append(worry) |
| 49 | + else: |
| 50 | + mks[int(monkey[5].split(' ')[-1])].append(worry) |
| 51 | + # i += 1 |
| 52 | + # inspections[int(monkey[0].split(' ')[-1].strip(':'))] += i |
| 53 | + monkey[1] = monkey[1].split(':')[0]+':' |
| 54 | + |
| 55 | + for monkey in range(0, len(monkeys)): |
| 56 | + if mks[monkey] != []: |
| 57 | + if monkeys[monkey][1].split(':')[-1] != '': |
| 58 | + monkeys[monkey][1].split(':')[-1] += ', ' |
| 59 | + for k in range(0, len(mks[monkey])): |
| 60 | + monkeys[monkey][1] += str(mks[monkey][k]) |
| 61 | + if k+1 != len(mks[monkey]): |
| 62 | + monkeys[monkey][1] += ', ' |
| 63 | + |
| 64 | + |
| 65 | + """ |
| 66 | + for m in range(0, len(mks)): |
| 67 | + print(m) |
| 68 | + if monkeys[int(monkeys[m][2].split(' ')[-1])][1].split(':')[1] != '': |
| 69 | + monkeys[int(monkeys[m][2].splot(' ')[-1])][1] += ', ' |
| 70 | + for k in range(0, len(mks[m])): |
| 71 | + monkeys += str(mks[m][k]) |
| 72 | + if k+1 != len(mks[m]): |
| 73 | + monkeys += ', ' |
| 74 | + """ |
| 75 | + |
| 76 | +print(inspections) |
0 commit comments