Unable to load image

ADVENT OF CODE DAY 11: MONKEY BUSINESS :marseymonke:

Those FRICKING monkeys stole your stuff! Get it back! :soycry: :!marseymonke:

(Also, remember to leave a comment if you did something interesting :marseylove:)

35
Jump in the discussion.

No email address required.

anyone who wants nice :marseyfingergoodjob: data :marseychartgaussian: prep code... lol no

from y2022.scaffold import *

class Day11(Day):
	def __init__(self):
		super()
	
	@property
	def day(self): return :marseymonke: 11

	def prepare_data(self) -> Any:
		data = self.get_data().split('\n\n')
		#data = open('y2022/day11test.txt').read().split('\n\n')
		data2 = []
		for i in range(len(data)):
			key = i
			monkey_raw = data[i]
			monkey_raw2 = data[i].splitlines()
			monkey2 = SimpleNamespace()
			monkey2.number = key
			for j in range(len(monkey_raw2)):
				#print(monkey_raw[j])
				#if i > 7: continue
				if j == 1:
					if i == 0: monkey2.items = [59, 74, 65, 86]
					if i == 1: monkey2.items = [62, 84, 72, 91, 68, 78, 51]
					if i == 2: monkey2.items = [78, 84, 96]
					if i == 3: monkey2.items = [97, 86]
					if i == 4: monkey2.items = [50]
					if i == 5: monkey2.items = [73, 65, 69, 65, 51]
					if i == 6: monkey2.items = [69, 82, 97, 93, 82, 84, 58, 63]
					if i == 7: monkey2.items = [81, 78, 82, 76, 79, 80]
					
					'''### TEST :marseyitsrigged: ###
					if i == 0: monkey2.items = [79, 98]
					if i == 1: monkey2.items = [54, 65, 75, 74]
					if i == 2: monkey2.items = [79, 60, 97]
					if i == 3: monkey2.items = [74]
					### END TEST :marseyitsrigged: ###
					'''
					#if i > 7: raise :marseysuspicious: Exception("TOO MANY :marseymanysuchcases: MONKEYS!!!!!1")
					#idx_start = monkey_raw.find('s: ')
					#idx_end = monkey_raw.
					#items = monkey_raw.split(',')
					#items[0] = items[0].replace('Starting items:', '').strip()
					#monkey2.items = list(map(lambda x:int(x), items))
				elif j == 2:
					#print(monkey_raw2[j])
					idk_anymore = monkey_raw2[j].replace('  Operation: new = old ', '')
					monkey2.op = 'o' if 'old' in idk_anymore else idk_anymore[0]
					idk_anymore = idk_anymore[2:].strip()
					monkey2.operand = int(idk_anymore) if not 'ld' in idk_anymore else 111111111
					#r = search('Operation: new = old {} {:g}', monkey_raw, evaluate_result=True)
					#if not r: raise :marseysuspicious: Exception("COULD NOT PARASES DAHFADGAHGFSG")
					#monkey2.op, monkey2.operand = r[0], r[1]
				elif j == 3:
					r = search('Test: divisible by {:g}{:g}', monkey_raw, evaluate_result=True)
					if r:
						r = r = (int(r[0]) * 10) + int(r[1])
					else:
						r = search('Test: divisible by {:g}', monkey_raw, evaluate_result=True)
					if not r: raise :marseysuspicious: Exception('parse 2')
					monkey2.divisible = int(r[0] if not isinstance(r, int) else r)
				elif j == 4:
					r = search('If true: throw :marseypuke: to monkey :marseybrasileiro: {:g}', monkey_raw, evaluate_result=True)
					if not r: raise :marseysuspicious: Exception('parse 3')
					monkey2.throw_if_true = int(r[0])
				elif j == 5:
					r = search('If false: throw :marseypuke: to monkey :marseybigfoot: {:g}', monkey_raw, evaluate_result=True)
					if not r: raise :marseysuspicious: Exception('parse 4')
					monkey2.throw_if_false = int(r[0])
			monkey2.inspected = 0
			data2.append(monkey2)
		return data2

	def a(self):
		return
		monkeys = self.prepare_data()
		# level of monkey :marseybigfoot: business after 20 rounds
		SIMULATION_ROUNDS = 20
		TOP_FOCUS = 2
		# multiply the top two together
		for round_no in range(SIMULATION_ROUNDS):
			debug = True
			for monkey :marseysopa: in monkeys:
				while monkey.items:
					#if debug:
					#	print(monkey)
					value = int(monkey.items[0])
					old_value = int(monkey.items[0])
					try: operand = int(monkey.operand)
					except: operand = value
					#if debug: repl(locals())
					#print(monkey)
					if monkey.op == '+': value += operand
					elif monkey.op == '*': value *= operand
					elif monkey.op == 'o': value **= 2
					else: raise :marseysuspicious: Exception(f'Unknown operation type {monkey.op}!')

					if debug:
						pass
						#print_filtered_locals(locals())
						#print(f"{old_value} {monkey.op} {operand} = {value}")
						#debug = False

					monkey.inspected += 1
					value //= 3

					if value % monkey.divisible == 0:
						monkeys[monkey.throw_if_true].items.append(value)
					else:
						#print(monkey)
						#print(monkey.throw_if_false)
						#print(monkeys[5])
						monkeys[monkey.throw_if_false].items.append(value)
					del monkey.items[0]

					if debug:
						#continue
						#print(monkey)
						#print(monkeys[3])
						#print("---")
						debug = False
				#print("--------- SIMUL ROUND ----------")
		# 59760 is too low, 63000 is not right :marseyhesright: agbhdsjfsf
		#s = list(monkeys)
		s = list(sorted(monkeys, key=lambda m:m.inspected, reverse=True))
		for item in s[:2]:
			print(item)
	
	def b(self):
		monkeys = self.prepare_data()
		SIMULATION_ROUNDS = 10000
		max2 = 1
		for monkey :marseysopa: in monkeys:
			max2 *= monkey.divisible
		for round_no in range(SIMULATION_ROUNDS):
			if round_no % 2500 == 0: print(f"SIMULATION ROUND {round_no}")
			for monkey :marseybigfoot: in monkeys:
				while monkey.items:
					value = int(monkey.items[0])
					try: operand = int(monkey.operand)
					except: operand = value

					if monkey.op == '+': value += operand
					elif monkey.op == '*': value *= operand
					elif monkey.op == 'o': value *= value# % max2

					#value //= 100000000000
					value %= max2
					#value %= monkey.divisible

					monkey.inspected += 1

					if value % monkey.divisible % monkey.divisible == 0:
						monkeys[monkey.throw_if_true].items.append(value)
					else:
						monkeys[monkey.throw_if_false].items.append(value)# % monkey.divisible)
					del monkey.items[0]
		s = list(map(lambda m:m.inspected, sorted(monkeys, key=lambda m:m.inspected, reverse=True)))
		for item in s[:2]:
			print(item)
		## TOO LOW  8100990030
		## NO HINT :marseywink: 14400239965
		## TOO LOW 14401560030
		## NO HINT :marseywink: 14400959980
		## NO HINT :marseywink: 19203834231 (139343/137817)
		## TOO HIGH :marseycocaine: 22037989580
		## TOO HIGH :marseyrick: 27846764496

no comments removed because fuck :marseyracistgrandpa: you

Jump in the discussion.

No email address required.

Your pulitzer's in the mail

Jump in the discussion.

No email address required.

:marseydunkon:

Jump in the discussion.

No email address required.

My favorite part is how monkey2 represents an arbitrary monkey in a loop and not actually monkey #2.

Jump in the discussion.

No email address required.

me too

Jump in the discussion.

No email address required.



Now playing: Voices of the Temple (DKC).mp3

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.