Move Python solutions
This commit is contained in:
parent
6fae773051
commit
2f1872bbd9
255 changed files with 0 additions and 4323 deletions
109
Python/2015/01.ipynb
Normal file
109
Python/2015/01.ipynb
Normal file
|
@ -0,0 +1,109 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/markdown": [
|
||||
"# Day 01"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.Markdown object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import sys; sys.path.insert(0, \"..\")\n",
|
||||
"\n",
|
||||
"import aoc\n",
|
||||
"\n",
|
||||
"year, day = 2015, 1\n",
|
||||
"\n",
|
||||
"puzzle = aoc.setup(year, day)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"138"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"x = 0\n",
|
||||
"cum = [(x := x + [-1,1][c==\"(\"]) for c in puzzle]\n",
|
||||
"cum[-1]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"1771"
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"cum.index(-1) + 1"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
114
Python/2015/02.ipynb
Normal file
114
Python/2015/02.ipynb
Normal file
|
@ -0,0 +1,114 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/markdown": [
|
||||
"# Day 02"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.Markdown object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import sys; sys.path.insert(0, \"..\")\n",
|
||||
"\n",
|
||||
"import aoc\n",
|
||||
"\n",
|
||||
"year, day = 2015, 2\n",
|
||||
"\n",
|
||||
"puzzle = aoc.setup(year, day)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"1586300"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"boxes = [sorted(map(int, line.split(\"x\"))) for line in puzzle.splitlines()]\n",
|
||||
"total = 0\n",
|
||||
"for a, b, c in boxes:\n",
|
||||
" total += 2*(a*b+a*c+b*c) + a*b\n",
|
||||
"total"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"3737498"
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"total = 0\n",
|
||||
"for a, b, c in boxes:\n",
|
||||
" total += 2*(a+b) + a*b*c\n",
|
||||
"total"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
117
Python/2015/03.ipynb
Normal file
117
Python/2015/03.ipynb
Normal file
|
@ -0,0 +1,117 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/markdown": [
|
||||
"# Day 03"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.Markdown object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import sys; sys.path.insert(0, \"..\")\n",
|
||||
"\n",
|
||||
"import aoc\n",
|
||||
"\n",
|
||||
"year, day = 2015, 3\n",
|
||||
"\n",
|
||||
"puzzle = aoc.setup(year, day)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"2572"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"def get_houses(instructions):\n",
|
||||
" houses = [(x:=0, y:=0)]\n",
|
||||
" for c in instructions:\n",
|
||||
" x += c==\">\"\n",
|
||||
" x -= c==\"<\"\n",
|
||||
" y += c==\"v\"\n",
|
||||
" y -= c==\"^\"\n",
|
||||
" houses.append((x, y))\n",
|
||||
" return houses\n",
|
||||
"\n",
|
||||
"len(set(get_houses(puzzle)))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"2631"
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"len(set(get_houses(puzzle[::2])) | set(get_houses(puzzle[1::2])))"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
118
Python/2015/04.ipynb
Normal file
118
Python/2015/04.ipynb
Normal file
|
@ -0,0 +1,118 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/markdown": [
|
||||
"# Day 04"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.Markdown object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import sys; sys.path.insert(0, \"..\")\n",
|
||||
"\n",
|
||||
"import aoc\n",
|
||||
"\n",
|
||||
"year, day = 2015, 4\n",
|
||||
"\n",
|
||||
"puzzle = aoc.setup(year, day)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"117946"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import hashlib\n",
|
||||
"\n",
|
||||
"def check(inp, cnt):\n",
|
||||
" return hashlib.md5(inp.encode()).hexdigest().startswith(\"0\" * cnt)\n",
|
||||
"\n",
|
||||
"def mine(cnt):\n",
|
||||
" i = 1\n",
|
||||
" while not check(f\"{puzzle}{i}\", cnt):\n",
|
||||
" i += 1\n",
|
||||
" return i\n",
|
||||
"\n",
|
||||
"mine(5)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"3938038"
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"mine(6)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
115
Python/2015/05.ipynb
Normal file
115
Python/2015/05.ipynb
Normal file
|
@ -0,0 +1,115 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/markdown": [
|
||||
"# Day 05"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.Markdown object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import sys; sys.path.insert(0, \"..\")\n",
|
||||
"\n",
|
||||
"import aoc\n",
|
||||
"\n",
|
||||
"year, day = 2015, 5\n",
|
||||
"\n",
|
||||
"puzzle = aoc.setup(year, day)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"238"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import re\n",
|
||||
"\n",
|
||||
"def is_nice(x):\n",
|
||||
" return bool(re.match(r\"^(?=(.*[aeiou]){3,})(?=.*(?P<x>.)(?P=x)).*$\", x)) and not any(e in x for e in [\"ab\", \"cd\", \"pq\", \"xy\"])\n",
|
||||
"\n",
|
||||
"sum(map(is_nice, puzzle.splitlines()))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"69"
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"def is_nice(x):\n",
|
||||
" return bool(re.match(r\"^(?=.*(?P<a>..).*(?P=a))(?=.*(?P<b>.).(?P=b)).*$\", x))\n",
|
||||
"\n",
|
||||
"sum(map(is_nice, puzzle.splitlines()))"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
126
Python/2015/06.ipynb
Normal file
126
Python/2015/06.ipynb
Normal file
|
@ -0,0 +1,126 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/markdown": [
|
||||
"# Day 06"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.Markdown object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import sys; sys.path.insert(0, \"..\")\n",
|
||||
"\n",
|
||||
"import aoc\n",
|
||||
"\n",
|
||||
"year, day = 2015, 6\n",
|
||||
"\n",
|
||||
"puzzle = aoc.setup(year, day)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"377891"
|
||||
]
|
||||
},
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import re\n",
|
||||
"\n",
|
||||
"def parse_instructions(instructions):\n",
|
||||
" out = []\n",
|
||||
" for line in instructions:\n",
|
||||
" inst, *coords = re.match(r\"^([a-z ]+) (\\d+),(\\d+) through (\\d+),(\\d+)$\", line).groups()\n",
|
||||
" out.append(([\"turn off\", \"turn on\", \"toggle\"].index(inst), *map(int, coords)))\n",
|
||||
" return out\n",
|
||||
"\n",
|
||||
"lights = [[0 for _ in range(1000)] for _ in range(1000)]\n",
|
||||
"for inst, x1, y1, x2, y2 in parse_instructions(puzzle.splitlines()):\n",
|
||||
" for y in range(y1, y2+1):\n",
|
||||
" for x in range(x1, x2+1):\n",
|
||||
" lights[y][x] = 1 - lights[y][x] if inst == 2 else inst\n",
|
||||
"sum(map(sum, lights))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"14110788"
|
||||
]
|
||||
},
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"lights = [[0 for _ in range(1000)] for _ in range(1000)]\n",
|
||||
"for inst, x1, y1, x2, y2 in parse_instructions(puzzle.splitlines()):\n",
|
||||
" for y in range(y1, y2+1):\n",
|
||||
" for x in range(x1, x2+1):\n",
|
||||
" lights[y][x] = max(0, lights[y][x] + (inst or -1))\n",
|
||||
"sum(map(sum, lights))"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
139
Python/2015/07.ipynb
Normal file
139
Python/2015/07.ipynb
Normal file
|
@ -0,0 +1,139 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/markdown": [
|
||||
"# Day 07"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.Markdown object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import sys; sys.path.insert(0, \"..\")\n",
|
||||
"\n",
|
||||
"import aoc\n",
|
||||
"\n",
|
||||
"year, day = 2015, 7\n",
|
||||
"\n",
|
||||
"puzzle = aoc.setup(year, day)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 37,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"956"
|
||||
]
|
||||
},
|
||||
"execution_count": 37,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"MOD = 1<<16\n",
|
||||
"\n",
|
||||
"funcs = {dest: args for *args, _, dest in map(str.split, puzzle.splitlines())}\n",
|
||||
"\n",
|
||||
"dp = {}\n",
|
||||
"\n",
|
||||
"def solve(key):\n",
|
||||
" if key not in dp:\n",
|
||||
" if key.isnumeric():\n",
|
||||
" val = int(key)\n",
|
||||
" else:\n",
|
||||
" args = funcs[key]\n",
|
||||
" if len(args) == 1:\n",
|
||||
" val = solve(args[0])\n",
|
||||
" elif len(args) == 2:\n",
|
||||
" if args[0] == \"NOT\":\n",
|
||||
" val = ~solve(args[1]) % MOD\n",
|
||||
" elif len(args) == 3:\n",
|
||||
" if args[1] == \"OR\":\n",
|
||||
" val = solve(args[0]) | solve(args[2])\n",
|
||||
" elif args[1] == \"AND\":\n",
|
||||
" val = solve(args[0]) & solve(args[2])\n",
|
||||
" elif args[1] == \"LSHIFT\":\n",
|
||||
" val = solve(args[0]) << solve(args[2])\n",
|
||||
" elif args[1] == \"RSHIFT\":\n",
|
||||
" val = solve(args[0]) >> solve(args[2])\n",
|
||||
" dp[key] = val % MOD\n",
|
||||
" \n",
|
||||
" return dp[key]\n",
|
||||
"\n",
|
||||
"solve(\"a\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 38,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"40149"
|
||||
]
|
||||
},
|
||||
"execution_count": 38,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"dp.clear()\n",
|
||||
"dp = {\"b\": solve(\"a\")}\n",
|
||||
"solve(\"a\")"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
107
Python/2015/08.ipynb
Normal file
107
Python/2015/08.ipynb
Normal file
|
@ -0,0 +1,107 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/markdown": [
|
||||
"# Day 08"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.Markdown object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import sys; sys.path.insert(0, \"..\")\n",
|
||||
"\n",
|
||||
"import aoc\n",
|
||||
"\n",
|
||||
"year, day = 2015, 8\n",
|
||||
"\n",
|
||||
"puzzle = aoc.setup(year, day)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"1342"
|
||||
]
|
||||
},
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"sum(len(line) - len(eval(line)) for line in puzzle.splitlines())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 21,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"2074"
|
||||
]
|
||||
},
|
||||
"execution_count": 21,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"sum(len(repr(line))+line.count('\"') - len(line) for line in puzzle.splitlines())"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
114
Python/2015/09.ipynb
Normal file
114
Python/2015/09.ipynb
Normal file
|
@ -0,0 +1,114 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/markdown": [
|
||||
"# Day 09"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.Markdown object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import sys; sys.path.insert(0, \"..\")\n",
|
||||
"\n",
|
||||
"import aoc\n",
|
||||
"\n",
|
||||
"year, day = 2015, 9\n",
|
||||
"\n",
|
||||
"puzzle = aoc.setup(year, day)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"207"
|
||||
]
|
||||
},
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import itertools\n",
|
||||
"\n",
|
||||
"graph = {}\n",
|
||||
"for src, _, dst, _, dist in map(str.split, puzzle.splitlines()):\n",
|
||||
" graph.setdefault(src, {})[dst] = int(dist)\n",
|
||||
" graph.setdefault(dst, {})[src] = int(dist)\n",
|
||||
"\n",
|
||||
"min(sum(graph[a][b] for a, b in zip(locs, locs[1:])) for locs in itertools.permutations(graph))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"804"
|
||||
]
|
||||
},
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"max(sum(graph[a][b] for a, b in zip(locs, locs[1:])) for locs in itertools.permutations(graph))"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
125
Python/2015/10.ipynb
Normal file
125
Python/2015/10.ipynb
Normal file
|
@ -0,0 +1,125 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/markdown": [
|
||||
"# Day 10"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.Markdown object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import sys; sys.path.insert(0, \"..\")\n",
|
||||
"\n",
|
||||
"import aoc\n",
|
||||
"\n",
|
||||
"year, day = 2015, 10\n",
|
||||
"\n",
|
||||
"puzzle = aoc.setup(year, day)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"329356"
|
||||
]
|
||||
},
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"def las(inp):\n",
|
||||
" out = \"\"\n",
|
||||
" i = 0\n",
|
||||
" while i < len(inp):\n",
|
||||
" c = inp[i]\n",
|
||||
" j = 1\n",
|
||||
" while i+j < len(inp) and inp[i+j] == c:\n",
|
||||
" j += 1\n",
|
||||
" out += f\"{j}{c}\"\n",
|
||||
" i += j\n",
|
||||
" return out\n",
|
||||
"\n",
|
||||
"x = puzzle\n",
|
||||
"for _ in range(40):\n",
|
||||
" x = las(x)\n",
|
||||
"len(x)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"4666278"
|
||||
]
|
||||
},
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"x = puzzle\n",
|
||||
"for _ in range(50):\n",
|
||||
" x = las(x)\n",
|
||||
"len(x)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
127
Python/2015/11.ipynb
Normal file
127
Python/2015/11.ipynb
Normal file
|
@ -0,0 +1,127 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/markdown": [
|
||||
"# Day 11"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.Markdown object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import sys; sys.path.insert(0, \"..\")\n",
|
||||
"\n",
|
||||
"import aoc\n",
|
||||
"\n",
|
||||
"year, day = 2015, 11\n",
|
||||
"\n",
|
||||
"puzzle = aoc.setup(year, day)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 29,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'cqjxxyzz'"
|
||||
]
|
||||
},
|
||||
"execution_count": 29,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"def inc(x):\n",
|
||||
" x = [ord(c)-0x61 for c in x]\n",
|
||||
" for i in range(len(x))[::-1]:\n",
|
||||
" x[i] += 1\n",
|
||||
" while chr(0x61+x[i]) in \"iol\":\n",
|
||||
" x[i] += 1\n",
|
||||
" if x[i] < 26:\n",
|
||||
" break\n",
|
||||
" x[i] = 0\n",
|
||||
" return \"\".join(chr(c+0x61) for c in x)\n",
|
||||
"\n",
|
||||
"def check(x):\n",
|
||||
" return all(c not in x for c in \"iol\") and any((chr(i)+chr(i+1)+chr(i+2)) in x for i in range(0x61,0x61+24)) and sum(chr(0x61+i)*2 in x for i in range(26))>=2\n",
|
||||
"\n",
|
||||
"def nxt(x):\n",
|
||||
" x = inc(x)\n",
|
||||
" while not check(x):\n",
|
||||
" x = inc(x)\n",
|
||||
" return x\n",
|
||||
"\n",
|
||||
"nxt(puzzle)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 30,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'cqkaabcc'"
|
||||
]
|
||||
},
|
||||
"execution_count": 30,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"nxt(nxt(puzzle))"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
131
Python/2015/12.ipynb
Normal file
131
Python/2015/12.ipynb
Normal file
|
@ -0,0 +1,131 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/markdown": [
|
||||
"# Day 12"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.Markdown object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import sys; sys.path.insert(0, \"..\")\n",
|
||||
"\n",
|
||||
"import aoc\n",
|
||||
"\n",
|
||||
"year, day = 2015, 12\n",
|
||||
"\n",
|
||||
"puzzle = aoc.setup(year, day)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"111754"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import json\n",
|
||||
"\n",
|
||||
"data = json.loads(puzzle)\n",
|
||||
"\n",
|
||||
"def count(obj):\n",
|
||||
" if isinstance(obj, dict):\n",
|
||||
" return sum(map(count, obj.values()))\n",
|
||||
" elif isinstance(obj, list):\n",
|
||||
" return sum(map(count, obj))\n",
|
||||
" elif isinstance(obj, int):\n",
|
||||
" return obj\n",
|
||||
" return 0\n",
|
||||
"\n",
|
||||
"count(data)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"65402"
|
||||
]
|
||||
},
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"def count(obj):\n",
|
||||
" if isinstance(obj, dict):\n",
|
||||
" if \"red\" in obj.values():\n",
|
||||
" return 0\n",
|
||||
" return sum(map(count, obj.values()))\n",
|
||||
" elif isinstance(obj, list):\n",
|
||||
" return sum(map(count, obj))\n",
|
||||
" elif isinstance(obj, int):\n",
|
||||
" return obj\n",
|
||||
" return 0\n",
|
||||
"\n",
|
||||
"count(data)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
121
Python/2015/13.ipynb
Normal file
121
Python/2015/13.ipynb
Normal file
|
@ -0,0 +1,121 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/markdown": [
|
||||
"# Day 13"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.Markdown object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import sys; sys.path.insert(0, \"..\")\n",
|
||||
"\n",
|
||||
"import aoc\n",
|
||||
"\n",
|
||||
"year, day = 2015, 13\n",
|
||||
"\n",
|
||||
"puzzle = aoc.setup(year, day)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"733"
|
||||
]
|
||||
},
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import itertools\n",
|
||||
"\n",
|
||||
"hp = {}\n",
|
||||
"\n",
|
||||
"for a, _, m, c, *_, b in map(str.split, puzzle.splitlines()):\n",
|
||||
" b = b[:-1]\n",
|
||||
" c = int(c)\n",
|
||||
" if m == \"lose\":\n",
|
||||
" c *= -1\n",
|
||||
" hp.setdefault(b, {})[a] = hp[a][b] = hp.setdefault(a, {}).get(b, 0) + c\n",
|
||||
"\n",
|
||||
"def calc(table):\n",
|
||||
" return sum(hp.get(table[i],{}).get(table[(i+1)%len(table)],0) for i in range(len(table)))\n",
|
||||
"\n",
|
||||
"max(map(calc, itertools.permutations(hp)))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"725"
|
||||
]
|
||||
},
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"max(map(calc, itertools.permutations([*hp,\"_\"])))"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
134
Python/2015/14.ipynb
Normal file
134
Python/2015/14.ipynb
Normal file
|
@ -0,0 +1,134 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/markdown": [
|
||||
"# Day 14"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.Markdown object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import sys; sys.path.insert(0, \"..\")\n",
|
||||
"\n",
|
||||
"import aoc\n",
|
||||
"\n",
|
||||
"year, day = 2015, 14\n",
|
||||
"\n",
|
||||
"puzzle = aoc.setup(year, day)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 23,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"2640"
|
||||
]
|
||||
},
|
||||
"execution_count": 23,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"T = 2503\n",
|
||||
"out = 0\n",
|
||||
"\n",
|
||||
"for line in map(str.split, puzzle.splitlines()):\n",
|
||||
" v, t, r = map(int, [line[3], line[6], line[-2]])\n",
|
||||
" x = T // (t + r) * t * v\n",
|
||||
" x += min(t, T % (t + r)) * v\n",
|
||||
" out = max(out, x)\n",
|
||||
"out"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 37,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"1102"
|
||||
]
|
||||
},
|
||||
"execution_count": 37,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"T = 2503\n",
|
||||
"\n",
|
||||
"reindeers = [tuple(map(int, [line[3], line[6], line[-2]])) for line in map(str.split, puzzle.splitlines())]\n",
|
||||
"states = [0]*len(reindeers)\n",
|
||||
"meters = [0]*len(reindeers)\n",
|
||||
"bonus = [0]*len(reindeers)\n",
|
||||
"\n",
|
||||
"for _ in range(T):\n",
|
||||
" for i, (v, t, r) in enumerate(reindeers):\n",
|
||||
" if states[i] >= 0:\n",
|
||||
" meters[i] += v\n",
|
||||
" states[i] += 1\n",
|
||||
" if states[i] >= t:\n",
|
||||
" states[i] = -r\n",
|
||||
" mx = max(meters)\n",
|
||||
" for i, m in enumerate(meters):\n",
|
||||
" if m == mx:\n",
|
||||
" bonus[i] += 1\n",
|
||||
"\n",
|
||||
"max(bonus)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
133
Python/2015/15.ipynb
Normal file
133
Python/2015/15.ipynb
Normal file
|
@ -0,0 +1,133 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/markdown": [
|
||||
"# Day 15"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.Markdown object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import sys; sys.path.insert(0, \"..\")\n",
|
||||
"\n",
|
||||
"import aoc\n",
|
||||
"\n",
|
||||
"year, day = 2015, 15\n",
|
||||
"\n",
|
||||
"puzzle = aoc.setup(year, day)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 32,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"13882464"
|
||||
]
|
||||
},
|
||||
"execution_count": 32,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"TOTAL = 100\n",
|
||||
"\n",
|
||||
"ingredients = [[int(x.strip(\",\")) for x in i[2:-1:2]] for i in map(str.split, puzzle.splitlines())]\n",
|
||||
"\n",
|
||||
"def solve(i, x, prev):\n",
|
||||
" if i == len(ingredients)-1:\n",
|
||||
" out = 1\n",
|
||||
" for p,e in zip(prev,ingredients[-1]):\n",
|
||||
" out *= max(0, p+x*e)\n",
|
||||
" return out\n",
|
||||
" \n",
|
||||
" return max(solve(i + 1, x - j, [a+j*b for a, b in zip(prev, ingredients[i])]) for j in range(x + 1))\n",
|
||||
"\n",
|
||||
"solve(0, TOTAL, [0]*4)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 33,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"11171160"
|
||||
]
|
||||
},
|
||||
"execution_count": 33,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"ingredients = [[int(x.strip(\",\")) for x in i[2::2]] for i in map(str.split, puzzle.splitlines())]\n",
|
||||
"\n",
|
||||
"def solve(i, x, prev):\n",
|
||||
" if i == len(ingredients)-1:\n",
|
||||
" if prev[-1]+x*ingredients[-1][-1] != 500:\n",
|
||||
" return 0\n",
|
||||
" out = 1\n",
|
||||
" for p,e in zip(prev[:-1],ingredients[-1][:-1]):\n",
|
||||
" out *= max(0, p+x*e)\n",
|
||||
" return out\n",
|
||||
" \n",
|
||||
" return max(solve(i + 1, x - j, [a+j*b for a, b in zip(prev, ingredients[i])]) for j in range(x + 1))\n",
|
||||
"\n",
|
||||
"solve(0, TOTAL, [0]*5)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
132
Python/2015/16.ipynb
Normal file
132
Python/2015/16.ipynb
Normal file
|
@ -0,0 +1,132 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/markdown": [
|
||||
"# Day 16"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.Markdown object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import sys; sys.path.insert(0, \"..\")\n",
|
||||
"\n",
|
||||
"import aoc\n",
|
||||
"\n",
|
||||
"year, day = 2015, 16\n",
|
||||
"\n",
|
||||
"puzzle = aoc.setup(year, day)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"373"
|
||||
]
|
||||
},
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"aunts = [{k:int(v) for k, v in map(str.split, \"\".join(line.split()[2:]).replace(\":\",\" \").split(\",\"))} for line in puzzle.splitlines()]\n",
|
||||
"\n",
|
||||
"sue = {\n",
|
||||
" \"children\": 3,\n",
|
||||
" \"cats\": 7,\n",
|
||||
" \"samoyeds\": 2,\n",
|
||||
" \"pomeranians\": 3,\n",
|
||||
" \"akitas\": 0,\n",
|
||||
" \"vizslas\": 0,\n",
|
||||
" \"goldfish\": 5,\n",
|
||||
" \"trees\": 3,\n",
|
||||
" \"cars\": 2,\n",
|
||||
" \"perfumes\": 1,\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
"for i, x in enumerate(aunts):\n",
|
||||
" if all(k not in x or x[k]==v for k, v in sue.items()):\n",
|
||||
" break\n",
|
||||
"i+1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"260"
|
||||
]
|
||||
},
|
||||
"execution_count": 17,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"for i, x in enumerate(aunts):\n",
|
||||
" if not all(k not in x or x[k]>sue[k] for k in [\"cats\",\"trees\"]):\n",
|
||||
" continue\n",
|
||||
" if not all(k not in x or x[k]<sue[k] for k in [\"pomeranians\",\"goldfish\"]):\n",
|
||||
" continue\n",
|
||||
" if all(k not in x or x[k]==v for k, v in sue.items() if k not in [\"cats\",\"trees\",\"pomeranians\",\"goldfish\"]):\n",
|
||||
" break\n",
|
||||
"i+1"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
142
Python/2015/17.ipynb
Normal file
142
Python/2015/17.ipynb
Normal file
|
@ -0,0 +1,142 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/markdown": [
|
||||
"# Day 17"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.Markdown object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import sys; sys.path.insert(0, \"..\")\n",
|
||||
"\n",
|
||||
"import aoc\n",
|
||||
"\n",
|
||||
"year, day = 2015, 17\n",
|
||||
"\n",
|
||||
"puzzle = aoc.setup(year, day)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"1638"
|
||||
]
|
||||
},
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"containers = list(map(int, puzzle.splitlines()))\n",
|
||||
"dp = {}\n",
|
||||
"\n",
|
||||
"def solve(i, x):\n",
|
||||
" if i == len(containers):\n",
|
||||
" return int(x == 0)\n",
|
||||
" \n",
|
||||
" if (i,x) not in dp:\n",
|
||||
" out = solve(i + 1, x)\n",
|
||||
" if containers[i] <= x:\n",
|
||||
" out += solve(i + 1, x - containers[i])\n",
|
||||
" dp[(i, x)] = out\n",
|
||||
" return dp[(i, x)]\n",
|
||||
"\n",
|
||||
"solve(0, 150)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 25,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"17"
|
||||
]
|
||||
},
|
||||
"execution_count": 25,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"containers = list(map(int, puzzle.splitlines()))\n",
|
||||
"dp = {}\n",
|
||||
"\n",
|
||||
"def solve(i, x):\n",
|
||||
" if i == len(containers):\n",
|
||||
" return None if x else (0, 1)\n",
|
||||
" \n",
|
||||
" if (i,x) not in dp:\n",
|
||||
" out = solve(i + 1, x)\n",
|
||||
" if containers[i] <= x:\n",
|
||||
" y = solve(i + 1, x - containers[i])\n",
|
||||
" if y is not None:\n",
|
||||
" l, c = y\n",
|
||||
" l += 1\n",
|
||||
" if out is None or l < out[0]:\n",
|
||||
" out = l, c\n",
|
||||
" elif out is not None and out[0] == l:\n",
|
||||
" out = l, out[1] + c\n",
|
||||
" dp[(i, x)] = out\n",
|
||||
" return dp[(i, x)]\n",
|
||||
"\n",
|
||||
"solve(0, 150)[1]"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
130
Python/2015/18.ipynb
Normal file
130
Python/2015/18.ipynb
Normal file
|
@ -0,0 +1,130 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/markdown": [
|
||||
"# Day 18"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.Markdown object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import sys; sys.path.insert(0, \"..\")\n",
|
||||
"\n",
|
||||
"import aoc\n",
|
||||
"\n",
|
||||
"year, day = 2015, 18\n",
|
||||
"\n",
|
||||
"puzzle = aoc.setup(year, day)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 27,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"814"
|
||||
]
|
||||
},
|
||||
"execution_count": 27,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"gol = puzzle.splitlines()\n",
|
||||
"\n",
|
||||
"for _ in range(100):\n",
|
||||
" gol = [\n",
|
||||
" [\n",
|
||||
" \".#\"[(cnt:=sum(gol[p][q]==\"#\" for r in range(3) for s in range(3) if (r,s)!=(1,1) and 0<=(p:=i-1+r)<len(gol) and 0<=(q:=j-1+s)<len(row))) in (2,3) and x==\"#\" or x==\".\" and cnt==3]\n",
|
||||
" for j, x in enumerate(row)\n",
|
||||
" ]\n",
|
||||
" for i, row in enumerate(gol)\n",
|
||||
" ]\n",
|
||||
"sum(x==\"#\" for row in gol for x in row)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 29,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"924"
|
||||
]
|
||||
},
|
||||
"execution_count": 29,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"gol = list(map(list, puzzle.splitlines()))\n",
|
||||
"gol[0][0] = gol[0][-1] = gol[-1][0] = gol[-1][-1] = \"#\"\n",
|
||||
"\n",
|
||||
"for _ in range(100):\n",
|
||||
" gol = [\n",
|
||||
" [\n",
|
||||
" \".#\"[(cnt:=sum(gol[p][q]==\"#\" for r in range(3) for s in range(3) if (r,s)!=(1,1) and 0<=(p:=i-1+r)<len(gol) and 0<=(q:=j-1+s)<len(row))) in (2,3) and x==\"#\" or x==\".\" and cnt==3]\n",
|
||||
" for j, x in enumerate(row)\n",
|
||||
" ]\n",
|
||||
" for i, row in enumerate(gol)\n",
|
||||
" ]\n",
|
||||
" gol[0][0] = gol[0][-1] = gol[-1][0] = gol[-1][-1] = \"#\"\n",
|
||||
" \n",
|
||||
"sum(x==\"#\" for row in gol for x in row)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
139
Python/2015/19.ipynb
Normal file
139
Python/2015/19.ipynb
Normal file
|
@ -0,0 +1,139 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/markdown": [
|
||||
"# Day 19"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.Markdown object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import sys; sys.path.insert(0, \"..\")\n",
|
||||
"\n",
|
||||
"import aoc\n",
|
||||
"\n",
|
||||
"year, day = 2015, 19\n",
|
||||
"\n",
|
||||
"puzzle = aoc.setup(year, day)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 27,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"518"
|
||||
]
|
||||
},
|
||||
"execution_count": 27,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"*replacements, _, mol = puzzle.splitlines()\n",
|
||||
"rep = {}\n",
|
||||
"for k, _, v in map(str.split, replacements):\n",
|
||||
" rep.setdefault(k, []).append(v)\n",
|
||||
"\n",
|
||||
"out = set()\n",
|
||||
"for k, v in rep.items():\n",
|
||||
" for i in range(len(mol)):\n",
|
||||
" if k == mol[i:i+len(k)]:\n",
|
||||
" for e in v:\n",
|
||||
" out.add(mol[:i] + e + mol[i+len(k):])\n",
|
||||
"len(out)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 78,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"200"
|
||||
]
|
||||
},
|
||||
"execution_count": 78,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"*replacements, _, mol = puzzle.splitlines()\n",
|
||||
"rep = {}\n",
|
||||
"for v, _, k in map(str.split, replacements):\n",
|
||||
" rep[k] = v\n",
|
||||
"\n",
|
||||
"def replace(inp):\n",
|
||||
" out = set()\n",
|
||||
" for k, v in rep.items():\n",
|
||||
" for i in range(len(inp)):\n",
|
||||
" if k == inp[i:i+len(k)]:\n",
|
||||
" out.add(inp[:i] + v + inp[i+len(k):])\n",
|
||||
" return out\n",
|
||||
"\n",
|
||||
"def solve(inp):\n",
|
||||
" if inp == \"e\":\n",
|
||||
" return 0\n",
|
||||
" \n",
|
||||
" for x in replace(inp):\n",
|
||||
" if (y := solve(x)) is not None:\n",
|
||||
" return y + 1\n",
|
||||
"\n",
|
||||
"solve(mol)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
131
Python/2015/20.ipynb
Normal file
131
Python/2015/20.ipynb
Normal file
|
@ -0,0 +1,131 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/markdown": [
|
||||
"# Day 20"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.Markdown object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import sys; sys.path.insert(0, \"..\")\n",
|
||||
"\n",
|
||||
"import aoc\n",
|
||||
"\n",
|
||||
"year, day = 2015, 20\n",
|
||||
"\n",
|
||||
"puzzle = aoc.setup(year, day)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 20,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"665280"
|
||||
]
|
||||
},
|
||||
"execution_count": 20,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"target = int(puzzle) // 10\n",
|
||||
"\n",
|
||||
"houses = [0] * target\n",
|
||||
"for i in range(1, target + 1):\n",
|
||||
" k = i-1\n",
|
||||
" while k < target:\n",
|
||||
" houses[k] += i\n",
|
||||
" k += i\n",
|
||||
"for i, x in enumerate(houses):\n",
|
||||
" if x >= target:\n",
|
||||
" break\n",
|
||||
"i+1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 22,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"705600"
|
||||
]
|
||||
},
|
||||
"execution_count": 22,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"target = int(puzzle) // 11\n",
|
||||
"\n",
|
||||
"houses = [0] * target\n",
|
||||
"for i in range(1, target + 1):\n",
|
||||
" k = i-1\n",
|
||||
" c = 0\n",
|
||||
" while k < target and c < 50:\n",
|
||||
" c += 1\n",
|
||||
" houses[k] += i\n",
|
||||
" k += i\n",
|
||||
"for i, x in enumerate(houses):\n",
|
||||
" if x >= target:\n",
|
||||
" break\n",
|
||||
"i+1"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
169
Python/2015/21.ipynb
Normal file
169
Python/2015/21.ipynb
Normal file
|
@ -0,0 +1,169 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/markdown": [
|
||||
"# Day 21"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.Markdown object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import sys; sys.path.insert(0, \"..\")\n",
|
||||
"\n",
|
||||
"import aoc\n",
|
||||
"\n",
|
||||
"year, day = 2015, 21\n",
|
||||
"\n",
|
||||
"puzzle = aoc.setup(year, day)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 43,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"111"
|
||||
]
|
||||
},
|
||||
"execution_count": 43,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"(*_, boss_hp), (*_, boss_damage), (*_, boss_armor) = map(str.split, puzzle.splitlines())\n",
|
||||
"boss_hp, boss_damage, boss_armor = map(int, [boss_hp, boss_damage, boss_armor])\n",
|
||||
"\n",
|
||||
"player_hp = 100\n",
|
||||
"\n",
|
||||
"NEUTRAL = (0, 0, 0)\n",
|
||||
"shop_weapons = [\n",
|
||||
" (8, 4, 0),\n",
|
||||
" (10, 5, 0),\n",
|
||||
" (25, 6, 0),\n",
|
||||
" (40, 7, 0),\n",
|
||||
" (74, 8, 0),\n",
|
||||
"]\n",
|
||||
"shop_armor = [\n",
|
||||
" NEUTRAL,\n",
|
||||
" (13, 0, 1),\n",
|
||||
" (31, 0, 2),\n",
|
||||
" (53, 0, 3),\n",
|
||||
" (75, 0, 4),\n",
|
||||
" (102, 0, 5),\n",
|
||||
"]\n",
|
||||
"shop_rings = [\n",
|
||||
" NEUTRAL,\n",
|
||||
" NEUTRAL,\n",
|
||||
" (25, 1, 0),\n",
|
||||
" (50, 2, 0),\n",
|
||||
" (100, 3, 0),\n",
|
||||
" (20, 0, 1),\n",
|
||||
" (40, 0, 2),\n",
|
||||
" (80, 0, 3),\n",
|
||||
"]\n",
|
||||
"\n",
|
||||
"def fight(player_damage, player_armor):\n",
|
||||
" player = player_hp\n",
|
||||
" boss = boss_hp\n",
|
||||
" while True:\n",
|
||||
" boss -= max(1, player_damage - boss_armor)\n",
|
||||
" if boss <= 0:\n",
|
||||
" return True\n",
|
||||
" player -= max(1, boss_damage - player_armor)\n",
|
||||
" if player <= 0:\n",
|
||||
" return False\n",
|
||||
"\n",
|
||||
"def combine(*args):\n",
|
||||
" return tuple(map(sum, zip(*args)))\n",
|
||||
" \n",
|
||||
"best = 1e1337\n",
|
||||
"for weapon in shop_weapons:\n",
|
||||
" for armor in shop_armor:\n",
|
||||
" for i, ring1 in enumerate(shop_rings):\n",
|
||||
" for ring2 in shop_rings[i+1:]:\n",
|
||||
" cost, d, a = combine(weapon, armor, ring1, ring2)\n",
|
||||
" if fight(d, a):\n",
|
||||
" best = min(best, cost)\n",
|
||||
"best"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 44,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"188"
|
||||
]
|
||||
},
|
||||
"execution_count": 44,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"best = 0\n",
|
||||
"for weapon in shop_weapons:\n",
|
||||
" for armor in shop_armor:\n",
|
||||
" for i, ring1 in enumerate(shop_rings):\n",
|
||||
" for ring2 in shop_rings[i+1:]:\n",
|
||||
" cost, d, a = combine(weapon, armor, ring1, ring2)\n",
|
||||
" if not fight(d, a):\n",
|
||||
" best = max(best, cost)\n",
|
||||
"best"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
213
Python/2015/22.ipynb
Normal file
213
Python/2015/22.ipynb
Normal file
|
@ -0,0 +1,213 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/markdown": [
|
||||
"# Day 22"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.Markdown object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import sys; sys.path.insert(0, \"..\")\n",
|
||||
"\n",
|
||||
"import aoc\n",
|
||||
"\n",
|
||||
"year, day = 2015, 22\n",
|
||||
"\n",
|
||||
"puzzle = aoc.setup(year, day)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"1824"
|
||||
]
|
||||
},
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"(*_, boss_hp), (*_, boss_damage) = map(str.split, puzzle.splitlines())\n",
|
||||
"boss_hp, boss_damage = map(int, [boss_hp, boss_damage])\n",
|
||||
"\n",
|
||||
"player_hp = 50\n",
|
||||
"player_mana = 500\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def tick(*timers):\n",
|
||||
" return [max(0, t - 1) for t in timers]\n",
|
||||
"\n",
|
||||
"def fight_boss(player, boss, mana, shield_timer, poison_timer, recharge_timer):\n",
|
||||
" armor = 7 if shield_timer else 0\n",
|
||||
" if poison_timer:\n",
|
||||
" boss -= 3\n",
|
||||
" if recharge_timer:\n",
|
||||
" mana += 101\n",
|
||||
" shield_timer, poison_timer, recharge_timer = tick(shield_timer, poison_timer, recharge_timer)\n",
|
||||
" \n",
|
||||
" if boss <= 0:\n",
|
||||
" return 0\n",
|
||||
"\n",
|
||||
" return fight_player(player - max(1, boss_damage - armor), boss, mana, shield_timer, poison_timer, recharge_timer)\n",
|
||||
"\n",
|
||||
"def fight_player(player, boss, mana, shield_timer, poison_timer, recharge_timer):\n",
|
||||
" if player <= 0:\n",
|
||||
" return None\n",
|
||||
" \n",
|
||||
" if poison_timer:\n",
|
||||
" boss -= 3\n",
|
||||
" if recharge_timer:\n",
|
||||
" mana += 101\n",
|
||||
" shield_timer, poison_timer, recharge_timer = tick(shield_timer, poison_timer, recharge_timer)\n",
|
||||
"\n",
|
||||
" if boss <= 0:\n",
|
||||
" return 0\n",
|
||||
"\n",
|
||||
" out = []\n",
|
||||
" \n",
|
||||
" # Magic Missile costs 53 mana. It instantly does 4 damage.\n",
|
||||
" if mana >= 53 and (x := fight_boss(player, boss - 4, mana - 53, shield_timer, poison_timer, recharge_timer)) is not None:\n",
|
||||
" out.append(x + 53)\n",
|
||||
" \n",
|
||||
" # Drain costs 73 mana. It instantly does 2 damage and heals you for 2 hit points.\n",
|
||||
" if mana >= 73 and (x := fight_boss(player + 2, boss - 2, mana - 73, shield_timer, poison_timer, recharge_timer)) is not None:\n",
|
||||
" out.append(x + 73)\n",
|
||||
"\n",
|
||||
" # Shield costs 113 mana. It starts an effect that lasts for 6 turns. \n",
|
||||
" # While it is active, your armor is increased by 7.\n",
|
||||
" if mana >= 113 and not shield_timer and (x := fight_boss(player, boss, mana - 113, 6, poison_timer, recharge_timer)) is not None:\n",
|
||||
" out.append(x + 113)\n",
|
||||
" \n",
|
||||
" # Poison costs 173 mana. It starts an effect that lasts for 6 turns. \n",
|
||||
" # At the start of each turn while it is active, it deals the boss 3 damage.\n",
|
||||
" if mana >= 173 and not poison_timer and (x := fight_boss(player, boss, mana - 173, shield_timer, 6, recharge_timer)) is not None:\n",
|
||||
" out.append(x + 173)\n",
|
||||
" \n",
|
||||
" # Recharge costs 229 mana. It starts an effect that lasts for 5 turns. \n",
|
||||
" # At the start of each turn while it is active, it gives you 101 new mana.\n",
|
||||
" if mana >= 229 and not recharge_timer and (x := fight_boss(player, boss, mana - 229, shield_timer, poison_timer, 5)) is not None:\n",
|
||||
" out.append(x + 229)\n",
|
||||
" \n",
|
||||
" return min(out, default=None)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"fight_player(player_hp, boss_hp, player_mana, 0, 0, 0)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"1937"
|
||||
]
|
||||
},
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"def fight_player(player, boss, mana, shield_timer, poison_timer, recharge_timer):\n",
|
||||
" player -= 1\n",
|
||||
" if player <= 0:\n",
|
||||
" return None\n",
|
||||
" \n",
|
||||
" if poison_timer:\n",
|
||||
" boss -= 3\n",
|
||||
" if recharge_timer:\n",
|
||||
" mana += 101\n",
|
||||
" shield_timer, poison_timer, recharge_timer = tick(shield_timer, poison_timer, recharge_timer)\n",
|
||||
"\n",
|
||||
" if boss <= 0:\n",
|
||||
" return 0\n",
|
||||
"\n",
|
||||
" out = []\n",
|
||||
" \n",
|
||||
" # Magic Missile costs 53 mana. It instantly does 4 damage.\n",
|
||||
" if mana >= 53 and (x := fight_boss(player, boss - 4, mana - 53, shield_timer, poison_timer, recharge_timer)) is not None:\n",
|
||||
" out.append(x + 53)\n",
|
||||
" \n",
|
||||
" # Drain costs 73 mana. It instantly does 2 damage and heals you for 2 hit points.\n",
|
||||
" if mana >= 73 and (x := fight_boss(player + 2, boss - 2, mana - 73, shield_timer, poison_timer, recharge_timer)) is not None:\n",
|
||||
" out.append(x + 73)\n",
|
||||
"\n",
|
||||
" # Shield costs 113 mana. It starts an effect that lasts for 6 turns. \n",
|
||||
" # While it is active, your armor is increased by 7.\n",
|
||||
" if mana >= 113 and not shield_timer and (x := fight_boss(player, boss, mana - 113, 6, poison_timer, recharge_timer)) is not None:\n",
|
||||
" out.append(x + 113)\n",
|
||||
" \n",
|
||||
" # Poison costs 173 mana. It starts an effect that lasts for 6 turns. \n",
|
||||
" # At the start of each turn while it is active, it deals the boss 3 damage.\n",
|
||||
" if mana >= 173 and not poison_timer and (x := fight_boss(player, boss, mana - 173, shield_timer, 6, recharge_timer)) is not None:\n",
|
||||
" out.append(x + 173)\n",
|
||||
" \n",
|
||||
" # Recharge costs 229 mana. It starts an effect that lasts for 5 turns. \n",
|
||||
" # At the start of each turn while it is active, it gives you 101 new mana.\n",
|
||||
" if mana >= 229 and not recharge_timer and (x := fight_boss(player, boss, mana - 229, shield_timer, poison_timer, 5)) is not None:\n",
|
||||
" out.append(x + 229)\n",
|
||||
" \n",
|
||||
" return min(out, default=None)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"fight_player(player_hp, boss_hp, player_mana, 0, 0, 0)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
154
Python/2015/23.ipynb
Normal file
154
Python/2015/23.ipynb
Normal file
|
@ -0,0 +1,154 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/markdown": [
|
||||
"# Day 23"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.Markdown object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import sys; sys.path.insert(0, \"..\")\n",
|
||||
"\n",
|
||||
"import aoc\n",
|
||||
"\n",
|
||||
"year, day = 2015, 23\n",
|
||||
"\n",
|
||||
"puzzle = aoc.setup(year, day)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"170"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"program = puzzle.splitlines()\n",
|
||||
"pc = 0\n",
|
||||
"registers = {\"a\": 0, \"b\": 0}\n",
|
||||
"while pc < len(program):\n",
|
||||
" cmd, args = program[pc].split(maxsplit=1)\n",
|
||||
" args = args.split(\", \")\n",
|
||||
" if cmd == \"hlf\":\n",
|
||||
" registers[args[0]] //= 2\n",
|
||||
" pc += 1\n",
|
||||
" elif cmd == \"tpl\":\n",
|
||||
" registers[args[0]] *= 3\n",
|
||||
" pc += 1\n",
|
||||
" elif cmd == \"inc\":\n",
|
||||
" registers[args[0]] += 1\n",
|
||||
" pc += 1\n",
|
||||
" elif cmd == \"jmp\":\n",
|
||||
" pc += int(args[0])\n",
|
||||
" elif cmd == \"jie\":\n",
|
||||
" pc += int(args[1]) if registers[args[0]] % 2 == 0 else 1\n",
|
||||
" elif cmd == \"jio\":\n",
|
||||
" pc += int(args[1]) if registers[args[0]] == 1 else 1\n",
|
||||
" else:\n",
|
||||
" break\n",
|
||||
"\n",
|
||||
"registers[\"b\"]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"247"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"pc = 0\n",
|
||||
"registers = {\"a\": 1, \"b\": 0}\n",
|
||||
"while pc < len(program):\n",
|
||||
" cmd, args = program[pc].split(maxsplit=1)\n",
|
||||
" args = args.split(\", \")\n",
|
||||
" if cmd == \"hlf\":\n",
|
||||
" registers[args[0]] //= 2\n",
|
||||
" pc += 1\n",
|
||||
" elif cmd == \"tpl\":\n",
|
||||
" registers[args[0]] *= 3\n",
|
||||
" pc += 1\n",
|
||||
" elif cmd == \"inc\":\n",
|
||||
" registers[args[0]] += 1\n",
|
||||
" pc += 1\n",
|
||||
" elif cmd == \"jmp\":\n",
|
||||
" pc += int(args[0])\n",
|
||||
" elif cmd == \"jie\":\n",
|
||||
" pc += int(args[1]) if registers[args[0]] % 2 == 0 else 1\n",
|
||||
" elif cmd == \"jio\":\n",
|
||||
" pc += int(args[1]) if registers[args[0]] == 1 else 1\n",
|
||||
" else:\n",
|
||||
" break\n",
|
||||
"\n",
|
||||
"registers[\"b\"]"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
159
Python/2015/24.ipynb
Normal file
159
Python/2015/24.ipynb
Normal file
|
@ -0,0 +1,159 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/markdown": [
|
||||
"# Day 24"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.Markdown object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import sys; sys.path.insert(0, \"..\")\n",
|
||||
"\n",
|
||||
"import aoc\n",
|
||||
"\n",
|
||||
"year, day = 2015, 24\n",
|
||||
"\n",
|
||||
"puzzle = aoc.setup(year, day)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 39,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"11266889531"
|
||||
]
|
||||
},
|
||||
"execution_count": 39,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"packages = list(map(int, puzzle.splitlines()))\n",
|
||||
"gs = sum(packages) // 3\n",
|
||||
"\n",
|
||||
"dp = {}\n",
|
||||
"def test(i, x, pkg):\n",
|
||||
" if x == 0:\n",
|
||||
" return [[]]\n",
|
||||
" if i == len(pkg):\n",
|
||||
" return []\n",
|
||||
" \n",
|
||||
" if (i, x) not in dp:\n",
|
||||
" out = [[0] + y for y in test(i + 1, x, pkg)]\n",
|
||||
" if packages[i] <= x:\n",
|
||||
" out += [[1] + y for y in test(i + 1, x - pkg[i], pkg)]\n",
|
||||
" dp[(i, x)] = out\n",
|
||||
" return dp[(i, x)]\n",
|
||||
"\n",
|
||||
"arr = []\n",
|
||||
"for x in test(0, gs, packages):\n",
|
||||
" f = [z for y, z in zip(x, packages) if y]\n",
|
||||
" if not test(0, gs, f):\n",
|
||||
" continue\n",
|
||||
" p = 1\n",
|
||||
" for y in f:\n",
|
||||
" p *= y\n",
|
||||
" arr.append((len(f), p))\n",
|
||||
"min(arr)[1]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 40,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"77387711"
|
||||
]
|
||||
},
|
||||
"execution_count": 40,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"packages = list(map(int, puzzle.splitlines()))\n",
|
||||
"gs = sum(packages) // 4\n",
|
||||
"\n",
|
||||
"dp = {}\n",
|
||||
"def test(i, x, pkg):\n",
|
||||
" if x == 0:\n",
|
||||
" return [[]]\n",
|
||||
" if i == len(pkg):\n",
|
||||
" return []\n",
|
||||
" \n",
|
||||
" if (i, x) not in dp:\n",
|
||||
" out = [[0] + y for y in test(i + 1, x, pkg)]\n",
|
||||
" if packages[i] <= x:\n",
|
||||
" out += [[1] + y for y in test(i + 1, x - pkg[i], pkg)]\n",
|
||||
" dp[(i, x)] = out\n",
|
||||
" return dp[(i, x)]\n",
|
||||
"\n",
|
||||
"arr = []\n",
|
||||
"for x in test(0, gs, packages):\n",
|
||||
" f = [z for y, z in zip(x, packages) if y]\n",
|
||||
" if not test(0, gs, f):\n",
|
||||
" continue\n",
|
||||
" p = 1\n",
|
||||
" for y in f:\n",
|
||||
" p *= y\n",
|
||||
" arr.append((len(f), p))\n",
|
||||
"min(arr)[1]"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
99
Python/2015/25.ipynb
Normal file
99
Python/2015/25.ipynb
Normal file
|
@ -0,0 +1,99 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/markdown": [
|
||||
"# Day 25"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.Markdown object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import sys; sys.path.insert(0, \"..\")\n",
|
||||
"\n",
|
||||
"import aoc\n",
|
||||
"\n",
|
||||
"year, day = 2015, 25\n",
|
||||
"\n",
|
||||
"puzzle = aoc.setup(year, day)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 33,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"19980801"
|
||||
]
|
||||
},
|
||||
"execution_count": 33,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import re\n",
|
||||
"\n",
|
||||
"row, col = map(int, re.match(r\"^.*?(\\d+).*?(\\d+).*?$\", puzzle).groups())\n",
|
||||
"\n",
|
||||
"n = (row + col - 2)\n",
|
||||
"(20151125 * pow(252533, n * (n + 1) // 2 + 1 + col - 2, 33554393)) % 33554393"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Puzzle 2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue