Skip to content

Commit

Permalink
10th & 11th December
Browse files Browse the repository at this point in the history
  • Loading branch information
lismith2-cisco committed Dec 25, 2022
1 parent f07ff4e commit 8ae80dd
Show file tree
Hide file tree
Showing 5 changed files with 314 additions and 0 deletions.
17 changes: 17 additions & 0 deletions 2022_10_addx_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from itertools import islice


def get_x():
x = 1
with open("2022_10_addx_input.txt") as f:
for line in f:
line = line.strip()
if line == 'noop':
yield x
else:
yield x
yield x
x += int(line.split()[1])


print(sum(cycle * x for cycle, x in islice(enumerate(get_x(), 1), 19, None, 40)))
26 changes: 26 additions & 0 deletions 2022_10_addx_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from itertools import islice


def get_sprite_pixels():
x = 1
with open("2022_10_addx_input.txt") as f:
for line in f:
line = line.strip()
if line == 'noop':
yield (x-1, x, x+1)
else:
yield (x-1, x, x+1)
yield (x-1, x, x+1)
x += int(line.split()[1])


sprite_pixels_iterator = get_sprite_pixels()

for _ in range(6):
output_line = ''
for crt_pos, sprite_pixels in zip(range(40), sprite_pixels_iterator):
if crt_pos in sprite_pixels:
output_line += '#'
else:
output_line += '.'
print(output_line)
138 changes: 138 additions & 0 deletions 2022_10_addx_input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
addx 1
noop
addx 4
noop
noop
addx 7
noop
noop
noop
addx 3
noop
noop
addx 5
addx -1
addx 1
addx 5
addx 3
noop
addx 3
noop
addx -1
noop
addx 3
addx 5
addx -38
addx 7
addx 10
addx -14
addx 5
addx 30
addx -25
noop
addx 2
addx 3
addx -2
addx 2
addx 5
addx 2
addx 2
addx -21
addx 22
addx 5
addx 2
addx 3
noop
addx -39
addx 1
noop
noop
addx 3
addx 5
addx 4
addx -5
addx 4
addx 4
noop
addx -9
addx 12
addx 5
addx 2
addx -1
addx 6
addx -2
noop
addx 3
addx 3
addx 2
addx -37
addx 39
addx -33
addx -1
addx 1
addx 8
noop
noop
noop
addx 2
addx 20
addx -19
addx 4
noop
noop
noop
addx 3
addx 2
addx 5
noop
addx 1
addx 4
addx -21
addx 22
addx -38
noop
noop
addx 7
addx 32
addx -27
noop
addx 3
addx -2
addx 2
addx 5
addx 2
addx 2
addx 3
addx -2
addx 2
noop
addx 3
addx 5
addx 2
addx 3
noop
addx -39
addx 2
noop
addx 4
addx 8
addx -8
addx 6
addx -1
noop
addx 5
noop
noop
noop
addx 3
addx 5
addx 2
addx -11
addx 12
addx 2
noop
addx 3
addx 2
addx 5
addx -6
noop
78 changes: 78 additions & 0 deletions 2022_11_monkeys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import operator
import heapq
import math


class Monkey:
def __init__(self, starting_items, operation, test_value, true_monkey, false_monkey):
self.items = starting_items
self.operation = operation
self.test_value = test_value
self.test = make_test(test_value)
self.true_monkey = true_monkey
self.false_monkey = false_monkey
self.inspection_count = 0

def take_turn(self):
for item in self.items:
self.inspection_count += 1
item = self.operation(item)
item %= mod
if self.test(item):
monkeys[self.true_monkey].items.append(item)
else:
monkeys[self.false_monkey].items.append(item)
self.items.clear()


operators = {
'+': operator.add,
'-': operator.sub,
'*': operator.mul,
}


def make_operation(left, symbol, right):
operator_function = operators[symbol]
left = None if left == 'old' else int(left)
right = None if right == 'old' else int(right)

def operation(old):
return operator_function(old if left is None else left, old if right is None else right)
return operation


def make_test(test_value):
def test(value):
return not value % test_value
return test


monkeys = []

lines = (line.strip() for line in open("2022_11_monkeys_input.txt"))

for line in lines:
if not line:
continue
line = next(lines)
starting_items = [int(item)
for item in line.split("Starting items: ", 1)[1].split(', ')]
line = next(lines)
operation = make_operation(*line.split("Operation: new = ", 1)[1].split())
line = next(lines)
test_value = int(line.split("Test: divisible by ", 1)[1])
line = next(lines)
true_monkey = int(line.split("If true: throw to monkey ", 1)[1])
line = next(lines)
false_monkey = int(line.split("If false: throw to monkey ", 1)[1])
monkeys.append(Monkey(starting_items, operation,
test_value, true_monkey, false_monkey))

mod = math.prod(monkey.test_value for monkey in monkeys)

for _ in range(10000):
for monkey in monkeys:
monkey.take_turn()

print(operator.mul(*heapq.nlargest(2, (monkey.inspection_count for monkey in monkeys))))
55 changes: 55 additions & 0 deletions 2022_11_monkeys_input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
Monkey 0:
Starting items: 83, 97, 95, 67
Operation: new = old * 19
Test: divisible by 17
If true: throw to monkey 2
If false: throw to monkey 7

Monkey 1:
Starting items: 71, 70, 79, 88, 56, 70
Operation: new = old + 2
Test: divisible by 19
If true: throw to monkey 7
If false: throw to monkey 0

Monkey 2:
Starting items: 98, 51, 51, 63, 80, 85, 84, 95
Operation: new = old + 7
Test: divisible by 7
If true: throw to monkey 4
If false: throw to monkey 3

Monkey 3:
Starting items: 77, 90, 82, 80, 79
Operation: new = old + 1
Test: divisible by 11
If true: throw to monkey 6
If false: throw to monkey 4

Monkey 4:
Starting items: 68
Operation: new = old * 5
Test: divisible by 13
If true: throw to monkey 6
If false: throw to monkey 5

Monkey 5:
Starting items: 60, 94
Operation: new = old + 5
Test: divisible by 3
If true: throw to monkey 1
If false: throw to monkey 0

Monkey 6:
Starting items: 81, 51, 85
Operation: new = old * old
Test: divisible by 5
If true: throw to monkey 5
If false: throw to monkey 1

Monkey 7:
Starting items: 98, 81, 63, 65, 84, 71, 84
Operation: new = old + 3
Test: divisible by 2
If true: throw to monkey 2
If false: throw to monkey 3

0 comments on commit 8ae80dd

Please sign in to comment.