Skip to content

Instantly share code, notes, and snippets.

@abra3607
Created December 28, 2023 18:13
Show Gist options
  • Select an option

  • Save abra3607/71306f5e30c0e5eb11f9102ce1e6c626 to your computer and use it in GitHub Desktop.

Select an option

Save abra3607/71306f5e30c0e5eb11f9102ce1e6c626 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from numpy import *\n",
"import matplotlib\n",
"from matplotlib import pyplot as plt\n",
"from tqdm.auto import tqdm\n",
"import requests\n",
"import pickle\n",
"import re\n",
"import itertools\n",
"import functools\n",
"import collections\n",
"import string\n",
"import time\n",
"from bs4 import BeautifulSoup\n",
"from sortedcontainers import SortedList # http://www.grantjenks.com/docs/sortedcontainers/sortedlist.html#sortedlist\n",
"\n",
"import multiprocess\n",
"# with multiprocess.Pool(processes=8) as pool:\n",
"# for i in pool.imap_unordered(f, a):\n",
"# print(i)\n",
"\n",
"from adventlib.common import *\n",
"# n, m, a = lines_as_matrix_nm(lines)\n",
"# a = lines_as_matrix(lines)\n",
"# a = lines_as_digit_matrix(lines)\n",
"# chunks = split_iter_by_start_indices(a, indices)\n",
"# chunks = split_iter_by_bools(a, bools)\n",
"\n",
"# from adventlib.point_2d import *\n",
"# from adventlib.point_3d import *\n",
"\n",
"YEAR = 2023\n",
"DAY = int('20')\n",
"\n",
"submit1, submit2 = generate_submits(YEAR, DAY)\n",
"\n",
"while True:\n",
" try:\n",
" raw_input = get_input(YEAR, DAY)\n",
" break\n",
" except Exception as e:\n",
" print(e)\n",
" time.sleep(1)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"lines, groups = linesplit(\"\"\"\n",
"broadcaster -> a\n",
"%a -> inv, con\n",
"&inv -> b\n",
"%b -> con\n",
"&con -> output\n",
"\"\"\")"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"58 lines\n",
"1 groups\n",
">>>\n",
"%fx -> kh, hl\n",
"%nv -> fx, hl\n",
"&vm -> zg\n",
"...\n"
]
}
],
"source": [
"lines, groups = linesplit(raw_input, verbose=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"edges = {}\n",
"inputs = collections.defaultdict(list)\n",
"types = {}\n",
"\n",
"for line in lines:\n",
" module, targets = line.split(' -> ')\n",
" targets = targets.split(', ')\n",
" \n",
" if module[0] == '%' or module[0] == '&':\n",
" typ = module[0]\n",
" module = module[1:]\n",
" else:\n",
" typ = '*'\n",
" \n",
" types[module] = typ\n",
" edges[module] = targets\n",
" \n",
" for target in targets:\n",
" inputs[target].append(module)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"states = {}\n",
"for module, typ in types.items():\n",
" match typ:\n",
" case '%':\n",
" states[module] = 0\n",
" case '&':\n",
" states[module] = {i: 0 for i in inputs[module]}\n",
"\n",
"stats = [0, 0]\n",
"\n",
"for it in range(1000):\n",
" q = collections.deque()\n",
"\n",
" q.append(('button', 'broadcaster', 0))\n",
" stats[0] += 1\n",
"\n",
" while len(q) > 0:\n",
" origin, module, pulse = q.popleft()\n",
"\n",
" match types.get(module, ''):\n",
" case '*':\n",
" to_send = pulse\n",
" for t in edges[module]:\n",
" q.append((module, t, to_send))\n",
" stats[to_send] += 1\n",
"\n",
" case '%':\n",
" if pulse == 1:\n",
" pass\n",
" else:\n",
" states[module] = states[module] ^ 1\n",
" to_send = states[module]\n",
" for t in edges[module]:\n",
" q.append((module, t, to_send))\n",
" stats[to_send] += 1\n",
"\n",
" case '&':\n",
" states[module][origin] = pulse\n",
" all_high = all([i == 1 for i in states[module].values()])\n",
"\n",
" to_send = int(all_high) ^ 1\n",
" for t in edges[module]:\n",
" q.append((module, t, to_send))\n",
" stats[to_send] += 1"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[17529, 47116]"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"stats"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"825896364"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.product(stats)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Submitting 825896364...\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"submit1(_)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "aeb52611cb834fa5a0eb4ec111d8f35c",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/999999999 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[41], line 39\u001b[0m\n\u001b[1;32m 37\u001b[0m \u001b[38;5;28;01mcase\u001b[39;00m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m&\u001b[39m\u001b[38;5;124m'\u001b[39m:\n\u001b[1;32m 38\u001b[0m states[module][origin] \u001b[38;5;241m=\u001b[39m pulse\n\u001b[0;32m---> 39\u001b[0m all_high \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mall\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[43mi\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m==\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mi\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mstates\u001b[49m\u001b[43m[\u001b[49m\u001b[43mmodule\u001b[49m\u001b[43m]\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvalues\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 41\u001b[0m to_send \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mint\u001b[39m(all_high) \u001b[38;5;241m^\u001b[39m \u001b[38;5;241m1\u001b[39m\n\u001b[1;32m 42\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m t \u001b[38;5;129;01min\u001b[39;00m edges[module]:\n",
"File \u001b[0;32m<__array_function__ internals>:180\u001b[0m, in \u001b[0;36mall\u001b[0;34m(*args, **kwargs)\u001b[0m\n",
"File \u001b[0;32m~/projects/jupyter/.venv/lib/python3.11/site-packages/numpy/core/fromnumeric.py:2489\u001b[0m, in \u001b[0;36mall\u001b[0;34m(a, axis, out, keepdims, where)\u001b[0m\n\u001b[1;32m 2406\u001b[0m \u001b[38;5;129m@array_function_dispatch\u001b[39m(_all_dispatcher)\n\u001b[1;32m 2407\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mall\u001b[39m(a, axis\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m, out\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m, keepdims\u001b[38;5;241m=\u001b[39mnp\u001b[38;5;241m.\u001b[39m_NoValue, \u001b[38;5;241m*\u001b[39m, where\u001b[38;5;241m=\u001b[39mnp\u001b[38;5;241m.\u001b[39m_NoValue):\n\u001b[1;32m 2408\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 2409\u001b[0m \u001b[38;5;124;03m Test whether all array elements along a given axis evaluate to True.\u001b[39;00m\n\u001b[1;32m 2410\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 2487\u001b[0m \n\u001b[1;32m 2488\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m-> 2489\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_wrapreduction\u001b[49m\u001b[43m(\u001b[49m\u001b[43ma\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mlogical_and\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mall\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mout\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 2490\u001b[0m \u001b[43m \u001b[49m\u001b[43mkeepdims\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mkeepdims\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mwhere\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mwhere\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/projects/jupyter/.venv/lib/python3.11/site-packages/numpy/core/fromnumeric.py:86\u001b[0m, in \u001b[0;36m_wrapreduction\u001b[0;34m(obj, ufunc, method, axis, dtype, out, **kwargs)\u001b[0m\n\u001b[1;32m 83\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 84\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m reduction(axis\u001b[38;5;241m=\u001b[39maxis, out\u001b[38;5;241m=\u001b[39mout, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mpasskwargs)\n\u001b[0;32m---> 86\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mufunc\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mreduce\u001b[49m\u001b[43m(\u001b[49m\u001b[43mobj\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdtype\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mout\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mpasskwargs\u001b[49m\u001b[43m)\u001b[49m\n",
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
]
}
],
"source": [
"states = {}\n",
"for module, typ in types.items():\n",
" match typ:\n",
" case '%':\n",
" states[module] = 0\n",
" case '&':\n",
" states[module] = {i: 0 for i in inputs[module]}\n",
"\n",
"history = []\n",
" \n",
"for it in tqdm(range(1, 1000000000)):\n",
" q = collections.deque()\n",
"\n",
" q.append(('button', 'broadcaster', 0))\n",
" send_history = collections.defaultdict(list)\n",
"\n",
" while len(q) > 0:\n",
" origin, module, pulse = q.popleft()\n",
"\n",
" match types.get(module, ''):\n",
" case '*':\n",
" to_send = pulse\n",
" for t in edges[module]:\n",
" q.append((module, t, to_send))\n",
" send_history[t].append(to_send)\n",
"\n",
" case '%':\n",
" if pulse == 1:\n",
" pass\n",
" else:\n",
" states[module] = states[module] ^ 1\n",
" to_send = states[module]\n",
" for t in edges[module]:\n",
" q.append((module, t, to_send))\n",
" send_history[t].append(to_send)\n",
"\n",
" case '&':\n",
" states[module][origin] = pulse\n",
" all_high = all([i == 1 for i in states[module].values()])\n",
"\n",
" to_send = int(all_high) ^ 1\n",
" for t in edges[module]:\n",
" q.append((module, t, to_send))\n",
" send_history[t].append(to_send)\n",
" \n",
" history.append(list(send_history['rx']))\n",
" if 0 in send_history['rx']:\n",
" print(iter)\n",
" break"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1, 1, 1]]"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"history[:100]"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"pct_fx -> pct_kh, amp_hl\n",
"pct_nv -> pct_fx, amp_hl\n",
"amp_vm -> amp_zg\n",
"pct_rr -> pct_rl, amp_ql\n",
"amp_bc -> pct_rn, pct_pm, pct_rp, pct_xg, amp_fv, pct_tn\n",
"pct_xg -> pct_xm\n",
"pct_kl -> amp_hl\n",
"amp_hq -> pct_xt, pct_sp, pct_gl, amp_jd, pct_jl, pct_ss, pct_mc\n",
"pct_xn -> pct_mv, amp_bc\n",
"pct_dp -> pct_hh, amp_ql\n",
"pct_mv -> amp_bc, pct_pp\n",
"pct_rg -> amp_hq, pct_zq\n",
"amp_lm -> amp_zg\n",
"pct_mc -> pct_xt\n",
"pct_ct -> pct_rd\n",
"pct_ss -> pct_mz\n",
"pct_rt -> pct_kk, amp_hl\n",
"pct_mz -> amp_hq, pct_sp\n",
"pct_zq -> amp_hq, pct_qj\n",
"pct_rn -> pct_pm\n",
"pct_kk -> pct_ld\n",
"pct_hh -> amp_ql, pct_vb\n",
"pct_kc -> pct_kl, amp_hl\n",
"pct_pm -> pct_tt\n",
"pct_fh -> amp_bc, pct_jn\n",
"amp_jd -> amp_zg\n",
"broadcaster -> pct_rt, pct_jr, pct_rp, pct_jl\n",
"pct_cp -> amp_ql, pct_ln\n",
"amp_fv -> amp_zg\n",
"amp_ql -> pct_ln, pct_jr, pct_xs, pct_mg, amp_vm\n",
"pct_xm -> amp_bc, pct_xn\n",
"pct_xt -> pct_ss\n",
"pct_mg -> pct_lt\n",
"pct_ln -> pct_rr\n",
"pct_qj -> amp_hq\n",
"pct_ld -> pct_nv\n",
"pct_pp -> amp_bc\n",
"pct_gl -> pct_mc\n",
"pct_rd -> amp_hl, pct_zz\n",
"pct_jl -> pct_js, amp_hq\n",
"pct_jr -> pct_xs, amp_ql\n",
"pct_jn -> amp_bc, pct_tn\n",
"pct_sp -> pct_gh\n",
"pct_vb -> amp_ql\n",
"pct_gh -> pct_rg, amp_hq\n",
"pct_vh -> amp_ql, pct_dp\n",
"pct_js -> pct_gl, amp_hq\n",
"pct_kh -> pct_ck\n",
"amp_hl -> pct_kh, pct_rt, pct_ct, pct_kk, amp_lm, pct_ld\n",
"pct_rp -> pct_fh, amp_bc\n",
"pct_tt -> pct_xg, amp_bc\n",
"pct_xs -> pct_mg\n",
"pct_lt -> amp_ql, pct_cp\n",
"pct_zz -> amp_hl, pct_kc\n",
"pct_tn -> pct_rn\n",
"pct_ck -> amp_hl, pct_ct\n",
"pct_rl -> amp_ql, pct_vh\n",
"amp_zg -> rx\n"
]
}
],
"source": [
"def t(module):\n",
" typ = types.get(module, \"\")\n",
" if typ == '%':\n",
" typ = 'pct_'\n",
" if typ == '&':\n",
" typ = 'amp_'\n",
" if typ == '*':\n",
" typ = ''\n",
" \n",
" return f'{typ}{module}'\n",
"\n",
"for module, neighs in edges.items():\n",
" \n",
" print(f'{t(module)} -> ', end='')\n",
" ss = [f'{t(i)}' for i in neighs]\n",
" print(', '.join(ss))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "8035c8118e1041ea8d2c3ac13d0dec05",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/999999999 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"3907\n",
"7814\n",
"11721\n",
"15628\n",
"19535\n",
"23442\n",
"27349\n",
"31256\n",
"35163\n",
"39070\n",
"42977\n",
"46884\n",
"50791\n",
"54698\n",
"58605\n"
]
},
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[55], line 20\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(q) \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[1;32m 18\u001b[0m origin, module, pulse \u001b[38;5;241m=\u001b[39m q\u001b[38;5;241m.\u001b[39mpopleft()\n\u001b[0;32m---> 20\u001b[0m \u001b[38;5;28;01mmatch\u001b[39;00m types\u001b[38;5;241m.\u001b[39mget(module, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m'\u001b[39m):\n\u001b[1;32m 21\u001b[0m \u001b[38;5;28;01mcase\u001b[39;00m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m*\u001b[39m\u001b[38;5;124m'\u001b[39m:\n\u001b[1;32m 22\u001b[0m to_send \u001b[38;5;241m=\u001b[39m pulse\n",
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
]
}
],
"source": [
"states = {}\n",
"for module, typ in types.items():\n",
" match typ:\n",
" case '%':\n",
" states[module] = 0\n",
" case '&':\n",
" states[module] = {i: 0 for i in inputs[module]}\n",
"\n",
"history = []\n",
" \n",
"for it in tqdm(range(1, 1000000000)):\n",
" q = collections.deque()\n",
"\n",
" q.append(('broadcaster', 'jl', 0))\n",
" send_history = collections.defaultdict(list)\n",
"\n",
" while len(q) > 0:\n",
" origin, module, pulse = q.popleft()\n",
"\n",
" match types.get(module, ''):\n",
" case '*':\n",
" to_send = pulse\n",
" for t in edges[module]:\n",
" q.append((module, t, to_send))\n",
" send_history[t].append(to_send)\n",
"\n",
" case '%':\n",
" if pulse == 1:\n",
" pass\n",
" else:\n",
" states[module] = states[module] ^ 1\n",
" to_send = states[module]\n",
" for t in edges[module]:\n",
" q.append((module, t, to_send))\n",
" send_history[t].append(to_send)\n",
"\n",
" case '&':\n",
" states[module][origin] = pulse\n",
" all_high = all([i == 1 for i in states[module].values()])\n",
"\n",
" to_send = int(all_high) ^ 1\n",
" for t in edges[module]:\n",
" q.append((module, t, to_send))\n",
" send_history[t].append(to_send)\n",
" \n",
" # history.append(list(send_history['zg']))\n",
" if 1 in send_history['zg']:\n",
" print(it)\n",
" history.append(it)\n",
" # break"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(array([ 3907, 7814, 11721, 15628, 19535, 23442, 27349, 31256, 35163,\n",
" 39070, 42977, 46884, 50791, 54698, 58605]),\n",
" array([3907, 3907, 3907, 3907, 3907, 3907, 3907, 3907, 3907, 3907, 3907,\n",
" 3907, 3907, 3907]))"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"history = np.array(history)\n",
"history, history[1:] - history[:-1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"jr: 4057\n",
"rt: 3929\n",
"rp: 3911\n",
"jl: 3907"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"243566897206981"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.product([4057,\n",
"3929,\n",
"3911,\n",
"3907,])"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Submitting 243566897206981...\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"submit2(_)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment