From 7b1efc0d9cae231c27edd8e9aaf8a90a07cdc83f Mon Sep 17 00:00:00 2001 From: Defelo Date: Sun, 29 Oct 2023 13:29:25 +0100 Subject: [PATCH] [Python/2017] Move solutions into .py files --- Python/2017/01.ipynb | 149 ---------------------- Python/2017/01.py | 10 ++ Python/2017/02.ipynb | 162 ------------------------ Python/2017/02.py | 27 ++++ Python/2017/03.ipynb | 177 -------------------------- Python/2017/03.py | 39 ++++++ Python/2017/04.ipynb | 156 ----------------------- Python/2017/04.py | 19 +++ Python/2017/05.ipynb | 167 ------------------------- Python/2017/05.py | 32 +++++ Python/2017/06.ipynb | 177 -------------------------- Python/2017/06.py | 37 ++++++ Python/2017/07.ipynb | 182 --------------------------- Python/2017/07.py | 52 ++++++++ Python/2017/08.ipynb | 162 ------------------------ Python/2017/08.py | 27 ++++ Python/2017/09.ipynb | 191 ---------------------------- Python/2017/09.py | 54 ++++++++ Python/2017/10.ipynb | 187 ---------------------------- Python/2017/10.py | 55 ++++++++ Python/2017/11.ipynb | 180 --------------------------- Python/2017/11.py | 42 +++++++ Python/2017/12.ipynb | 190 ---------------------------- Python/2017/12.py | 42 +++++++ Python/2017/13.ipynb | 178 -------------------------- Python/2017/13.py | 56 +++++++++ Python/2017/14.ipynb | 216 -------------------------------- Python/2017/14.py | 60 +++++++++ Python/2017/15.ipynb | 141 --------------------- Python/2017/15.py | 35 ++++++ Python/2017/16.ipynb | 177 -------------------------- Python/2017/16.py | 38 ++++++ Python/2017/17.ipynb | 160 ------------------------ Python/2017/17.py | 22 ++++ Python/2017/18.ipynb | 210 ------------------------------- Python/2017/18.py | 78 ++++++++++++ Python/2017/19.ipynb | 194 ----------------------------- Python/2017/19.py | 53 ++++++++ Python/2017/20.ipynb | 187 ---------------------------- Python/2017/20.py | 46 +++++++ Python/2017/21.ipynb | 209 ------------------------------- Python/2017/21.py | 86 +++++++++++++ Python/2017/22.ipynb | 194 ----------------------------- Python/2017/22.py | 58 +++++++++ Python/2017/23.ipynb | 289 ------------------------------------------- Python/2017/23.py | 42 +++++++ Python/2017/24.ipynb | 179 --------------------------- Python/2017/24.py | 50 ++++++++ Python/2017/25.ipynb | 124 ------------------- Python/2017/25.py | 32 +++++ README.md | 16 +-- 51 files changed, 1100 insertions(+), 4546 deletions(-) delete mode 100644 Python/2017/01.ipynb create mode 100644 Python/2017/01.py delete mode 100644 Python/2017/02.ipynb create mode 100644 Python/2017/02.py delete mode 100644 Python/2017/03.ipynb create mode 100644 Python/2017/03.py delete mode 100644 Python/2017/04.ipynb create mode 100644 Python/2017/04.py delete mode 100644 Python/2017/05.ipynb create mode 100644 Python/2017/05.py delete mode 100644 Python/2017/06.ipynb create mode 100644 Python/2017/06.py delete mode 100644 Python/2017/07.ipynb create mode 100644 Python/2017/07.py delete mode 100644 Python/2017/08.ipynb create mode 100644 Python/2017/08.py delete mode 100644 Python/2017/09.ipynb create mode 100644 Python/2017/09.py delete mode 100644 Python/2017/10.ipynb create mode 100644 Python/2017/10.py delete mode 100644 Python/2017/11.ipynb create mode 100644 Python/2017/11.py delete mode 100644 Python/2017/12.ipynb create mode 100644 Python/2017/12.py delete mode 100644 Python/2017/13.ipynb create mode 100644 Python/2017/13.py delete mode 100644 Python/2017/14.ipynb create mode 100644 Python/2017/14.py delete mode 100644 Python/2017/15.ipynb create mode 100644 Python/2017/15.py delete mode 100644 Python/2017/16.ipynb create mode 100644 Python/2017/16.py delete mode 100644 Python/2017/17.ipynb create mode 100644 Python/2017/17.py delete mode 100644 Python/2017/18.ipynb create mode 100644 Python/2017/18.py delete mode 100644 Python/2017/19.ipynb create mode 100644 Python/2017/19.py delete mode 100644 Python/2017/20.ipynb create mode 100644 Python/2017/20.py delete mode 100644 Python/2017/21.ipynb create mode 100644 Python/2017/21.py delete mode 100644 Python/2017/22.ipynb create mode 100644 Python/2017/22.py delete mode 100644 Python/2017/23.ipynb create mode 100644 Python/2017/23.py delete mode 100644 Python/2017/24.ipynb create mode 100644 Python/2017/24.py delete mode 100644 Python/2017/25.ipynb create mode 100644 Python/2017/25.py diff --git a/Python/2017/01.ipynb b/Python/2017/01.ipynb deleted file mode 100644 index fd125dd..0000000 --- a/Python/2017/01.ipynb +++ /dev/null @@ -1,149 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/markdown": [ - "# Day 01" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "import sys; sys.path.insert(0, \"..\")\n", - "\n", - "import aoc\n", - "\n", - "year, day = 2017, 1\n", - "\n", - "puzzle = aoc.setup(year, day)\n", - "plines = puzzle.splitlines()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 1" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "995" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve1():\n", - " return sum(int(a) for a, b in zip(puzzle, puzzle[1:] + puzzle[0]) if a == b)\n", - "\n", - "solve1()" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "229 µs ± 3.49 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n" - ] - } - ], - "source": [ - "%timeit solve1()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 2" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "1130" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve2():\n", - " n = len(puzzle) // 2\n", - " return sum(int(a) for a, b in zip(puzzle, puzzle[n:] + puzzle[:n]) if a == b)\n", - "\n", - "solve2()" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "238 µs ± 5.77 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n" - ] - } - ], - "source": [ - "%timeit solve2()" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "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.8.6" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/Python/2017/01.py b/Python/2017/01.py new file mode 100644 index 0000000..88ae8f6 --- /dev/null +++ b/Python/2017/01.py @@ -0,0 +1,10 @@ +from lib import * + +input = read_input(2017, 1).strip() + + +print(sum(int(a) for a, b in zip(input, input[1:] + input[0]) if a == b)) + + +n = len(input) // 2 +print(sum(int(a) for a, b in zip(input, input[n:] + input[:n]) if a == b)) diff --git a/Python/2017/02.ipynb b/Python/2017/02.ipynb deleted file mode 100644 index d82da76..0000000 --- a/Python/2017/02.ipynb +++ /dev/null @@ -1,162 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/markdown": [ - "# Day 02" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "import sys; sys.path.insert(0, \"..\")\n", - "\n", - "import aoc\n", - "\n", - "year, day = 2017, 2\n", - "\n", - "puzzle = aoc.setup(year, day)\n", - "plines = puzzle.splitlines()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 1" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "36174" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve1():\n", - " s = 0\n", - " for line in plines:\n", - " nums = [*map(int, line.split())]\n", - " s += max(nums) - min(nums)\n", - " return s\n", - "\n", - "solve1()" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "68.7 µs ± 6.06 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n" - ] - } - ], - "source": [ - "%timeit solve1()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 2" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "244" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve2():\n", - " s = 0\n", - " for line in plines:\n", - " nums = [*map(int, line.split())]\n", - " for a in nums:\n", - " for b in nums:\n", - " if a % b == 0 and a != b:\n", - " s += a // b\n", - " break\n", - " else: continue\n", - " break\n", - " return s\n", - "\n", - "solve2()" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "291 µs ± 89.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n" - ] - } - ], - "source": [ - "%timeit solve2()" - ] - } - ], - "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.9.6" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/Python/2017/02.py b/Python/2017/02.py new file mode 100644 index 0000000..e9b00a0 --- /dev/null +++ b/Python/2017/02.py @@ -0,0 +1,27 @@ +from lib import * + +input = read_input(2017, 2) + +lines = input.splitlines() + + +s = 0 +for line in lines: + nums = [*map(int, line.split())] + s += max(nums) - min(nums) +print(s) + + +s = 0 +for line in lines: + nums = [*map(int, line.split())] + for a in nums: + for b in nums: + if a % b == 0 and a != b: + s += a // b + break + else: + continue + break + +print(s) diff --git a/Python/2017/03.ipynb b/Python/2017/03.ipynb deleted file mode 100644 index 38032fe..0000000 --- a/Python/2017/03.ipynb +++ /dev/null @@ -1,177 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/markdown": [ - "# Day 03" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "import sys; sys.path.insert(0, \"..\")\n", - "\n", - "import aoc\n", - "\n", - "year, day = 2017, 3\n", - "\n", - "puzzle = aoc.setup(year, day)\n", - "plines = puzzle.splitlines()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 1" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "430" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve1():\n", - " num = int(puzzle)\n", - " i = (int((num-1) ** .5)+1) // 2\n", - " a, b = (i*2-1)**2, (i*2+1)**2\n", - " out = 1e1337\n", - " for j in range(4):\n", - " t = j / 4 + .125\n", - " out = min(out, abs(num-int((1-t)*a+t*b)))\n", - " return out + i\n", - "\n", - "solve1()" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "4.23 µs ± 1.05 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n" - ] - } - ], - "source": [ - "%timeit solve1()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 2" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "312453" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def iter_steps():\n", - " x, y = 0, 0\n", - " dx, dy = 1, 0\n", - " s = 0\n", - " i = 1\n", - " while True:\n", - " yield x, y\n", - " i += 1\n", - " x += dx\n", - " y += dy\n", - "\n", - " if i >= (s*2+1)**2:\n", - " s += 1\n", - " if abs(x + dx) > s or abs(y + dy) > s:\n", - " dx, dy = dy, -dx\n", - " \n", - "def solve2():\n", - " grid = {}\n", - " k = int(puzzle)\n", - " for x, y in iter_steps():\n", - " s = sum(grid.get((x+i,y+j), 0) for i in [-1,0,1] for j in [-1,0,1] if i or j) or 1\n", - " grid[(x,y)] = s\n", - " if s > k:\n", - " return s\n", - "\n", - "solve2()" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "293 µs ± 57.9 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n" - ] - } - ], - "source": [ - "%timeit solve2()" - ] - } - ], - "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.9.6" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/Python/2017/03.py b/Python/2017/03.py new file mode 100644 index 0000000..f3ed952 --- /dev/null +++ b/Python/2017/03.py @@ -0,0 +1,39 @@ +from lib import * + +input = read_input(2017, 3) + + +num = int(input) +i = (int((num - 1) ** 0.5) + 1) // 2 +a, b = (i * 2 - 1) ** 2, (i * 2 + 1) ** 2 +out = 1e1337 +for j in range(4): + t = j / 4 + 0.125 + out = min(out, abs(num - int((1 - t) * a + t * b))) +print(out + i) + + +def iter_steps(): + x, y = 0, 0 + dx, dy = 1, 0 + s = 0 + i = 1 + while True: + yield x, y + i += 1 + x += dx + y += dy + if i >= (s * 2 + 1) ** 2: + s += 1 + if abs(x + dx) > s or abs(y + dy) > s: + dx, dy = dy, -dx + + +grid = {} +k = int(input) +for x, y in iter_steps(): + s = sum(grid.get((x + i, y + j), 0) for i in [-1, 0, 1] for j in [-1, 0, 1] if i or j) or 1 + grid[(x, y)] = s + if s > k: + print(s) + break diff --git a/Python/2017/04.ipynb b/Python/2017/04.ipynb deleted file mode 100644 index b566c69..0000000 --- a/Python/2017/04.ipynb +++ /dev/null @@ -1,156 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/markdown": [ - "# Day 04" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "import sys; sys.path.insert(0, \"..\")\n", - "\n", - "import aoc\n", - "\n", - "year, day = 2017, 4\n", - "\n", - "puzzle = aoc.setup(year, day)\n", - "plines = puzzle.splitlines()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 1" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "466" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve1():\n", - " out = 0\n", - " for pw in plines:\n", - " x = pw.split()\n", - " out += len(set(x)) == len(x)\n", - " return out\n", - "\n", - "solve1()" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "809 µs ± 113 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n" - ] - } - ], - "source": [ - "%timeit solve1()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 2" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "251" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve2():\n", - " out = 0\n", - " for pw in plines:\n", - " x = [\"\".join(sorted(e)) for e in pw.split()]\n", - " out += len(set(x)) == len(x)\n", - " return out\n", - "\n", - "solve2()" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "4.79 ms ± 2.03 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" - ] - } - ], - "source": [ - "%timeit solve2()" - ] - } - ], - "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.9.6" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/Python/2017/04.py b/Python/2017/04.py new file mode 100644 index 0000000..46f19d7 --- /dev/null +++ b/Python/2017/04.py @@ -0,0 +1,19 @@ +from lib import * + +input = read_input(2017, 4) + +lines = input.splitlines() + + +out = 0 +for pw in lines: + x = pw.split() + out += len(set(x)) == len(x) +print(out) + + +out = 0 +for pw in lines: + x = ["".join(sorted(e)) for e in pw.split()] + out += len(set(x)) == len(x) +print(out) diff --git a/Python/2017/05.ipynb b/Python/2017/05.ipynb deleted file mode 100644 index 4f69cd9..0000000 --- a/Python/2017/05.ipynb +++ /dev/null @@ -1,167 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/markdown": [ - "# Day 05" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "import sys; sys.path.insert(0, \"..\")\n", - "\n", - "import aoc\n", - "\n", - "year, day = 2017, 5\n", - "\n", - "puzzle = aoc.setup(year, day)\n", - "plines = puzzle.splitlines()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 1" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "376976" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve1():\n", - " jumps = [*map(int, plines)]\n", - " pos = 0\n", - " i = 0\n", - " while pos in range(len(jumps)):\n", - " p = pos\n", - " pos += jumps[pos]\n", - " jumps[p] += 1\n", - " i += 1\n", - " return i\n", - "\n", - "solve1()" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "244 ms ± 25.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" - ] - } - ], - "source": [ - "%timeit solve1()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 2" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "29227751" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve2():\n", - " jumps = [*map(int, plines)]\n", - " pos = 0\n", - " i = 0\n", - " while pos in range(len(jumps)):\n", - " p = pos\n", - " pos += jumps[pos]\n", - " if jumps[p] >= 3:\n", - " jumps[p] -= 1\n", - " else:\n", - " jumps[p] += 1\n", - " i += 1\n", - " return i\n", - "\n", - "solve2()" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "21.8 s ± 2.56 s per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" - ] - } - ], - "source": [ - "%timeit solve2()" - ] - } - ], - "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.9.6" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/Python/2017/05.py b/Python/2017/05.py new file mode 100644 index 0000000..c1404ec --- /dev/null +++ b/Python/2017/05.py @@ -0,0 +1,32 @@ +from lib import * + +input = read_input(2017, 5) + +lines = input.splitlines() + + +jumps = [*map(int, lines)] +pos = 0 +i = 0 +while pos in range(len(jumps)): + p = pos + pos += jumps[pos] + jumps[p] += 1 + i += 1 + +print(i) + + +jumps = [*map(int, lines)] +pos = 0 +i = 0 +while pos in range(len(jumps)): + p = pos + pos += jumps[pos] + if jumps[p] >= 3: + jumps[p] -= 1 + else: + jumps[p] += 1 + i += 1 + +print(i) diff --git a/Python/2017/06.ipynb b/Python/2017/06.ipynb deleted file mode 100644 index 21c1e6f..0000000 --- a/Python/2017/06.ipynb +++ /dev/null @@ -1,177 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/markdown": [ - "# Day 06" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "import sys; sys.path.insert(0, \"..\")\n", - "\n", - "import aoc\n", - "\n", - "year, day = 2017, 6\n", - "\n", - "puzzle = aoc.setup(year, day)\n", - "plines = puzzle.splitlines()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 1" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "6681" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve1():\n", - " nums = [*map(int, puzzle.split())]\n", - " seen = set()\n", - " out = 0\n", - " while tuple(nums) not in seen:\n", - " seen.add(tuple(nums))\n", - " idx = nums.index(max(nums))\n", - " n = nums[idx]\n", - " nums[idx] = 0\n", - " i = idx\n", - " for _ in range(n):\n", - " i = (i+1)%len(nums)\n", - " nums[i] += 1\n", - " out += 1 \n", - " \n", - " return out\n", - "\n", - "solve1()" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "40.8 ms ± 6.92 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n" - ] - } - ], - "source": [ - "%timeit solve1()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 2" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "2392" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve2():\n", - " nums = [*map(int, puzzle.split())]\n", - " seen = {}\n", - " out = 0\n", - " while tuple(nums) not in seen:\n", - " seen[tuple(nums)] = out\n", - " idx = nums.index(max(nums))\n", - " n = nums[idx]\n", - " nums[idx] = 0\n", - " i = idx\n", - " for _ in range(n):\n", - " i = (i+1)%len(nums)\n", - " nums[i] += 1\n", - " out += 1\n", - " \n", - " return out - seen[tuple(nums)]\n", - "\n", - "\n", - "solve2()" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "52.8 ms ± 20 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n" - ] - } - ], - "source": [ - "%timeit solve2()" - ] - } - ], - "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.9.6" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/Python/2017/06.py b/Python/2017/06.py new file mode 100644 index 0000000..5a62a32 --- /dev/null +++ b/Python/2017/06.py @@ -0,0 +1,37 @@ +from lib import * + +input = read_input(2017, 6) + + +nums = [*map(int, input.split())] +seen = set() +out = 0 +while tuple(nums) not in seen: + seen.add(tuple(nums)) + idx = nums.index(max(nums)) + n = nums[idx] + nums[idx] = 0 + i = idx + for _ in range(n): + i = (i + 1) % len(nums) + nums[i] += 1 + out += 1 + +print(out) + + +nums = [*map(int, input.split())] +seen = {} +out = 0 +while tuple(nums) not in seen: + seen[tuple(nums)] = out + idx = nums.index(max(nums)) + n = nums[idx] + nums[idx] = 0 + i = idx + for _ in range(n): + i = (i + 1) % len(nums) + nums[i] += 1 + out += 1 + +print(out - seen[tuple(nums)]) diff --git a/Python/2017/07.ipynb b/Python/2017/07.ipynb deleted file mode 100644 index 1b8ccbd..0000000 --- a/Python/2017/07.ipynb +++ /dev/null @@ -1,182 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/markdown": [ - "# Day 07" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "import sys; sys.path.insert(0, \"..\")\n", - "\n", - "import aoc\n", - "\n", - "year, day = 2017, 7\n", - "\n", - "puzzle = aoc.setup(year, day)\n", - "plines = puzzle.splitlines()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 1" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'uownj'" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve1():\n", - " names = set()\n", - " subs = set()\n", - " for line in plines:\n", - " name = line.split()[0]\n", - " sub = [] if \"->\" not in line else line.split(\"-> \")[1].split(\", \")\n", - " names.add(name)\n", - " subs.update(sub)\n", - " return next(iter(names - subs))\n", - "\n", - "solve1()" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1.21 ms ± 168 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" - ] - } - ], - "source": [ - "%timeit solve1()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 2" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "(596, None)" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve2():\n", - " progs = {}\n", - " names = set()\n", - " subs = set()\n", - " for line in plines:\n", - " name = line.split()[0]\n", - " weight = int(line.split(\"(\")[1].split(\")\")[0])\n", - " sub = [] if \"->\" not in line else line.split(\"-> \")[1].split(\", \")\n", - " progs[name] = weight, sub\n", - " names.add(name)\n", - " subs.update(sub)\n", - " root = next(iter(names - subs))\n", - " \n", - " def check_sum(p):\n", - " s = progs[p][0]\n", - " w = {}\n", - " for q in progs[p][1]:\n", - " a, b = check_sum(q)\n", - " if a is not None: return a, None\n", - " s += b\n", - " w[q] = b\n", - " \n", - " x = max(w.values(), key=list(w.values()).count, default=None)\n", - " for k, v in w.items():\n", - " if v != x:\n", - " return x - (v - progs[k][0]), None\n", - " return None, s\n", - " \n", - " return check_sum(root)\n", - "\n", - "solve2()" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "5.44 ms ± 1.85 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" - ] - } - ], - "source": [ - "%timeit solve2()" - ] - } - ], - "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.9.6" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/Python/2017/07.py b/Python/2017/07.py new file mode 100644 index 0000000..c3dd0a1 --- /dev/null +++ b/Python/2017/07.py @@ -0,0 +1,52 @@ +from lib import * + +input = read_input(2017, 7) + +lines = input.splitlines() + + +names = set() +subs = set() +for line in lines: + name = line.split()[0] + sub = [] if "->" not in line else line.split("-> ")[1].split(", ") + names.add(name) + subs.update(sub) + +print(next(iter(names - subs))) + + +progs = {} +names = set() +subs = set() +for line in lines: + name = line.split()[0] + weight = int(line.split("(")[1].split(")")[0]) + sub = [] if "->" not in line else line.split("-> ")[1].split(", ") + progs[name] = weight, sub + names.add(name) + subs.update(sub) + +root = next(iter(names - subs)) + + +def check_sum(p): + s = progs[p][0] + w = {} + for q in progs[p][1]: + a, b = check_sum(q) + if a is not None: + return a, None + s += b + w[q] = b + + x = max(w.values(), key=list(w.values()).count, default=None) + + for k, v in w.items(): + if v != x: + return x - (v - progs[k][0]), None + + return None, s + + +print(check_sum(root)[0]) diff --git a/Python/2017/08.ipynb b/Python/2017/08.ipynb deleted file mode 100644 index a41e2f1..0000000 --- a/Python/2017/08.ipynb +++ /dev/null @@ -1,162 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/markdown": [ - "# Day 08" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "import sys; sys.path.insert(0, \"..\")\n", - "\n", - "import aoc\n", - "\n", - "year, day = 2017, 8\n", - "\n", - "puzzle = aoc.setup(year, day)\n", - "plines = puzzle.splitlines()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 1" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "3880" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve1():\n", - " registers = {}\n", - " for line in plines:\n", - " reg, op, n, _, a, b, c = line.split()\n", - " if eval(str(registers.get(a, 0))+b+c):\n", - " x = registers.get(reg, 0)\n", - " registers[reg] = x + (-1 if op == \"dec\" else 1) * int(n)\n", - " return max(registers.values())\n", - "\n", - "solve1()" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "8.48 ms ± 1.42 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" - ] - } - ], - "source": [ - "%timeit solve1()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 2" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "5035" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve2():\n", - " registers = {}\n", - " out = 0\n", - " for line in plines:\n", - " reg, op, n, _, a, b, c = line.split()\n", - " if eval(str(registers.get(a, 0))+b+c):\n", - " x = registers.get(reg, 0)\n", - " registers[reg] = x + (-1 if op == \"dec\" else 1) * int(n)\n", - " out = max(out, registers[reg])\n", - " return out\n", - "\n", - "solve2()" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "10.2 ms ± 1.81 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" - ] - } - ], - "source": [ - "%timeit solve2()" - ] - } - ], - "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.9.6" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/Python/2017/08.py b/Python/2017/08.py new file mode 100644 index 0000000..c3ad050 --- /dev/null +++ b/Python/2017/08.py @@ -0,0 +1,27 @@ +from lib import * + +input = read_input(2017, 8) + +lines = input.splitlines() + + +registers = {} +for line in lines: + reg, op, n, _, a, b, c = line.split() + if eval(str(registers.get(a, 0)) + b + c): + x = registers.get(reg, 0) + registers[reg] = x + (-1 if op == "dec" else 1) * int(n) + +print(max(registers.values())) + + +registers = {} +out = 0 +for line in lines: + reg, op, n, _, a, b, c = line.split() + if eval(str(registers.get(a, 0)) + b + c): + x = registers.get(reg, 0) + registers[reg] = x + (-1 if op == "dec" else 1) * int(n) + out = max(out, registers[reg]) + +print(out) diff --git a/Python/2017/09.ipynb b/Python/2017/09.ipynb deleted file mode 100644 index b2488d6..0000000 --- a/Python/2017/09.ipynb +++ /dev/null @@ -1,191 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/markdown": [ - "# Day 09" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "import sys; sys.path.insert(0, \"..\")\n", - "\n", - "import aoc\n", - "\n", - "year, day = 2017, 9\n", - "\n", - "puzzle = aoc.setup(year, day)\n", - "plines = puzzle.splitlines()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 1" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "9251" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve1():\n", - " out = 0\n", - " level = 0\n", - " garbage = False\n", - " skip = False\n", - " for c in puzzle:\n", - " if skip:\n", - " skip = False\n", - " continue\n", - " if garbage:\n", - " if c == \"!\":\n", - " skip = True\n", - " elif c == \">\":\n", - " garbage = False\n", - " else:\n", - " if c == \"{\":\n", - " level += 1\n", - " elif c == \"}\":\n", - " out += level\n", - " level -= 1\n", - " elif c == \"<\":\n", - " garbage = True\n", - " return out\n", - "\n", - "solve1()" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1.41 ms ± 102 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n" - ] - } - ], - "source": [ - "%timeit solve1()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 2" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "4322" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve2():\n", - " out = 0\n", - " level = 0\n", - " garbage = False\n", - " skip = False\n", - " for c in puzzle:\n", - " if skip:\n", - " skip = False\n", - " continue\n", - " if garbage:\n", - " if c == \"!\":\n", - " skip = True\n", - " elif c == \">\":\n", - " garbage = False\n", - " else:\n", - " out += 1\n", - " else:\n", - " if c == \"{\":\n", - " level += 1\n", - " elif c == \"}\":\n", - " level -= 1\n", - " elif c == \"<\":\n", - " garbage = True\n", - " return out\n", - "\n", - "solve2()" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1.7 ms ± 178 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n" - ] - } - ], - "source": [ - "%timeit solve2()" - ] - } - ], - "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.9.6" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/Python/2017/09.py b/Python/2017/09.py new file mode 100644 index 0000000..1dcd6f5 --- /dev/null +++ b/Python/2017/09.py @@ -0,0 +1,54 @@ +from lib import * + +input = read_input(2017, 9) + + +out = 0 +level = 0 +garbage = False +skip = False +for c in input: + if skip: + skip = False + continue + if garbage: + if c == "!": + skip = True + elif c == ">": + garbage = False + else: + if c == "{": + level += 1 + elif c == "}": + out += level + level -= 1 + elif c == "<": + garbage = True + +print(out) + + +out = 0 +level = 0 +garbage = False +skip = False +for c in input: + if skip: + skip = False + continue + if garbage: + if c == "!": + skip = True + elif c == ">": + garbage = False + else: + out += 1 + else: + if c == "{": + level += 1 + elif c == "}": + level -= 1 + elif c == "<": + garbage = True + +print(out) diff --git a/Python/2017/10.ipynb b/Python/2017/10.ipynb deleted file mode 100644 index 294aac5..0000000 --- a/Python/2017/10.ipynb +++ /dev/null @@ -1,187 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/markdown": [ - "# Day 10" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "import sys; sys.path.insert(0, \"..\")\n", - "\n", - "import aoc\n", - "\n", - "year, day = 2017, 10\n", - "\n", - "puzzle = aoc.setup(year, day)\n", - "plines = puzzle.splitlines()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 1" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "20056" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve1():\n", - " nums = [*range(256)]\n", - " pos = 0\n", - " skip = 0\n", - " \n", - " def rev(a, b):\n", - " a %= len(nums)\n", - " b = (b - a) % len(nums)\n", - " nums[:] = nums[a:] + nums[:a]\n", - " nums[:b] = nums[:b][::-1]\n", - " nums[:] = nums[-a:] + nums[:-a]\n", - " \n", - " for length in map(int, puzzle.split(\",\")):\n", - " rev(pos, pos + length)\n", - " pos += length + skip\n", - " skip += 1\n", - " return nums[0] * nums[1]\n", - "\n", - "solve1()" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "414 µs ± 123 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n" - ] - } - ], - "source": [ - "%timeit solve1()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 2" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'d9a7de4a809c56bf3a9465cb84392c8e'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve2():\n", - " lengths = [*map(ord, puzzle), 17, 31, 73, 47, 23]\n", - " nums = [*range(256)]\n", - " pos = 0\n", - " skip = 0\n", - " \n", - " def rev(a, b):\n", - " a %= len(nums)\n", - " b = (b - a) % len(nums)\n", - " nums[:] = nums[a:] + nums[:a]\n", - " nums[:b] = nums[:b][::-1]\n", - " nums[:] = nums[-a:] + nums[:-a]\n", - " \n", - " for _ in range(64):\n", - " for length in lengths:\n", - " rev(pos, pos + length)\n", - " pos += length + skip\n", - " skip += 1\n", - " \n", - " dense = []\n", - " for i in range(16):\n", - " x = 0\n", - " for j in range(16): x ^= nums[i*16+j]\n", - " dense.append(x)\n", - " \n", - " return \"\".join(f\"{x:02x}\" for x in dense)\n", - "\n", - "solve2()" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "60.4 ms ± 10.9 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n" - ] - } - ], - "source": [ - "%timeit solve2()" - ] - } - ], - "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.9.6" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/Python/2017/10.py b/Python/2017/10.py new file mode 100644 index 0000000..71ffbef --- /dev/null +++ b/Python/2017/10.py @@ -0,0 +1,55 @@ +from lib import * + +input = read_input(2017, 10).strip() + + +nums = [*range(256)] +pos = 0 +skip = 0 + + +def rev(a, b): + a %= len(nums) + b = (b - a) % len(nums) + nums[:] = nums[a:] + nums[:a] + nums[:b] = nums[:b][::-1] + nums[:] = nums[-a:] + nums[:-a] + + +for length in map(int, input.split(",")): + rev(pos, pos + length) + pos += length + skip + skip += 1 + +print(nums[0] * nums[1]) + + +lengths = [*map(ord, input), 17, 31, 73, 47, 23] +nums = [*range(256)] +pos = 0 +skip = 0 + + +def rev(a, b): + a %= len(nums) + b = (b - a) % len(nums) + nums[:] = nums[a:] + nums[:a] + nums[:b] = nums[:b][::-1] + nums[:] = nums[-a:] + nums[:-a] + + +for _ in range(64): + for length in lengths: + rev(pos, pos + length) + pos += length + skip + skip += 1 + +dense = [] +for i in range(16): + x = 0 + for j in range(16): + x ^= nums[i * 16 + j] + + dense.append(x) + +print("".join(f"{x:02x}" for x in dense)) diff --git a/Python/2017/11.ipynb b/Python/2017/11.ipynb deleted file mode 100644 index 06b7e03..0000000 --- a/Python/2017/11.ipynb +++ /dev/null @@ -1,180 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/markdown": [ - "# Day 11" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "import sys; sys.path.insert(0, \"..\")\n", - "\n", - "import aoc\n", - "\n", - "year, day = 2017, 11\n", - "\n", - "puzzle = aoc.setup(year, day)\n", - "plines = puzzle.splitlines()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 1" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "796" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def step(x, y, d):\n", - " if d == \"n\":\n", - " return x, y-1 # (0, -1)\n", - " if d == \"ne\":\n", - " return x+1, y # (1, 0)\n", - " if d == \"se\":\n", - " return x+1, y+1 # (1, 1)\n", - " if d == \"s\":\n", - " return x, y+1 # (0, 1)\n", - " if d == \"sw\":\n", - " return x-1, y # (-1, 0)\n", - " if d == \"nw\":\n", - " return x-1, y-1 # (-1, -1)\n", - "\n", - "def solve1():\n", - " x, y = 0, 0\n", - " for d in puzzle.split(\",\"):\n", - " x, y = step(x, y, d)\n", - " k = 0\n", - " if x * y > 0:\n", - " k = min(x, y, key=abs)\n", - " x -= k\n", - " y -= k\n", - " return abs(x) + abs(y) + k\n", - "\n", - "solve1()" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "4.47 ms ± 1.33 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" - ] - } - ], - "source": [ - "%timeit solve1()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 2" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "1585" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve2():\n", - " x, y = 0, 0\n", - " out = 0\n", - " for d in puzzle.split(\",\"):\n", - " x, y = step(x, y, d)\n", - " k = 0\n", - " if x * y > 0:\n", - " k = min(x, y, key=abs)\n", - " x -= k\n", - " y -= k\n", - " out = max(out, abs(x) + abs(y) + k)\n", - " return out\n", - "\n", - "solve2()" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "9.17 ms ± 1.72 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" - ] - } - ], - "source": [ - "%timeit solve2()" - ] - } - ], - "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.9.6" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/Python/2017/11.py b/Python/2017/11.py new file mode 100644 index 0000000..f1b89e0 --- /dev/null +++ b/Python/2017/11.py @@ -0,0 +1,42 @@ +from lib import * + +input = read_input(2017, 11).strip() + + +def step(x, y, d): + if d == "n": + return x, y - 1 # (0, -1) + if d == "ne": + return x + 1, y # (1, 0) + if d == "se": + return x + 1, y + 1 # (1, 1) + if d == "s": + return x, y + 1 # (0, 1) + if d == "sw": + return x - 1, y # (-1, 0) + if d == "nw": + return x - 1, y - 1 # (-1, -1) + + +x, y = 0, 0 +for d in input.split(","): + x, y = step(x, y, d) +k = 0 +if x * y > 0: + k = min(x, y, key=abs) + x -= k + y -= k +print(abs(x) + abs(y) + k) + + +x, y = 0, 0 +out = 0 +for d in input.split(","): + x, y = step(x, y, d) + k = 0 + if x * y > 0: + k = min(x, y, key=abs) + x -= k + y -= k + out = max(out, abs(x) + abs(y) + k) +print(out) diff --git a/Python/2017/12.ipynb b/Python/2017/12.ipynb deleted file mode 100644 index d866140..0000000 --- a/Python/2017/12.ipynb +++ /dev/null @@ -1,190 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/markdown": [ - "# Day 12" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "import sys; sys.path.insert(0, \"..\")\n", - "\n", - "import aoc\n", - "\n", - "year, day = 2017, 12\n", - "\n", - "puzzle = aoc.setup(year, day)\n", - "plines = puzzle.splitlines()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 1" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "283" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve1():\n", - " edges = {}\n", - " for line in plines:\n", - " p, Q = line.split(\" <-> \")\n", - " p = int(p)\n", - " for q in map(int, Q.split(\", \")):\n", - " edges.setdefault(p, []).append(q)\n", - "\n", - " queue = [0]\n", - " visited = set()\n", - " while queue:\n", - " p = queue.pop(0)\n", - " \n", - " if p in visited: continue\n", - " visited.add(p)\n", - " \n", - " for q in edges.get(p, []):\n", - " queue.append(q)\n", - " \n", - " return len(visited)\n", - "\n", - "solve1()" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "3.37 ms ± 189 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" - ] - } - ], - "source": [ - "%timeit solve1()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 2" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "195" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "class UnionFind:\n", - " def __init__(self, n):\n", - " self.parents = list(range(n))\n", - " \n", - " def find(self, x):\n", - " if self.parents[x] == x: return x\n", - " out = self.find(self.parents[x])\n", - " self.parents[x] = out\n", - " return out\n", - " \n", - " def merge(self, a, b):\n", - " a = self.find(a)\n", - " b = self.find(b)\n", - " self.parents[a] = b\n", - "\n", - "def solve2():\n", - " uf = UnionFind(len(plines))\n", - " for line in plines:\n", - " p, Q = line.split(\" <-> \")\n", - " p = int(p)\n", - " for q in map(int, Q.split(\", \")):\n", - " uf.merge(p, q)\n", - "\n", - " groups = set()\n", - " for i in range(len(plines)): groups.add(uf.find(i))\n", - " return len(groups)\n", - "\n", - "solve2()" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "9.59 ms ± 787 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" - ] - } - ], - "source": [ - "%timeit solve2()" - ] - } - ], - "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.9.6" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/Python/2017/12.py b/Python/2017/12.py new file mode 100644 index 0000000..809281a --- /dev/null +++ b/Python/2017/12.py @@ -0,0 +1,42 @@ +from lib import * + +input = read_input(2017, 12) + +lines = input.splitlines() + + +edges = {} +for line in lines: + p, Q = line.split(" <-> ") + p = int(p) + for q in map(int, Q.split(", ")): + edges.setdefault(p, []).append(q) + +queue = [0] +visited = set() +while queue: + p = queue.pop(0) + + if p in visited: + continue + + visited.add(p) + + for q in edges.get(p, []): + queue.append(q) + +print(len(visited)) + + +uf = UnionFind(len(lines)) +for line in lines: + p, Q = line.split(" <-> ") + p = int(p) + for q in map(int, Q.split(", ")): + uf.merge(p, q) + +groups = set() +for i in range(len(lines)): + groups.add(uf.find(i)) + +print(len(groups)) diff --git a/Python/2017/13.ipynb b/Python/2017/13.ipynb deleted file mode 100644 index ee24684..0000000 --- a/Python/2017/13.ipynb +++ /dev/null @@ -1,178 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/markdown": [ - "# Day 13" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "import sys; sys.path.insert(0, \"..\")\n", - "\n", - "import aoc\n", - "\n", - "year, day = 2017, 13\n", - "\n", - "puzzle = aoc.setup(year, day)\n", - "plines = puzzle.splitlines()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 1" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "1928" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve1():\n", - " R = {}\n", - " S = {}\n", - " for line in plines:\n", - " a, b = map(int, line.split(\": \"))\n", - " R[a] = b\n", - " S[a] = 0\n", - " \n", - " n = max(S)\n", - " out = 0\n", - " for i in range(n):\n", - " if i in S and S[i] == 0:\n", - " out += i * R[i]\n", - " for k, v in S.items():\n", - " S[k] = (v + 1) % (R[k] * 2 - 2)\n", - " return out\n", - "\n", - "solve1()" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1.15 ms ± 145 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n" - ] - } - ], - "source": [ - "%timeit solve1()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 2" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "3830344" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def step(R, S):\n", - " for k, v in S.items():\n", - " S[k] = (v + 1) % (R[k] * 2 - 2)\n", - "\n", - "def solve2():\n", - " R = {}\n", - " SC = {}\n", - " for line in plines:\n", - " a, b = map(int, line.split(\": \"))\n", - " R[a] = b\n", - " SC[a] = 0\n", - " n = max(SC)\n", - " \n", - " def test():\n", - " S = SC.copy()\n", - " for i in range(n+1):\n", - " if i in S and S[i] == 0:\n", - " return False\n", - " step(R, S)\n", - " return True\n", - " \n", - " k = 0\n", - " while not test():\n", - " k += 1\n", - " step(R, SC)\n", - " return k\n", - "\n", - "solve2()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "%timeit solve2()" - ] - } - ], - "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.9.6" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/Python/2017/13.py b/Python/2017/13.py new file mode 100644 index 0000000..a46afb9 --- /dev/null +++ b/Python/2017/13.py @@ -0,0 +1,56 @@ +from lib import * + +input = read_input(2017, 13) + +lines = input.splitlines() + + +R = {} +S = {} +for line in lines: + a, b = map(int, line.split(": ")) + R[a] = b + S[a] = 0 + +n = max(S) +out = 0 +for i in range(n): + if i in S and S[i] == 0: + out += i * R[i] + + for k, v in S.items(): + S[k] = (v + 1) % (R[k] * 2 - 2) + +print(out) + + +def step(R, S): + for k, v in S.items(): + S[k] = (v + 1) % (R[k] * 2 - 2) + + +R = {} +SC = {} +for line in lines: + a, b = map(int, line.split(": ")) + R[a] = b + SC[a] = 0 + +n = max(SC) + + +def test(): + S = SC.copy() + for i in range(n + 1): + if i in S and S[i] == 0: + return False + step(R, S) + return True + + +k = 0 +while not test(): + k += 1 + step(R, SC) + +print(k) diff --git a/Python/2017/14.ipynb b/Python/2017/14.ipynb deleted file mode 100644 index 9728fdc..0000000 --- a/Python/2017/14.ipynb +++ /dev/null @@ -1,216 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/markdown": [ - "# Day 14" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "import sys; sys.path.insert(0, \"..\")\n", - "\n", - "import aoc\n", - "\n", - "year, day = 2017, 14\n", - "\n", - "puzzle = aoc.setup(year, day)\n", - "plines = puzzle.splitlines()" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "def knot(inp):\n", - " lengths = [*map(ord, inp), 17, 31, 73, 47, 23]\n", - " nums = [*range(256)]\n", - " pos = 0\n", - " skip = 0\n", - " \n", - " def rev(a, b):\n", - " a %= len(nums)\n", - " b = (b - a) % len(nums)\n", - " nums[:] = nums[a:] + nums[:a]\n", - " nums[:b] = nums[:b][::-1]\n", - " nums[:] = nums[-a:] + nums[:-a]\n", - " \n", - " for _ in range(64):\n", - " for length in lengths:\n", - " rev(pos, pos + length)\n", - " pos += length + skip\n", - " skip += 1\n", - " \n", - " dense = []\n", - " for i in range(16):\n", - " x = 0\n", - " for j in range(16): x ^= nums[i*16+j]\n", - " dense.append(x)\n", - " \n", - " return \"\".join(f\"{x:02x}\" for x in dense)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 1" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "8292" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve1():\n", - " out = 0\n", - " for i in range(128):\n", - " k = bin(int(knot(f\"{puzzle}-{i}\"), 16))[2:].zfill(128)\n", - " out += k.count(\"1\")\n", - " return out\n", - "\n", - "solve1()" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2.09 s ± 177 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" - ] - } - ], - "source": [ - "%timeit solve1()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 2" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "1069" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "class UnionFind:\n", - " def __init__(self, n):\n", - " self.parents = list(range(n))\n", - " \n", - " def find(self, x):\n", - " if self.parents[x] == x: return x\n", - " out = self.find(self.parents[x])\n", - " self.parents[x] = out\n", - " return out\n", - " \n", - " def merge(self, a, b):\n", - " a = self.find(a)\n", - " b = self.find(b)\n", - " self.parents[a] = b\n", - "\n", - "def solve2():\n", - " last = None\n", - " uf = UnionFind(128 * 128)\n", - " free = 0\n", - " for i in range(128):\n", - " k = bin(int(knot(f\"{puzzle}-{i}\"), 16))[2:].zfill(128)\n", - " for j in range(128):\n", - " if k[j] != \"1\":\n", - " free += 1\n", - " continue\n", - " if i and last[j] == \"1\": uf.merge((i-1)*128+j, i*128+j)\n", - " if j and k[j-1] == \"1\": uf.merge(i*128+j-1, i*128+j)\n", - " last = k\n", - " \n", - " groups = set()\n", - " for i in range(128*128): groups.add(uf.find(i))\n", - " return len(groups) - free\n", - "\n", - "solve2()" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2.49 s ± 467 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" - ] - } - ], - "source": [ - "%timeit solve2()" - ] - } - ], - "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.9.6" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/Python/2017/14.py b/Python/2017/14.py new file mode 100644 index 0000000..eb927f9 --- /dev/null +++ b/Python/2017/14.py @@ -0,0 +1,60 @@ +from lib import * + +input = read_input(2017, 14).strip() + + +def knot(inp): + lengths = [*map(ord, inp), 17, 31, 73, 47, 23] + nums = [*range(256)] + pos = 0 + skip = 0 + + def rev(a, b): + a %= len(nums) + b = (b - a) % len(nums) + nums[:] = nums[a:] + nums[:a] + nums[:b] = nums[:b][::-1] + nums[:] = nums[-a:] + nums[:-a] + + for _ in range(64): + for length in lengths: + rev(pos, pos + length) + pos += length + skip + skip += 1 + + dense = [] + for i in range(16): + x = 0 + for j in range(16): + x ^= nums[i * 16 + j] + dense.append(x) + return "".join(f"{x:02x}" for x in dense) + + +out = 0 +for i in range(128): + k = bin(int(knot(f"{input}-{i}"), 16))[2:].zfill(128) + out += k.count("1") + +print(out) + + +last = None +uf = UnionFind(128 * 128) +free = 0 +for i in range(128): + k = bin(int(knot(f"{input}-{i}"), 16))[2:].zfill(128) + for j in range(128): + if k[j] != "1": + free += 1 + continue + if i and last[j] == "1": + uf.merge((i - 1) * 128 + j, i * 128 + j) + if j and k[j - 1] == "1": + uf.merge(i * 128 + j - 1, i * 128 + j) + last = k + +groups = set() +for i in range(128 * 128): + groups.add(uf.find(i)) +print(len(groups) - free) diff --git a/Python/2017/15.ipynb b/Python/2017/15.ipynb deleted file mode 100644 index 7d74d33..0000000 --- a/Python/2017/15.ipynb +++ /dev/null @@ -1,141 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/markdown": [ - "# Day 15" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "import sys; sys.path.insert(0, \"..\")\n", - "\n", - "import aoc\n", - "\n", - "year, day = 2017, 15\n", - "\n", - "puzzle = aoc.setup(year, day)\n", - "plines = puzzle.splitlines()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 1" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "650" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def gen(prev, k):\n", - " return (prev * k) % 2147483647\n", - "\n", - "def solve1():\n", - " a, b = [int(line.split()[-1]) for line in plines]\n", - " out = 0\n", - " M = 1 << 16\n", - " for _ in range(40000000):\n", - " a = gen(a, 16807)\n", - " b = gen(b, 48271)\n", - " out += a % M == b % M\n", - " return out\n", - "\n", - "solve1()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "%timeit solve1()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 2" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "def generate(x, k, m):\n", - " while True:\n", - " x = gen(x, k)\n", - " if x % m == 0: yield x\n", - "\n", - "def solve2():\n", - " a, b = [int(line.split()[-1]) for line in plines]\n", - " out = 0\n", - " M = 1 << 16\n", - " for _, a, b in zip(range(5000000), generate(a, 16807, 4), generate(b, 48271, 8)):\n", - " out += a % M == b % M\n", - " return out\n", - "\n", - "solve2()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "%timeit solve2()" - ] - } - ], - "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.9.6" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/Python/2017/15.py b/Python/2017/15.py new file mode 100644 index 0000000..882388c --- /dev/null +++ b/Python/2017/15.py @@ -0,0 +1,35 @@ +from lib import * + +input = read_input(2017, 15) + +lines = input.splitlines() + + +def gen(prev, k): + return (prev * k) % 2147483647 + + +a, b = [int(line.split()[-1]) for line in lines] +out = 0 +M = 1 << 16 +for _ in range(40000000): + a = gen(a, 16807) + b = gen(b, 48271) + out += a % M == b % M + +print(out) + + +def generate(x, k, m): + while True: + x = gen(x, k) + if x % m == 0: + yield x + + +a, b = [int(line.split()[-1]) for line in lines] +out = 0 +M = 1 << 16 +for _, a, b in zip(range(5000000), generate(a, 16807, 4), generate(b, 48271, 8)): + out += a % M == b % M +print(out) diff --git a/Python/2017/16.ipynb b/Python/2017/16.ipynb deleted file mode 100644 index 80e2af5..0000000 --- a/Python/2017/16.ipynb +++ /dev/null @@ -1,177 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/markdown": [ - "# Day 16" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "import sys; sys.path.insert(0, \"..\")\n", - "\n", - "import aoc\n", - "\n", - "year, day = 2017, 16\n", - "\n", - "puzzle = aoc.setup(year, day)\n", - "plines = puzzle.splitlines()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 1" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'kbednhopmfcjilag'" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve1():\n", - " p = [chr(97 + i) for i in range(16)]\n", - " for cmd in puzzle.split(\",\"):\n", - " if cmd[0] == \"s\":\n", - " n = int(cmd[1:])\n", - " p = p[-n:] + p[:-n]\n", - " elif cmd[0] == \"x\":\n", - " a, b = map(int, cmd[1:].split(\"/\"))\n", - " p[a], p[b] = p[b], p[a]\n", - " elif cmd[0] == \"p\":\n", - " a, b = map(p.index, cmd[1:].split(\"/\"))\n", - " p[a], p[b] = p[b], p[a]\n", - " \n", - " return \"\".join(p)\n", - "\n", - "solve1()" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "11.7 ms ± 959 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" - ] - } - ], - "source": [ - "%timeit solve1()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 2" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'fbmcgdnjakpioelh'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve2():\n", - " p = [chr(97 + i) for i in range(16)]\n", - " q = p[:]\n", - " mem = []\n", - " while True:\n", - " mem.append(p[:])\n", - " for cmd in puzzle.split(\",\"):\n", - " if cmd[0] == \"s\":\n", - " n = int(cmd[1:])\n", - " p = p[-n:] + p[:-n]\n", - " elif cmd[0] == \"x\":\n", - " a, b = map(int, cmd[1:].split(\"/\"))\n", - " p[a], p[b] = p[b], p[a]\n", - " elif cmd[0] == \"p\":\n", - " a, b = map(p.index, cmd[1:].split(\"/\"))\n", - " p[a], p[b] = p[b], p[a]\n", - " if p == q: break\n", - " \n", - " return \"\".join(mem[1000000000 % len(mem)])\n", - "\n", - "solve2()" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "687 ms ± 72.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" - ] - } - ], - "source": [ - "%timeit solve2()" - ] - } - ], - "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.9.6" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/Python/2017/16.py b/Python/2017/16.py new file mode 100644 index 0000000..9563e4b --- /dev/null +++ b/Python/2017/16.py @@ -0,0 +1,38 @@ +from lib import * + +input = read_input(2017, 16).strip() + + +p = [chr(97 + i) for i in range(16)] +for cmd in input.split(","): + if cmd[0] == "s": + n = int(cmd[1:]) + p = p[-n:] + p[:-n] + elif cmd[0] == "x": + a, b = map(int, cmd[1:].split("/")) + p[a], p[b] = p[b], p[a] + elif cmd[0] == "p": + a, b = map(p.index, cmd[1:].split("/")) + p[a], p[b] = p[b], p[a] +print("".join(p)) + + +p = [chr(97 + i) for i in range(16)] +q = p[:] +mem = [] +while True: + mem.append(p[:]) + for cmd in input.split(","): + if cmd[0] == "s": + n = int(cmd[1:]) + p = p[-n:] + p[:-n] + elif cmd[0] == "x": + a, b = map(int, cmd[1:].split("/")) + p[a], p[b] = p[b], p[a] + elif cmd[0] == "p": + a, b = map(p.index, cmd[1:].split("/")) + p[a], p[b] = p[b], p[a] + if p == q: + break + +print("".join(mem[1000000000 % len(mem)])) diff --git a/Python/2017/17.ipynb b/Python/2017/17.ipynb deleted file mode 100644 index 231cb5a..0000000 --- a/Python/2017/17.ipynb +++ /dev/null @@ -1,160 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/markdown": [ - "# Day 17" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "import sys; sys.path.insert(0, \"..\")\n", - "\n", - "import aoc\n", - "\n", - "year, day = 2017, 17\n", - "\n", - "puzzle = aoc.setup(year, day)\n", - "plines = puzzle.splitlines()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 1" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "808" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve1():\n", - " n = int(puzzle)\n", - " lst = [0]\n", - " pos = 0\n", - " for i in range(1, 2018):\n", - " pos = (pos + n) % len(lst) + 1\n", - " lst.insert(pos, i)\n", - " return lst[(lst.index(2017)+1)%len(lst)]\n", - "\n", - "solve1()" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1.35 ms ± 379 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n" - ] - } - ], - "source": [ - "%timeit solve1()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 2" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "47465686" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve2():\n", - " k = int(puzzle)\n", - " i = 0\n", - " last = None\n", - " for j in range(1, 50000000+1):\n", - " i = (i + k) % j + 1\n", - " if i == 1: last = j\n", - " return last\n", - "\n", - "solve2()" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "9.62 s ± 1.52 s per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" - ] - } - ], - "source": [ - "%timeit solve2()" - ] - } - ], - "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.9.6" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/Python/2017/17.py b/Python/2017/17.py new file mode 100644 index 0000000..8749e30 --- /dev/null +++ b/Python/2017/17.py @@ -0,0 +1,22 @@ +from lib import * + +input = read_input(2017, 17) + + +n = int(input) +lst = [0] +pos = 0 +for i in range(1, 2018): + pos = (pos + n) % len(lst) + 1 + lst.insert(pos, i) +print(lst[(lst.index(2017) + 1) % len(lst)]) + + +k = int(input) +i = 0 +last = None +for j in range(1, 50000000 + 1): + i = (i + k) % j + 1 + if i == 1: + last = j +print(last) diff --git a/Python/2017/18.ipynb b/Python/2017/18.ipynb deleted file mode 100644 index 2f79f12..0000000 --- a/Python/2017/18.ipynb +++ /dev/null @@ -1,210 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/markdown": [ - "# Day 18" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "import sys; sys.path.insert(0, \"..\")\n", - "\n", - "import aoc\n", - "\n", - "year, day = 2017, 18\n", - "\n", - "puzzle = aoc.setup(year, day)\n", - "plines = puzzle.splitlines()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 1" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "4601" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve1():\n", - " registers = {}\n", - " lp = None\n", - " pc = 0\n", - " while pc in range(len(plines)):\n", - " cmd, *args = plines[pc].split()\n", - " get = lambda a: int(a) if a.removeprefix(\"-\").isnumeric() else registers.setdefault(a, 0)\n", - " if cmd == \"snd\":\n", - " lp = get(args[0])\n", - " elif cmd == \"set\":\n", - " registers[args[0]] = get(args[1])\n", - " elif cmd == \"add\":\n", - " registers[args[0]] = get(args[0]) + get(args[1])\n", - " elif cmd == \"mul\":\n", - " registers[args[0]] = get(args[0]) * get(args[1])\n", - " elif cmd == \"mod\":\n", - " registers[args[0]] = get(args[0]) % get(args[1])\n", - " elif cmd == \"rcv\":\n", - " if get(args[0]):\n", - " return lp\n", - " elif cmd == \"jgz\":\n", - " if get(args[0]) > 0:\n", - " pc += get(args[1])\n", - " continue\n", - " \n", - " pc += 1\n", - "\n", - "solve1()" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2.76 ms ± 304 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" - ] - } - ], - "source": [ - "%timeit solve1()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 2" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "6858" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "class VM:\n", - " def __init__(self, p):\n", - " self.registers = {\"p\": p}\n", - " self.pc = 0\n", - " self.inp = []\n", - " self.msg = None\n", - " self.cnt = 0\n", - " \n", - " def step(self):\n", - " if self.pc not in range(len(plines)): return False\n", - " cmd, *args = plines[self.pc].split()\n", - " get = lambda a: int(a) if a.removeprefix(\"-\").isnumeric() else self.registers.setdefault(a, 0)\n", - " if cmd == \"snd\":\n", - " self.cnt += 1\n", - " self.msg(get(args[0]))\n", - " elif cmd == \"set\":\n", - " self.registers[args[0]] = get(args[1])\n", - " elif cmd == \"add\":\n", - " self.registers[args[0]] = get(args[0]) + get(args[1])\n", - " elif cmd == \"mul\":\n", - " self.registers[args[0]] = get(args[0]) * get(args[1])\n", - " elif cmd == \"mod\":\n", - " self.registers[args[0]] = get(args[0]) % get(args[1])\n", - " elif cmd == \"rcv\":\n", - " if not self.inp: return False\n", - " self.registers[args[0]] = self.inp.pop(0)\n", - " elif cmd == \"jgz\":\n", - " if get(args[0]) > 0:\n", - " self.pc += get(args[1])\n", - " return True\n", - " self.pc += 1\n", - " return True\n", - "\n", - "def solve2():\n", - " p0 = VM(0)\n", - " p1 = VM(1)\n", - " p0.msg = p1.inp.append\n", - " p1.msg = p0.inp.append\n", - " while p0.step() + p1.step(): pass\n", - " return p1.cnt\n", - "\n", - "solve2()" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "373 ms ± 180 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" - ] - } - ], - "source": [ - "%timeit solve2()" - ] - } - ], - "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.9.6" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/Python/2017/18.py b/Python/2017/18.py new file mode 100644 index 0000000..b5236e2 --- /dev/null +++ b/Python/2017/18.py @@ -0,0 +1,78 @@ +from lib import * + +input = read_input(2017, 18) + +lines = input.splitlines() + + +registers = {} +lp = None +pc = 0 +while pc in range(len(lines)): + cmd, *args = lines[pc].split() + get = lambda a: int(a) if a.removeprefix("-").isnumeric() else registers.setdefault(a, 0) + if cmd == "snd": + lp = get(args[0]) + elif cmd == "set": + registers[args[0]] = get(args[1]) + elif cmd == "add": + registers[args[0]] = get(args[0]) + get(args[1]) + elif cmd == "mul": + registers[args[0]] = get(args[0]) * get(args[1]) + elif cmd == "mod": + registers[args[0]] = get(args[0]) % get(args[1]) + elif cmd == "rcv": + if get(args[0]): + print(lp) + break + elif cmd == "jgz": + if get(args[0]) > 0: + pc += get(args[1]) + continue + pc += 1 + + +class VM: + def __init__(self, p): + self.registers = {"p": p} + self.pc = 0 + self.inp = [] + self.msg = None + self.cnt = 0 + + def step(self): + if self.pc not in range(len(lines)): + return False + cmd, *args = lines[self.pc].split() + get = lambda a: int(a) if a.removeprefix("-").isnumeric() else self.registers.setdefault(a, 0) + if cmd == "snd": + self.cnt += 1 + self.msg(get(args[0])) + elif cmd == "set": + self.registers[args[0]] = get(args[1]) + elif cmd == "add": + self.registers[args[0]] = get(args[0]) + get(args[1]) + elif cmd == "mul": + self.registers[args[0]] = get(args[0]) * get(args[1]) + elif cmd == "mod": + self.registers[args[0]] = get(args[0]) % get(args[1]) + elif cmd == "rcv": + if not self.inp: + return False + self.registers[args[0]] = self.inp.pop(0) + elif cmd == "jgz": + if get(args[0]) > 0: + self.pc += get(args[1]) + return True + self.pc += 1 + return True + + +p0 = VM(0) +p1 = VM(1) +p0.msg = p1.inp.append +p1.msg = p0.inp.append +while p0.step() + p1.step(): + pass + +print(p1.cnt) diff --git a/Python/2017/19.ipynb b/Python/2017/19.ipynb deleted file mode 100644 index d7df9c6..0000000 --- a/Python/2017/19.ipynb +++ /dev/null @@ -1,194 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/markdown": [ - "# Day 19" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "import sys; sys.path.insert(0, \"..\")\n", - "\n", - "import aoc\n", - "\n", - "year, day = 2017, 19\n", - "\n", - "puzzle = aoc.setup(year, day, strip=False)\n", - "plines = puzzle.splitlines()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 1" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'MOABEUCWQS'" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve1():\n", - " w, h = len(plines[0]), len(plines)\n", - " x, y = plines[0].index(\"|\"), 0\n", - " dx, dy = 0, 1\n", - " out = \"\"\n", - " get = lambda x, y: plines[y][x] if y in range(h) and x in range(w) else \" \"\n", - " while True:\n", - " c = get(x, y)\n", - " f = get(x+dx, y+dy)\n", - " l = get(x+dy, y-dx)\n", - " r = get(x-dy, y+dx)\n", - " if c.isalpha():\n", - " out += c\n", - " \n", - " if f == \" \":\n", - " if l != \" \":\n", - " dx, dy = dy, -dx\n", - " elif r != \" \":\n", - " dx, dy = -dy, dx\n", - " else:\n", - " break\n", - "\n", - " x += dx\n", - " y += dy\n", - " \n", - " return out\n", - "\n", - "solve1()" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "66.6 ms ± 14.3 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n" - ] - } - ], - "source": [ - "%timeit solve1()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 2" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "18058" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve2():\n", - " w, h = len(plines[0]), len(plines)\n", - " x, y = plines[0].index(\"|\"), 0\n", - " dx, dy = 0, 1\n", - " out = 0\n", - " get = lambda x, y: plines[y][x] if y in range(h) and x in range(w) else \" \"\n", - " while True:\n", - " out += 1\n", - " f = get(x+dx, y+dy)\n", - " l = get(x+dy, y-dx)\n", - " r = get(x-dy, y+dx)\n", - " \n", - " if f == \" \":\n", - " if l != \" \":\n", - " dx, dy = dy, -dx\n", - " elif r != \" \":\n", - " dx, dy = -dy, dx\n", - " else:\n", - " break\n", - "\n", - " x += dx\n", - " y += dy\n", - " \n", - " return out\n", - "\n", - "solve2()" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "41.2 ms ± 1.32 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n" - ] - } - ], - "source": [ - "%timeit solve2()" - ] - } - ], - "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.9.6" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/Python/2017/19.py b/Python/2017/19.py new file mode 100644 index 0000000..ce38585 --- /dev/null +++ b/Python/2017/19.py @@ -0,0 +1,53 @@ +from lib import * + +input = read_input(2017, 19) + +lines = input.splitlines() + + +w, h = len(lines[0]), len(lines) +x, y = lines[0].index("|"), 0 +dx, dy = 0, 1 +out = "" +get = lambda x, y: lines[y][x] if y in range(h) and x in range(w) else " " +while True: + c = get(x, y) + f = get(x + dx, y + dy) + l = get(x + dy, y - dx) + r = get(x - dy, y + dx) + if c.isalpha(): + out += c + if f == " ": + if l != " ": + dx, dy = dy, -dx + elif r != " ": + dx, dy = -dy, dx + else: + break + x += dx + y += dy + +print(out) + + +w, h = len(lines[0]), len(lines) +x, y = lines[0].index("|"), 0 +dx, dy = 0, 1 +out = 0 +get = lambda x, y: lines[y][x] if y in range(h) and x in range(w) else " " +while True: + out += 1 + f = get(x + dx, y + dy) + l = get(x + dy, y - dx) + r = get(x - dy, y + dx) + if f == " ": + if l != " ": + dx, dy = dy, -dx + elif r != " ": + dx, dy = -dy, dx + else: + break + x += dx + y += dy + +print(out) diff --git a/Python/2017/20.ipynb b/Python/2017/20.ipynb deleted file mode 100644 index 4ec5dbf..0000000 --- a/Python/2017/20.ipynb +++ /dev/null @@ -1,187 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/markdown": [ - "# Day 20" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "import sys; sys.path.insert(0, \"..\")\n", - "\n", - "import aoc\n", - "\n", - "year, day = 2017, 20\n", - "\n", - "puzzle = aoc.setup(year, day)\n", - "plines = puzzle.splitlines()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 1" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "258" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve1():\n", - " n = 0\n", - " p = []\n", - " v = []\n", - " a = []\n", - " for line in plines:\n", - " x, y, z = [tuple(map(int, k.split(\"<\")[1].strip(\">\").split(\",\"))) for k in line.split(\", \")]\n", - " p.append(x)\n", - " v.append(y)\n", - " a.append(z)\n", - " n += 1\n", - " \n", - " for _ in range(2000):\n", - " for i in range(n):\n", - " v[i] = v[i][0]+a[i][0], v[i][1]+a[i][1], v[i][2]+a[i][2]\n", - " p[i] = v[i][0]+p[i][0], v[i][1]+p[i][1], v[i][2]+p[i][2]\n", - " \n", - " return min(range(n), key=lambda i: sum(map(abs, p[i])))\n", - "\n", - "solve1()" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2.57 s ± 151 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" - ] - } - ], - "source": [ - "%timeit solve1()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 2" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "707" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve2():\n", - " p = []\n", - " v = []\n", - " a = []\n", - " for line in plines:\n", - " x, y, z = [tuple(map(int, k.split(\"<\")[1].strip(\">\").split(\",\"))) for k in line.split(\", \")]\n", - " p.append(x)\n", - " v.append(y)\n", - " a.append(z)\n", - " \n", - " pos = lambda i, t: tuple(\n", - " p[i][k] + t*v[i][k] + t*(t+1)//2*a[i][k]\n", - " for k in range(3)\n", - " )\n", - " \n", - " alive = set(range(len(p)))\n", - " for t in range(1000):\n", - " s = {}\n", - " for i in alive:\n", - " s.setdefault(pos(i, t), []).append(i)\n", - " for y, x in s.items():\n", - " if len(x) > 1:\n", - " alive.difference_update(x)\n", - " \n", - " return len(alive)\n", - "\n", - "solve2()" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2.27 s ± 219 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" - ] - } - ], - "source": [ - "%timeit solve2()" - ] - } - ], - "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.9.6" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/Python/2017/20.py b/Python/2017/20.py new file mode 100644 index 0000000..96f1f61 --- /dev/null +++ b/Python/2017/20.py @@ -0,0 +1,46 @@ +from lib import * + +input = read_input(2017, 20) + +lines = input.splitlines() + + +n = 0 +p = [] +v = [] +a = [] +for line in lines: + x, y, z = [tuple(map(int, k.split("<")[1].strip(">").split(","))) for k in line.split(", ")] + p.append(x) + v.append(y) + a.append(z) + n += 1 + +for _ in range(2000): + for i in range(n): + v[i] = v[i][0] + a[i][0], v[i][1] + a[i][1], v[i][2] + a[i][2] + p[i] = v[i][0] + p[i][0], v[i][1] + p[i][1], v[i][2] + p[i][2] + +print(min(range(n), key=lambda i: sum(map(abs, p[i])))) + + +p = [] +v = [] +a = [] +for line in lines: + x, y, z = [tuple(map(int, k.split("<")[1].strip(">").split(","))) for k in line.split(", ")] + p.append(x) + v.append(y) + a.append(z) + +pos = lambda i, t: tuple(p[i][k] + t * v[i][k] + t * (t + 1) // 2 * a[i][k] for k in range(3)) +alive = set(range(len(p))) +for t in range(1000): + s = {} + for i in alive: + s.setdefault(pos(i, t), []).append(i) + for y, x in s.items(): + if len(x) > 1: + alive.difference_update(x) + +print(len(alive)) diff --git a/Python/2017/21.ipynb b/Python/2017/21.ipynb deleted file mode 100644 index 5cbd161..0000000 --- a/Python/2017/21.ipynb +++ /dev/null @@ -1,209 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/markdown": [ - "# Day 21" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "import sys; sys.path.insert(0, \"..\")\n", - "\n", - "import aoc\n", - "\n", - "year, day = 2017, 21\n", - "\n", - "puzzle = aoc.setup(year, day)\n", - "plines = puzzle.splitlines()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 1" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "197" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def rotate(p):\n", - " return tuple(\"\".join(p[j][i] for j in range(len(p))) for i in reversed(range(len(p[0]))))\n", - "\n", - "def solve1():\n", - " rules = {}\n", - " pattern = \".#.\", \"..#\", \"###\"\n", - " for line in plines:\n", - " a, b = [tuple(k.split(\"/\")) for k in line.split(\" => \")]\n", - " rules[a] = b\n", - " \n", - " def find_rule(p):\n", - " for _ in range(4):\n", - " if p in rules: return rules[p]\n", - " p = rotate(p)\n", - " p = tuple(reversed(p))\n", - " for _ in range(4):\n", - " if p in rules: return rules[p]\n", - " p = rotate(p)\n", - " \n", - " def apply():\n", - " k = 3 if len(pattern) % 2 else 2\n", - " out = []\n", - " for i in range(len(pattern)//k):\n", - " b = []\n", - " for j in range(len(pattern)//k):\n", - " new = find_rule(tuple(\"\".join(pattern[i*k+y][j*k+x] for x in range(k)) for y in range(k)))\n", - " b.append(new)\n", - " for line in zip(*b):\n", - " out.append(\"\".join(line))\n", - " return tuple(out)\n", - " \n", - " for _ in range(5):\n", - " pattern = apply()\n", - " return sum(line.count(\"#\") for line in pattern)\n", - "\n", - "solve1()" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "804 µs ± 63.1 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" - ] - } - ], - "source": [ - "%timeit solve1()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 2" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "3081737" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve2():\n", - " rules = {}\n", - " pattern = \".#.\", \"..#\", \"###\"\n", - " for line in plines:\n", - " a, b = [tuple(k.split(\"/\")) for k in line.split(\" => \")]\n", - " rules[a] = b\n", - " \n", - " def find_rule(p):\n", - " for _ in range(4):\n", - " if p in rules: return rules[p]\n", - " p = rotate(p)\n", - " p = tuple(reversed(p))\n", - " for _ in range(4):\n", - " if p in rules: return rules[p]\n", - " p = rotate(p)\n", - " \n", - " def apply():\n", - " k = 3 if len(pattern) % 2 else 2\n", - " out = []\n", - " for i in range(len(pattern)//k):\n", - " b = []\n", - " for j in range(len(pattern)//k):\n", - " new = find_rule(tuple(\"\".join(pattern[i*k+y][j*k+x] for x in range(k)) for y in range(k)))\n", - " b.append(new)\n", - " for line in zip(*b):\n", - " out.append(\"\".join(line))\n", - " return tuple(out)\n", - " \n", - " for _ in range(18):\n", - " pattern = apply()\n", - " return sum(line.count(\"#\") for line in pattern)\n", - "\n", - "solve2()" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "9.14 s ± 331 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" - ] - } - ], - "source": [ - "%timeit solve2()" - ] - } - ], - "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.9.6" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/Python/2017/21.py b/Python/2017/21.py new file mode 100644 index 0000000..f19206c --- /dev/null +++ b/Python/2017/21.py @@ -0,0 +1,86 @@ +from lib import * + +input = read_input(2017, 21) + +lines = input.splitlines() + + +def rotate(p): + return tuple("".join(p[j][i] for j in range(len(p))) for i in reversed(range(len(p[0])))) + + +rules = {} +pattern = ".#.", "..#", "###" +for line in lines: + a, b = [tuple(k.split("/")) for k in line.split(" => ")] + rules[a] = b + + +def find_rule(p): + for _ in range(4): + if p in rules: + return rules[p] + p = rotate(p) + + p = tuple(reversed(p)) + for _ in range(4): + if p in rules: + return rules[p] + p = rotate(p) + + +def apply(): + k = 3 if len(pattern) % 2 else 2 + out = [] + for i in range(len(pattern) // k): + b = [] + for j in range(len(pattern) // k): + new = find_rule(tuple("".join(pattern[i * k + y][j * k + x] for x in range(k)) for y in range(k))) + b.append(new) + for line in zip(*b): + out.append("".join(line)) + return tuple(out) + + +for _ in range(5): + pattern = apply() + +print(sum(line.count("#") for line in pattern)) + + +rules = {} +pattern = ".#.", "..#", "###" +for line in lines: + a, b = [tuple(k.split("/")) for k in line.split(" => ")] + rules[a] = b + + +def find_rule(p): + for _ in range(4): + if p in rules: + return rules[p] + p = rotate(p) + p = tuple(reversed(p)) + for _ in range(4): + if p in rules: + return rules[p] + p = rotate(p) + + +def apply(): + k = 3 if len(pattern) % 2 else 2 + out = [] + for i in range(len(pattern) // k): + b = [] + for j in range(len(pattern) // k): + new = find_rule(tuple("".join(pattern[i * k + y][j * k + x] for x in range(k)) for y in range(k))) + b.append(new) + for line in zip(*b): + out.append("".join(line)) + return tuple(out) + + +for _ in range(18): + pattern = apply() + +print(sum(line.count("#") for line in pattern)) diff --git a/Python/2017/22.ipynb b/Python/2017/22.ipynb deleted file mode 100644 index 1df9dae..0000000 --- a/Python/2017/22.ipynb +++ /dev/null @@ -1,194 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/markdown": [ - "# Day 22" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "import sys; sys.path.insert(0, \"..\")\n", - "\n", - "import aoc\n", - "\n", - "year, day = 2017, 22\n", - "\n", - "puzzle = aoc.setup(year, day)\n", - "plines = puzzle.splitlines()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 1" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "5256" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve1():\n", - " infected = set()\n", - " x, y = len(plines[0])//2, len(plines)//2\n", - " dx, dy = 0, -1\n", - " out = 0\n", - " for i, line in enumerate(plines):\n", - " for j, c in enumerate(line):\n", - " if c == \"#\":\n", - " infected.add((j, i))\n", - " for _ in range(10000):\n", - " inf = (x, y) in infected\n", - " if inf:\n", - " dx, dy = -dy, dx\n", - " infected.remove((x, y))\n", - " else:\n", - " dx, dy = dy, -dx\n", - " infected.add((x, y))\n", - " out += 1\n", - " \n", - " x += dx\n", - " y += dy\n", - " \n", - " return out\n", - " \n", - "solve1()" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "5.3 ms ± 448 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" - ] - } - ], - "source": [ - "%timeit solve1()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 2" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "2511345" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve2():\n", - " state = {}\n", - " x, y = len(plines[0])//2, len(plines)//2\n", - " dx, dy = 0, -1\n", - " out = 0\n", - " for i, line in enumerate(plines):\n", - " for j, c in enumerate(line):\n", - " if c == \"#\":\n", - " state[(j, i)] = 2\n", - " for _ in range(10000000):\n", - " s = state.get((x, y), 0)\n", - " \n", - " if s == 0: dx, dy = dy, -dx\n", - " elif s == 2: dx, dy = -dy, dx\n", - " elif s == 3: dx, dy = -dx, -dy\n", - " \n", - " if s == 1: out += 1\n", - "\n", - " if s == 3:\n", - " state.pop((x, y))\n", - " else:\n", - " state[(x, y)] = s + 1\n", - " \n", - " x += dx\n", - " y += dy\n", - " \n", - " return out\n", - "\n", - "solve2()" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "8.54 s ± 433 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" - ] - } - ], - "source": [ - "%timeit solve2()" - ] - } - ], - "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.9.6" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/Python/2017/22.py b/Python/2017/22.py new file mode 100644 index 0000000..2d633a6 --- /dev/null +++ b/Python/2017/22.py @@ -0,0 +1,58 @@ +from lib import * + +input = read_input(2017, 22) + +lines = input.splitlines() + + +infected = set() +x, y = len(lines[0]) // 2, len(lines) // 2 +dx, dy = 0, -1 +out = 0 +for i, line in enumerate(lines): + for j, c in enumerate(line): + if c == "#": + infected.add((j, i)) + +for _ in range(10000): + inf = (x, y) in infected + if inf: + dx, dy = -dy, dx + infected.remove((x, y)) + else: + dx, dy = dy, -dx + infected.add((x, y)) + out += 1 + x += dx + y += dy + +print(out) + + +state = {} +x, y = len(lines[0]) // 2, len(lines) // 2 +dx, dy = 0, -1 +out = 0 +for i, line in enumerate(lines): + for j, c in enumerate(line): + if c == "#": + state[(j, i)] = 2 + +for _ in range(10000000): + s = state.get((x, y), 0) + if s == 0: + dx, dy = dy, -dx + elif s == 2: + dx, dy = -dy, dx + elif s == 3: + dx, dy = -dx, -dy + if s == 1: + out += 1 + if s == 3: + state.pop((x, y)) + else: + state[(x, y)] = s + 1 + x += dx + y += dy + +print(out) diff --git a/Python/2017/23.ipynb b/Python/2017/23.ipynb deleted file mode 100644 index e9c45e2..0000000 --- a/Python/2017/23.ipynb +++ /dev/null @@ -1,289 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/markdown": [ - "# Day 23" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "import sys; sys.path.insert(0, \"..\")\n", - "\n", - "import aoc\n", - "\n", - "year, day = 2017, 23\n", - "\n", - "puzzle = aoc.setup(year, day)\n", - "plines = puzzle.splitlines()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 1" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "9409" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve1():\n", - " registers = {chr(i): 0 for i in range(97, 105)}\n", - " pc = 0\n", - " out = 0\n", - " while pc in range(len(plines)):\n", - " cmd, *args = plines[pc].split()\n", - " get = lambda a: registers[a] if a in registers else int(a)\n", - " if cmd == \"set\":\n", - " registers[args[0]] = get(args[1])\n", - " elif cmd == \"sub\":\n", - " registers[args[0]] -= get(args[1])\n", - " elif cmd == \"mul\":\n", - " registers[args[0]] *= get(args[1])\n", - " out += 1\n", - " elif cmd == \"jnz\":\n", - " if get(args[0]):\n", - " pc += get(args[1])\n", - " continue\n", - " pc += 1\n", - " \n", - " return out\n", - "\n", - "solve1()" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "134 ms ± 19 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n" - ] - } - ], - "source": [ - "%timeit solve1()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 2" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "001| b = 99\n", - "002| c = b\n", - "003| if a: goto 005\n", - "004| goto 009\n", - "\n", - "# jumps: 003\n", - "005| b *= 100\n", - "006| b += 100000\n", - "007| c = b\n", - "008| c += 17000\n", - "\n", - "# jumps: 004, 032\n", - "009| f = 1\n", - "010| d = 2\n", - "\n", - "# jumps: 024\n", - "011| e = 2\n", - "\n", - "# jumps: 020\n", - "012| g = d\n", - "013| g *= e\n", - "014| g -= b\n", - "015| if g: goto 017\n", - "016| f = 0\n", - "\n", - "# jumps: 015\n", - "017| e += 1\n", - "018| g = e\n", - "019| g -= b\n", - "020| if g: goto 012\n", - "021| d += 1\n", - "022| g = d\n", - "023| g -= b\n", - "024| if g: goto 011\n", - "025| if f: goto 027\n", - "026| h += 1\n", - "\n", - "# jumps: 025\n", - "027| g = b\n", - "028| g -= c\n", - "029| if g: goto 031\n", - "030| goto 033\n", - "\n", - "# jumps: 029\n", - "031| b += 17\n", - "032| goto 009\n" - ] - } - ], - "source": [ - "jumps = {}\n", - "out = []\n", - "for i, line in enumerate(plines):\n", - " cmd, *args = line.split()\n", - " if cmd == \"set\":\n", - " out.append(f\"{args[0]} = {args[1]}\")\n", - " elif cmd == \"sub\":\n", - " if args[1].strip(\"-\").isnumeric() and int(args[1]) < 0:\n", - " out.append(f\"{args[0]} += {-int(args[1])}\")\n", - " else:\n", - " out.append(f\"{args[0]} -= {args[1]}\")\n", - " elif cmd == \"mul\":\n", - " out.append(f\"{args[0]} *= {args[1]}\")\n", - " elif cmd == \"jnz\":\n", - " x, y = args\n", - " y = i + int(y)\n", - " if x == \"0\":\n", - " out.append(\"noop\")\n", - " continue\n", - " elif x.strip(\"-\").isnumeric():\n", - " out.append(f\"goto {y+1:03}\")\n", - " else:\n", - " out.append(f\"if {x}: goto {y+1:03}\")\n", - " jumps.setdefault(y, []).append(f\"{i+1:03}\")\n", - " else:\n", - " out.append(\"noop\")\n", - " \n", - "\n", - "for i, line in enumerate(out):\n", - " if i in jumps: print(f\"\\n# jumps: {', '.join(jumps[i])}\")\n", - " print(f\"{i+1:03}| {line}\")" - ] - }, - { - "cell_type": "raw", - "metadata": {}, - "source": [ - "b = 109900 - 17\n", - "\n", - "while b != 126900 {\n", - " b += 17\n", - " \n", - " f = 1\n", - " for d in range(2, b):\n", - " if b % d == 0:\n", - " f = 0\n", - " \n", - " if f == 0: h += 1\n", - "}" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "913" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve2():\n", - " b = 109900 - 17\n", - " h = 0\n", - " while b != 126900:\n", - " b += 17\n", - "\n", - " f = 1\n", - " for d in range(2, b):\n", - " if b % d == 0:\n", - " f = 0\n", - " break\n", - "\n", - " if f == 0: h += 1\n", - " return h\n", - "\n", - "solve2()" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1.15 s ± 297 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" - ] - } - ], - "source": [ - "%timeit solve2()" - ] - } - ], - "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.9.6" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/Python/2017/23.py b/Python/2017/23.py new file mode 100644 index 0000000..6e5f1ae --- /dev/null +++ b/Python/2017/23.py @@ -0,0 +1,42 @@ +from lib import * + +input = read_input(2017, 23) + +lines = input.splitlines() + + +registers = {chr(i): 0 for i in range(97, 105)} +pc = 0 +out = 0 +while pc in range(len(lines)): + cmd, *args = lines[pc].split() + get = lambda a: registers[a] if a in registers else int(a) + if cmd == "set": + registers[args[0]] = get(args[1]) + elif cmd == "sub": + registers[args[0]] -= get(args[1]) + elif cmd == "mul": + registers[args[0]] *= get(args[1]) + out += 1 + elif cmd == "jnz": + if get(args[0]): + pc += get(args[1]) + continue + pc += 1 + +print(out) + + +b = 109900 - 17 +h = 0 +while b != 126900: + b += 17 + f = 1 + for d in range(2, b): + if b % d == 0: + f = 0 + break + if f == 0: + h += 1 + +print(h) diff --git a/Python/2017/24.ipynb b/Python/2017/24.ipynb deleted file mode 100644 index 276f58d..0000000 --- a/Python/2017/24.ipynb +++ /dev/null @@ -1,179 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/markdown": [ - "# Day 24" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "import sys; sys.path.insert(0, \"..\")\n", - "\n", - "import aoc\n", - "\n", - "year, day = 2017, 24\n", - "\n", - "puzzle = aoc.setup(year, day)\n", - "plines = puzzle.splitlines()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 1" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "1656" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve1():\n", - " comps = sorted([tuple(sorted(map(int, line.split(\"/\")))) for line in plines])\n", - " used = set()\n", - " \n", - " def solve(port):\n", - " best = 0\n", - " for i, (p, q) in enumerate(comps):\n", - " if i in used: continue\n", - " if q == port: p, q = q, p\n", - " if p != port: continue\n", - " used.add(i)\n", - " best = max(best, p+q+solve(q))\n", - " used.remove(i)\n", - " return best\n", - " \n", - " \n", - " return solve(0)\n", - "\n", - "solve1()" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "7.62 s ± 409 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" - ] - } - ], - "source": [ - "%timeit solve1()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 2" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "1642" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve2():\n", - " comps = sorted([tuple(sorted(map(int, line.split(\"/\")))) for line in plines])\n", - " used = set()\n", - " \n", - " def solve(port):\n", - " best = 0, 0\n", - " for i, (p, q) in enumerate(comps):\n", - " if i in used: continue\n", - " if q == port: p, q = q, p\n", - " if p != port: continue\n", - " used.add(i)\n", - " a, b = solve(q)\n", - " best = max(best, (a + 1, p+q+b))\n", - " used.remove(i)\n", - " return best\n", - " \n", - " \n", - " return solve(0)[1]\n", - "\n", - "solve2()" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "7.7 s ± 371 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" - ] - } - ], - "source": [ - "%timeit solve2()" - ] - } - ], - "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.9.6" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/Python/2017/24.py b/Python/2017/24.py new file mode 100644 index 0000000..b4ea9de --- /dev/null +++ b/Python/2017/24.py @@ -0,0 +1,50 @@ +from lib import * + +input = read_input(2017, 24) + +lines = input.splitlines() + + +comps = sorted([tuple(sorted(map(int, line.split("/")))) for line in lines]) +used = set() + + +def solve(port): + best = 0 + for i, (p, q) in enumerate(comps): + if i in used: + continue + if q == port: + p, q = q, p + if p != port: + continue + used.add(i) + best = max(best, p + q + solve(q)) + used.remove(i) + return best + + +print(solve(0)) + + +comps = sorted([tuple(sorted(map(int, line.split("/")))) for line in lines]) +used = set() + + +def solve(port): + best = 0, 0 + for i, (p, q) in enumerate(comps): + if i in used: + continue + if q == port: + p, q = q, p + if p != port: + continue + used.add(i) + a, b = solve(q) + best = max(best, (a + 1, p + q + b)) + used.remove(i) + return best + + +print(solve(0)[1]) diff --git a/Python/2017/25.ipynb b/Python/2017/25.ipynb deleted file mode 100644 index 99aacbb..0000000 --- a/Python/2017/25.ipynb +++ /dev/null @@ -1,124 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/markdown": [ - "# Day 25" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "import sys; sys.path.insert(0, \"..\")\n", - "\n", - "import aoc\n", - "\n", - "year, day = 2017, 25\n", - "\n", - "puzzle = aoc.setup(year, day)\n", - "plines = puzzle.splitlines()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Puzzle 1" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "5593" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def solve1():\n", - " state = plines[0].split()[-1].strip(\".\")\n", - " n = int(plines[1].split()[-2])\n", - " tm = {}\n", - " on = set()\n", - " p = 0\n", - "\n", - " for s in map(str.splitlines, puzzle.split(\"\\n\\n\")[1:]):\n", - " tm[s[0].split()[-1].strip(\":\")] = [\n", - " (\n", - " int(s[i*4+2].split()[-1].strip(\".\")),\n", - " -1 if s[i*4+3].split()[-1] == \"left.\" else 1,\n", - " s[i*4+4].split()[-1].strip(\".\")\n", - " )\n", - " for i in range(2)\n", - " ]\n", - " \n", - " for _ in range(n):\n", - " s = tm[state][p in on]\n", - " if s[0]: on.add(p)\n", - " else: on.discard(p)\n", - " p += s[1]\n", - " state = s[2]\n", - " \n", - " return len(on)\n", - "\n", - "solve1()" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "5.49 s ± 546 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" - ] - } - ], - "source": [ - "%timeit solve1()" - ] - } - ], - "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.9.6" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/Python/2017/25.py b/Python/2017/25.py new file mode 100644 index 0000000..2a1dd6e --- /dev/null +++ b/Python/2017/25.py @@ -0,0 +1,32 @@ +from lib import * + +input = read_input(2017, 25) + +lines = input.splitlines() + + +state = lines[0].split()[-1].strip(".") +n = int(lines[1].split()[-2]) +tm = {} +on = set() +p = 0 +for s in map(str.splitlines, input.split("\n\n")[1:]): + tm[s[0].split()[-1].strip(":")] = [ + ( + int(s[i * 4 + 2].split()[-1].strip(".")), + -1 if s[i * 4 + 3].split()[-1] == "left." else 1, + s[i * 4 + 4].split()[-1].strip("."), + ) + for i in range(2) + ] + +for _ in range(n): + s = tm[state][p in on] + if s[0]: + on.add(p) + else: + on.discard(p) + p += s[1] + state = s[2] + +print(len(on)) diff --git a/README.md b/README.md index e8cb385..9dcdde1 100644 --- a/README.md +++ b/README.md @@ -43,19 +43,19 @@ |-|-|-|-|-|-|-| ||||||[**1**](https://adventofcode.com/2018/day/1) [](Python/2018/01 "Python solution for 2018/01")|[**2**](https://adventofcode.com/2018/day/2) [](Python/2018/02 "Python solution for 2018/02")| |[**3**](https://adventofcode.com/2018/day/3) [](Python/2018/03 "Python solution for 2018/03")|[**4**](https://adventofcode.com/2018/day/4) [](Python/2018/04 "Python solution for 2018/04")|[**5**](https://adventofcode.com/2018/day/5) [](Python/2018/05 "Python solution for 2018/05")|[**6**](https://adventofcode.com/2018/day/6) [](Python/2018/06 "Python solution for 2018/06")|[**7**](https://adventofcode.com/2018/day/7) [](Python/2018/07 "Python solution for 2018/07")|[**8**](https://adventofcode.com/2018/day/8) [](Python/2018/08 "Python solution for 2018/08")|[**9**](https://adventofcode.com/2018/day/9) [](Python/2018/09 "Python solution for 2018/09")| -|[**10**](https://adventofcode.com/2018/day/10) [](Python/2018/10 "Python solution for 2018/10")|[**11**](https://adventofcode.com/2018/day/11) [](Python/2018/11.ipynb "Python solution for 2018/11")|[**12**](https://adventofcode.com/2018/day/12) [](Python/2018/12.ipynb "Python solution for 2018/12")|[**13**](https://adventofcode.com/2018/day/13) [](Python/2018/13.ipynb "Python solution for 2018/13")|[**14**](https://adventofcode.com/2018/day/14) [](Python/2018/14.ipynb "Python solution for 2018/14")|[**15**](https://adventofcode.com/2018/day/15) [](Python/2018/15.ipynb "Python solution for 2018/15")|[**16**](https://adventofcode.com/2018/day/16) [](Python/2018/16.ipynb "Python solution for 2018/16")| -|[**17**](https://adventofcode.com/2018/day/17) [](Python/2018/17.ipynb "Python solution for 2018/17")|[**18**](https://adventofcode.com/2018/day/18) [](Python/2018/18.ipynb "Python solution for 2018/18")|[**19**](https://adventofcode.com/2018/day/19) [](Python/2018/19.ipynb "Python solution for 2018/19")|[**20**](https://adventofcode.com/2018/day/20) [](Python/2018/20.ipynb "Python solution for 2018/20")|[**21**](https://adventofcode.com/2018/day/21) [](Python/2018/21.ipynb "Python solution for 2018/21")|[**22**](https://adventofcode.com/2018/day/22) [](Python/2018/22.ipynb "Python solution for 2018/22")|[**23**](https://adventofcode.com/2018/day/23) [](Python/2018/23.ipynb "Python solution for 2018/23")| -|[**24**](https://adventofcode.com/2018/day/24) [](Python/2018/24.ipynb "Python solution for 2018/24")|[**25**](https://adventofcode.com/2018/day/25) [](Python/2018/25.ipynb "Python solution for 2018/25")|26|27|28|29|30| +|[**10**](https://adventofcode.com/2018/day/10) [](Python/2018/10 "Python solution for 2018/10")|[**11**](https://adventofcode.com/2018/day/11) [](Python/2018/11.py "Python solution for 2018/11")|[**12**](https://adventofcode.com/2018/day/12) [](Python/2018/12.py "Python solution for 2018/12")|[**13**](https://adventofcode.com/2018/day/13) [](Python/2018/13.py "Python solution for 2018/13")|[**14**](https://adventofcode.com/2018/day/14) [](Python/2018/14.py "Python solution for 2018/14")|[**15**](https://adventofcode.com/2018/day/15) [](Python/2018/15.py "Python solution for 2018/15")|[**16**](https://adventofcode.com/2018/day/16) [](Python/2018/16.py "Python solution for 2018/16")| +|[**17**](https://adventofcode.com/2018/day/17) [](Python/2018/17.py "Python solution for 2018/17")|[**18**](https://adventofcode.com/2018/day/18) [](Python/2018/18.py "Python solution for 2018/18")|[**19**](https://adventofcode.com/2018/day/19) [](Python/2018/19.py "Python solution for 2018/19")|[**20**](https://adventofcode.com/2018/day/20) [](Python/2018/20.py "Python solution for 2018/20")|[**21**](https://adventofcode.com/2018/day/21) [](Python/2018/21.py "Python solution for 2018/21")|[**22**](https://adventofcode.com/2018/day/22) [](Python/2018/22.py "Python solution for 2018/22")|[**23**](https://adventofcode.com/2018/day/23) [](Python/2018/23.py "Python solution for 2018/23")| +|[**24**](https://adventofcode.com/2018/day/24) [](Python/2018/24.py "Python solution for 2018/24")|[**25**](https://adventofcode.com/2018/day/25) [](Python/2018/25.py "Python solution for 2018/25")|26|27|28|29|30| |31||||||| ## [2017](https://adventofcode.com/2017) ([ Python](Python/2017): 25/25) |Mo|Tu|We|Th|Fr|Sa|Su| |-|-|-|-|-|-|-| -|||||[**1**](https://adventofcode.com/2017/day/1) [](Python/2017/01.ipynb "Python solution for 2017/01")|[**2**](https://adventofcode.com/2017/day/2) [](Python/2017/02.ipynb "Python solution for 2017/02")|[**3**](https://adventofcode.com/2017/day/3) [](Python/2017/03.ipynb "Python solution for 2017/03")| -|[**4**](https://adventofcode.com/2017/day/4) [](Python/2017/04.ipynb "Python solution for 2017/04")|[**5**](https://adventofcode.com/2017/day/5) [](Python/2017/05.ipynb "Python solution for 2017/05")|[**6**](https://adventofcode.com/2017/day/6) [](Python/2017/06.ipynb "Python solution for 2017/06")|[**7**](https://adventofcode.com/2017/day/7) [](Python/2017/07.ipynb "Python solution for 2017/07")|[**8**](https://adventofcode.com/2017/day/8) [](Python/2017/08.ipynb "Python solution for 2017/08")|[**9**](https://adventofcode.com/2017/day/9) [](Python/2017/09.ipynb "Python solution for 2017/09")|[**10**](https://adventofcode.com/2017/day/10) [](Python/2017/10.ipynb "Python solution for 2017/10")| -|[**11**](https://adventofcode.com/2017/day/11) [](Python/2017/11.ipynb "Python solution for 2017/11")|[**12**](https://adventofcode.com/2017/day/12) [](Python/2017/12.ipynb "Python solution for 2017/12")|[**13**](https://adventofcode.com/2017/day/13) [](Python/2017/13.ipynb "Python solution for 2017/13")|[**14**](https://adventofcode.com/2017/day/14) [](Python/2017/14.ipynb "Python solution for 2017/14")|[**15**](https://adventofcode.com/2017/day/15) [](Python/2017/15.ipynb "Python solution for 2017/15")|[**16**](https://adventofcode.com/2017/day/16) [](Python/2017/16.ipynb "Python solution for 2017/16")|[**17**](https://adventofcode.com/2017/day/17) [](Python/2017/17.ipynb "Python solution for 2017/17")| -|[**18**](https://adventofcode.com/2017/day/18) [](Python/2017/18.ipynb "Python solution for 2017/18")|[**19**](https://adventofcode.com/2017/day/19) [](Python/2017/19.ipynb "Python solution for 2017/19")|[**20**](https://adventofcode.com/2017/day/20) [](Python/2017/20.ipynb "Python solution for 2017/20")|[**21**](https://adventofcode.com/2017/day/21) [](Python/2017/21.ipynb "Python solution for 2017/21")|[**22**](https://adventofcode.com/2017/day/22) [](Python/2017/22.ipynb "Python solution for 2017/22")|[**23**](https://adventofcode.com/2017/day/23) [](Python/2017/23.ipynb "Python solution for 2017/23")|[**24**](https://adventofcode.com/2017/day/24) [](Python/2017/24.ipynb "Python solution for 2017/24")| -|[**25**](https://adventofcode.com/2017/day/25) [](Python/2017/25.ipynb "Python solution for 2017/25")|26|27|28|29|30|31| +|||||[**1**](https://adventofcode.com/2017/day/1) [](Python/2017/01.py "Python solution for 2017/01")|[**2**](https://adventofcode.com/2017/day/2) [](Python/2017/02.py "Python solution for 2017/02")|[**3**](https://adventofcode.com/2017/day/3) [](Python/2017/03.py "Python solution for 2017/03")| +|[**4**](https://adventofcode.com/2017/day/4) [](Python/2017/04.py "Python solution for 2017/04")|[**5**](https://adventofcode.com/2017/day/5) [](Python/2017/05.py "Python solution for 2017/05")|[**6**](https://adventofcode.com/2017/day/6) [](Python/2017/06.py "Python solution for 2017/06")|[**7**](https://adventofcode.com/2017/day/7) [](Python/2017/07.py "Python solution for 2017/07")|[**8**](https://adventofcode.com/2017/day/8) [](Python/2017/08.py "Python solution for 2017/08")|[**9**](https://adventofcode.com/2017/day/9) [](Python/2017/09.py "Python solution for 2017/09")|[**10**](https://adventofcode.com/2017/day/10) [](Python/2017/10.py "Python solution for 2017/10")| +|[**11**](https://adventofcode.com/2017/day/11) [](Python/2017/11.py "Python solution for 2017/11")|[**12**](https://adventofcode.com/2017/day/12) [](Python/2017/12.py "Python solution for 2017/12")|[**13**](https://adventofcode.com/2017/day/13) [](Python/2017/13.py "Python solution for 2017/13")|[**14**](https://adventofcode.com/2017/day/14) [](Python/2017/14.py "Python solution for 2017/14")|[**15**](https://adventofcode.com/2017/day/15) [](Python/2017/15.py "Python solution for 2017/15")|[**16**](https://adventofcode.com/2017/day/16) [](Python/2017/16.py "Python solution for 2017/16")|[**17**](https://adventofcode.com/2017/day/17) [](Python/2017/17.py "Python solution for 2017/17")| +|[**18**](https://adventofcode.com/2017/day/18) [](Python/2017/18.py "Python solution for 2017/18")|[**19**](https://adventofcode.com/2017/day/19) [](Python/2017/19.py "Python solution for 2017/19")|[**20**](https://adventofcode.com/2017/day/20) [](Python/2017/20.py "Python solution for 2017/20")|[**21**](https://adventofcode.com/2017/day/21) [](Python/2017/21.py "Python solution for 2017/21")|[**22**](https://adventofcode.com/2017/day/22) [](Python/2017/22.py "Python solution for 2017/22")|[**23**](https://adventofcode.com/2017/day/23) [](Python/2017/23.py "Python solution for 2017/23")|[**24**](https://adventofcode.com/2017/day/24) [](Python/2017/24.py "Python solution for 2017/24")| +|[**25**](https://adventofcode.com/2017/day/25) [](Python/2017/25.py "Python solution for 2017/25")|26|27|28|29|30|31| ## [2016](https://adventofcode.com/2016) ([ Python](Python/2016): 25/25) |Mo|Tu|We|Th|Fr|Sa|Su|