Finished Advent 2017
This commit is contained in:
@@ -2511,7 +2511,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 70,
|
"execution_count": 125,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
@@ -2529,7 +2529,8 @@
|
|||||||
" d = perform(dance, d)\n",
|
" d = perform(dance, d)\n",
|
||||||
" if d in seen:\n",
|
" if d in seen:\n",
|
||||||
" print(d, 'is seen in iterations', (seen[d], i))\n",
|
" print(d, 'is seen in iterations', (seen[d], i))\n",
|
||||||
" break"
|
" break\n",
|
||||||
|
" seen[d] = i"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -4016,7 +4017,7 @@
|
|||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"The `numba.jit` decorator really helps here, speeding up execution from 13 seconds to 1 second. It also helped on Day 15, but not as dramatically, and was not able to help on Day 22.\n",
|
"The `numba.jit` decorator really helps here, speeding up execution from 13 seconds to 1 second. It also helped on Day 15, but not as dramatically, and was not able to help on Day 15 or Day 22.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# [Day 24](https://adventofcode.com/2017/day/24): Electromagnetic Moat\n",
|
"# [Day 24](https://adventofcode.com/2017/day/24): Electromagnetic Moat\n",
|
||||||
@@ -4193,7 +4194,7 @@
|
|||||||
" for c in ctable[port] - chain:\n",
|
" for c in ctable[port] - chain:\n",
|
||||||
" # Update chain, port, strength\n",
|
" # Update chain, port, strength\n",
|
||||||
" # then recurse and possibly update best_strength\n",
|
" # then recurse and possibly update best_strength\n",
|
||||||
" # then backtrack and rest chain, port, strength\n",
|
" # then backtrack and restore chain, port, strength\n",
|
||||||
" chain.add(c)\n",
|
" chain.add(c)\n",
|
||||||
" port = other_port(c, port)\n",
|
" port = other_port(c, port)\n",
|
||||||
" strength += sum(c)\n",
|
" strength += sum(c)\n",
|
||||||
@@ -4220,7 +4221,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"# [Day 25](https://adventofcode.com/2017/day/25): The Halting Problem\n",
|
"# [Day 25](https://adventofcode.com/2017/day/25): The Halting Problem\n",
|
||||||
"\n",
|
"\n",
|
||||||
"I won't write a parser for my input; instead I'll translate it into a `dict`:"
|
"I won't write a parser for my input; instead I'll translate it into a `dict` by hand:"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -4235,13 +4236,12 @@
|
|||||||
" \"machine()[state][value] == (new_value, move, new_state)}\"\n",
|
" \"machine()[state][value] == (new_value, move, new_state)}\"\n",
|
||||||
" L, R = -1, +1\n",
|
" L, R = -1, +1\n",
|
||||||
" A, B, C, D, E, F = 'ABCDEF'\n",
|
" A, B, C, D, E, F = 'ABCDEF'\n",
|
||||||
" return {\n",
|
" return {A: ((1, R, B), (0, L, C)),\n",
|
||||||
" A: ((1, R, B), (0, L, C)),\n",
|
" B: ((1, L, A), (1, R, D)),\n",
|
||||||
" B: ((1, L, A), (1, R, D)),\n",
|
" C: ((0, L, B), (0, L, E)),\n",
|
||||||
" C: ((0, L, B), (0, L, E)),\n",
|
" D: ((1, R, A), (0, R, B)),\n",
|
||||||
" D: ((1, R, A), (0, R, B)),\n",
|
" E: ((1, L, F), (1, L, C)),\n",
|
||||||
" E: ((1, L, F), (1, L, C)),\n",
|
" F: ((1, R, D), (1, R, A))}"
|
||||||
" F: ((1, R, D), (1, R, A))}"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -4268,7 +4268,7 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"def turing(machine, state='A', steps=12667664):\n",
|
"def turing(machine, state, steps):\n",
|
||||||
" \"Run the Turing machine for given number of steps, then return tape.\"\n",
|
" \"Run the Turing machine for given number of steps, then return tape.\"\n",
|
||||||
" tape = defaultdict(int)\n",
|
" tape = defaultdict(int)\n",
|
||||||
" cursor = 0\n",
|
" cursor = 0\n",
|
||||||
@@ -4277,7 +4277,36 @@
|
|||||||
" cursor += move\n",
|
" cursor += move\n",
|
||||||
" return tape\n",
|
" return tape\n",
|
||||||
"\n",
|
"\n",
|
||||||
"sum(turing(machine()).values())"
|
"sum(turing(machine(), 'A', 12667664).values())"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 128,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"4770"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 128,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"def turing(machine, state):\n",
|
||||||
|
" \"Run the Turing machine, starting at state; yield tape each step.\"\n",
|
||||||
|
" tape = defaultdict(int)\n",
|
||||||
|
" cursor = 0\n",
|
||||||
|
" while True:\n",
|
||||||
|
" tape[cursor], move, state = machine[state][tape[cursor]]\n",
|
||||||
|
" cursor += move\n",
|
||||||
|
" yield tape\n",
|
||||||
|
"\n",
|
||||||
|
"quantify(nth(turing(machine(), 'A'), 12667664).values())"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -4397,7 +4426,7 @@
|
|||||||
" run23_2() == 913,\n",
|
" run23_2() == 913,\n",
|
||||||
" 24: lambda: strongest_chain() == 1695 and\n",
|
" 24: lambda: strongest_chain() == 1695 and\n",
|
||||||
" strength(max(chains(), key=length_and_strength)) == 1673,\n",
|
" strength(max(chains(), key=length_and_strength)) == 1673,\n",
|
||||||
" 25: lambda: sum(turing(machine()).values()) == 4769\n",
|
" 25: lambda: quantify(nth(turing(machine(), 'A'), 12667664).values()) == 4769\n",
|
||||||
"})"
|
"})"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user