[Python/2020] Move solutions into .py files

This commit is contained in:
Felix Bargfeldt 2023-10-29 00:26:40 +02:00
parent 8bb50bc682
commit 8c2be5fb77
Signed by: Defelo
GPG key ID: 2A05272471204DD3
52 changed files with 1140 additions and 5916 deletions

View file

@ -1,209 +0,0 @@
{
"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 = 2020, 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": [
"259716"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nums = list(map(int, plines))\n",
"\n",
"def solve1():\n",
" seen = set()\n",
" for a in nums:\n",
" b = 2020 - a\n",
" if b in seen:\n",
" return a * b\n",
" seen.add(a)\n",
"\n",
"solve1()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"259716"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve1m=lambda N={*map(int,plines)}:[a*b for a in N if(b:=2020-a)in N][0]\n",
"\n",
"solve1m()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"25.3 µs ± 2.3 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n",
"20.7 µs ± 1.22 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
]
}
],
"source": [
"%timeit solve1()\n",
"%timeit solve1m()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Puzzle 2"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"120637440"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def solve2():\n",
" for i, a in enumerate(nums):\n",
" seen = set()\n",
" for b in nums[i+1:]:\n",
" c = 2020 - a - b\n",
" if c in seen:\n",
" return a * b * c\n",
" seen.add(b)\n",
"\n",
"solve2()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"120637440"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve2m=lambda N={*map(int,plines)}:[a*b*c for a in N for b in N if(c:=2020-a-b)in N][0]\n",
"\n",
"solve2m()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"856 µs ± 38.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n",
"6.11 ms ± 558 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit solve2()\n",
"%timeit solve2m()"
]
}
],
"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
}

27
Python/2020/01.py Normal file
View file

@ -0,0 +1,27 @@
from lib import *
input = read_input(2020, 1)
nums = ints(input)
seen = set()
for a in nums:
b = 2020 - a
if b in seen:
print(a * b)
break
seen.add(a)
for i, a in enumerate(nums):
seen = set()
for b in nums[i + 1 :]:
c = 2020 - a - b
if c in seen:
print(a * b * c)
break
seen.add(b)
else:
continue
break

View file

@ -1,206 +0,0 @@
{
"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 = 2020, 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": [
"424"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def solve1():\n",
" out = 0\n",
" for policy, char, password in map(str.split, plines):\n",
" char = char[0]\n",
" a, b = map(int, policy.split(\"-\"))\n",
" out += a <= password.count(char) <= b\n",
" return out\n",
"\n",
"solve1()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"424"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve1m=lambda:sum((x:=[*map(int,p.split(\"-\"))])[0]<=P.count(c[0])<=x[1]for p,c,P in map(str.split,plines))\n",
"\n",
"solve1m()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.19 ms ± 85.4 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n",
"1.33 ms ± 48.4 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%timeit solve1()\n",
"%timeit solve1m()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Puzzle 2"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"747"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def solve2():\n",
" out = 0\n",
" for policy, char, password in map(str.split, plines):\n",
" char = char[0]\n",
" a, b = map(int, policy.split(\"-\"))\n",
" out += (password[a-1] == char) != (password[b-1] == char)\n",
" return out\n",
"\n",
"solve2()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"747"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve2m=lambda:sum(1==sum(c[0]==P[int(x)-1]for x in p.split(\"-\"))for p,c,P in map(str.split,plines))\n",
"\n",
"solve2m()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.18 ms ± 38.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n",
"1.73 ms ± 115 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%timeit solve2()\n",
"%timeit solve2m()"
]
}
],
"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
}

23
Python/2020/02.py Normal file
View file

@ -0,0 +1,23 @@
from lib import *
input = read_input(2020, 2)
lines = input.splitlines()
out = 0
for policy, char, password in map(str.split, lines):
char = char[0]
a, b = map(int, policy.split("-"))
out += a <= password.count(char) <= b
print(out)
out = 0
for policy, char, password in map(str.split, lines):
char = char[0]
a, b = map(int, policy.split("-"))
out += (password[a - 1] == char) != (password[b - 1] == char)
print(out)

View file

@ -1,211 +0,0 @@
{
"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 = 2020, 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": [
"294"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def solve1():\n",
" x = 0\n",
" out = 0\n",
" for i, line in enumerate(plines):\n",
" out += line[x] == \"#\"\n",
" x = (x + 3) % len(line)\n",
" return out\n",
"\n",
"solve1()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"294"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve1m=lambda:sum(l[(i*3)%len(l)]==\"#\"for i,l in enumerate(plines))\n",
"\n",
"solve1m()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"76.8 µs ± 2.77 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n",
"83.8 µs ± 968 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n"
]
}
],
"source": [
"%timeit solve1()\n",
"%timeit solve1m()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Puzzle 2"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5774564250"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def solve2():\n",
" out = 0\n",
" for i, line in enumerate(plines[::2]):\n",
" out += line[i%len(line)] == \"#\"\n",
" for d in [1, 3, 5, 7]:\n",
" x = 0\n",
" o = 0\n",
" for i, line in enumerate(plines):\n",
" o += line[x] == \"#\"\n",
" x = (x + d) % len(line)\n",
" out *= o\n",
" return out\n",
"\n",
"solve2()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5774564250"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve2m=lambda:(o:=(X:=lambda a,b:sum(l[(i*a)%len(l)]==\"#\"for i,l in enumerate(plines[::b])))(1,2))and[o:=o*X(d,1)for d in[1,3,5,7]]and o\n",
"\n",
"solve2m()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"342 µs ± 11.9 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n",
"390 µs ± 11.9 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%timeit solve2()\n",
"%timeit solve2m()"
]
}
],
"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
}

29
Python/2020/03.py Normal file
View file

@ -0,0 +1,29 @@
from lib import *
input = read_input(2020, 3)
lines = input.splitlines()
x = 0
out = 0
for i, line in enumerate(lines):
out += line[x] == "#"
x = (x + 3) % len(line)
print(out)
out = 0
for i, line in enumerate(lines[::2]):
out += line[i % len(line)] == "#"
for d in [1, 3, 5, 7]:
x = 0
o = 0
for i, line in enumerate(lines):
o += line[x] == "#"
x = (x + d) % len(line)
out *= o
print(out)

View file

@ -1,226 +0,0 @@
{
"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 = 2020, 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": [
"200"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def solve1():\n",
" out = 0\n",
" for kv in map(str.split, puzzle.split(\"\\n\\n\")):\n",
" x = {x.split(\":\")[0] for x in kv}\n",
" out += all(e in x for e in [\"byr\",\"iyr\",\"eyr\",\"hgt\",\"hcl\",\"ecl\",\"pid\"])\n",
" return out\n",
"\n",
"solve1()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"200"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve1m=lambda:sum(all(map(k.count,\"byr iyr eyr hgt hcl ecl pid\".split()))for k in puzzle.split(\"\\n\\n\"))\n",
"\n",
"solve1m()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.08 ms ± 56.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n",
"608 µs ± 37.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%timeit solve1()\n",
"%timeit solve1m()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Puzzle 2"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"116"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import re\n",
"\n",
"def valid(x):\n",
" if not re.match(r\"^\\d{4}$\", x.get(\"byr\", \"\")) or not 1920 <= int(x[\"byr\"]) <= 2002:\n",
" return False\n",
" if not re.match(r\"^\\d{4}$\", x.get(\"iyr\", \"\")) or not 2010 <= int(x[\"iyr\"]) <= 2020:\n",
" return False\n",
" if not re.match(r\"^\\d{4}$\", x.get(\"eyr\", \"\")) or not 2020 <= int(x[\"eyr\"]) <= 2030:\n",
" return False\n",
" if m := re.match(r\"^(\\d+)(in|cm)$\", x.get(\"hgt\", \"\")):\n",
" a, b = m.groups()\n",
" if b == \"cm\" and not 150 <= int(a) <= 193:\n",
" return False\n",
" elif b == \"in\" and not 59 <= int(a) <= 76:\n",
" return False\n",
" else:\n",
" return False\n",
" if not re.match(r\"^#[\\da-f]{6}$\", x.get(\"hcl\", \"\")):\n",
" return False\n",
" if x.get(\"ecl\", \"\") not in \"amb blu brn gry grn hzl oth\".split():\n",
" return False\n",
" return bool(re.match(r\"^\\d{9}$\", x.get(\"pid\", \"\")))\n",
"\n",
"def solve2():\n",
" out = 0\n",
" for kv in map(str.split, puzzle.split(\"\\n\\n\")):\n",
" out += valid(dict(x.split(\":\") for x in kv))\n",
" return out\n",
"\n",
"solve2()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"116"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve2m=lambda f=__import__(\"re\").fullmatch:sum(all([d:=dict(x.split(\":\")for x in K)]+[f(p,d.get(k,\"\"))for p,k in[(r\"19[2-9]\\d|200[012]\",\"byr\"),(r\"20(1\\d|20)\",\"iyr\"),(r\"20(2\\d|30)\",\"eyr\"),(r\"1([5-8]\\d|9[0-3])cm|(59|6\\d|7[0-6])+in\",\"hgt\"),(r\"#[\\da-f]{6}\",\"hcl\"),(\"amb|blu|brn|gry|grn|hzl|oth\",\"ecl\"),(r\"\\d{9}\",\"pid\")]])for K in map(str.split,puzzle.split(\"\\n\\n\")))\n",
"\n",
"solve2m()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2.44 ms ± 38 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n",
"3.71 ms ± 904 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit solve2()\n",
"%timeit solve2m()"
]
}
],
"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
}

49
Python/2020/04.py Normal file
View file

@ -0,0 +1,49 @@
from lib import *
input = read_input(2020, 4)
out = 0
for kv in map(str.split, input.split("\n\n")):
x = {x.split(":")[0] for x in kv}
out += all(e in x for e in ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"])
print(out)
def valid(x):
if not re.match(r"^\d{4}$", x.get("byr", "")) or not 1920 <= int(x["byr"]) <= 2002:
return False
if not re.match(r"^\d{4}$", x.get("iyr", "")) or not 2010 <= int(x["iyr"]) <= 2020:
return False
if not re.match(r"^\d{4}$", x.get("eyr", "")) or not 2020 <= int(x["eyr"]) <= 2030:
return False
if m := re.match(r"^(\d+)(in|cm)$", x.get("hgt", "")):
a, b = m.groups()
if b == "cm" and not 150 <= int(a) <= 193:
return False
elif b == "in" and not 59 <= int(a) <= 76:
return False
else:
return False
if not re.match(r"^#[\da-f]{6}$", x.get("hcl", "")):
return False
if x.get("ecl", "") not in "amb blu brn gry grn hzl oth".split():
return False
return bool(re.match(r"^\d{9}$", x.get("pid", "")))
out = 0
for kv in map(str.split, input.split("\n\n")):
out += valid(dict(x.split(":") for x in kv))
print(out)

View file

@ -1,208 +0,0 @@
{
"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 = 2020, 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": [
"938"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def solve1():\n",
" out = 0\n",
" for line in plines:\n",
" row = int(line[:7].replace(\"F\", \"0\").replace(\"B\", \"1\"), 2)\n",
" col = int(line[-3:].replace(\"L\", \"0\").replace(\"R\", \"1\"), 2)\n",
" out = max(out, row * 8 + col)\n",
" return out\n",
"\n",
"solve1()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"938"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve1m=lambda:max(int(l.translate({70:\"0\",76:\"0\",66:\"1\",82:\"1\"}),2)for l in plines)\n",
"\n",
"solve1m()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.14 ms ± 22.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n",
"617 µs ± 15.4 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%timeit solve1()\n",
"%timeit solve1m()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Puzzle 2"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"696"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def solve2():\n",
" seats = set()\n",
" found = set()\n",
" for line in plines:\n",
" row = int(line[:7].replace(\"F\", \"0\").replace(\"B\", \"1\"), 2)\n",
" col = int(line[-3:].replace(\"L\", \"0\").replace(\"R\", \"1\"), 2)\n",
" found.add(s := row * 8 + col)\n",
" seats |= {s - 1, s + 1}\n",
" return next(iter(seats - found - {min(seats), max(seats)}))\n",
"\n",
"solve2()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"696"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve2m=lambda:[*{*range(min(S:={int(l.translate({70:\"0\",76:\"0\",66:\"1\",82:\"1\"}),2)for l in plines}),max(S)+1)}-S][0]\n",
"\n",
"solve2m()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.4 ms ± 45.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n",
"701 µs ± 13 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%timeit solve2()\n",
"%timeit solve2m()"
]
}
],
"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
}

25
Python/2020/05.py Normal file
View file

@ -0,0 +1,25 @@
from lib import *
input = read_input(2020, 5)
lines = input.splitlines()
out = 0
for line in lines:
row = int(line[:7].replace("F", "0").replace("B", "1"), 2)
col = int(line[-3:].replace("L", "0").replace("R", "1"), 2)
out = max(out, row * 8 + col)
print(out)
seats = set()
found = set()
for line in lines:
row = int(line[:7].replace("F", "0").replace("B", "1"), 2)
col = int(line[-3:].replace("L", "0").replace("R", "1"), 2)
found.add(s := row * 8 + col)
seats |= {s - 1, s + 1}
print(next(iter(seats - found - {min(seats), max(seats)})))

View file

@ -1,205 +0,0 @@
{
"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 = 2020, 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": [
"6782"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def solve1():\n",
" out = 0\n",
" for group in puzzle.split(\"\\n\\n\"):\n",
" out += len(set(group.replace(\"\\n\",\"\")))\n",
" return out\n",
"\n",
"solve1()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6782"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve1m=lambda:sum(len(set(g)-{\"\\n\"})for g in puzzle.split(\"\\n\\n\"))\n",
"\n",
"solve1m()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"872 µs ± 51.6 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n",
"1.12 ms ± 43 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%timeit solve1()\n",
"%timeit solve1m()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Puzzle 2"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3596"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def solve2():\n",
" out = 0\n",
" for group in puzzle.split(\"\\n\\n\"):\n",
" x = set(group.replace(\"\\n\",\"\"))\n",
" for person in group.split():\n",
" x &= set(person)\n",
" out += len(x)\n",
" return out\n",
"\n",
"solve2()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3596"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve2m=lambda:sum(len(set.intersection(*map(set,g.split())))for g in puzzle.split(\"\\n\\n\"))\n",
"\n",
"solve2m()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.12 ms ± 82.6 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n",
"2.16 ms ± 29.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit solve2()\n",
"%timeit solve2m()"
]
}
],
"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
}

20
Python/2020/06.py Normal file
View file

@ -0,0 +1,20 @@
from lib import *
input = read_input(2020, 6)
out = 0
for group in input.split("\n\n"):
out += len(set(group.replace("\n", "")))
print(out)
out = 0
for group in input.split("\n\n"):
x = set(group.replace("\n", ""))
for person in group.split():
x &= set(person)
out += len(x)
print(out)

View file

@ -1,228 +0,0 @@
{
"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 = 2020, 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": [
"124"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import re\n",
"\n",
"def solve1():\n",
" g = {}\n",
" for line in plines:\n",
" a, *b = re.findall(r\"([a-z]+ [a-z]+) bags?\", line)\n",
" for x in b:\n",
" g.setdefault(x, []).append(a)\n",
"\n",
" cnt = -1\n",
" q = [\"shiny gold\"]\n",
" visited = set()\n",
" while q:\n",
" p = q.pop(0)\n",
" \n",
" if p in visited:\n",
" continue\n",
" visited.add(p)\n",
" \n",
" cnt += 1\n",
" q += g.get(p, [])\n",
" return cnt\n",
"\n",
"solve1()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"124"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve1m=lambda f=__import__(\"re\").findall:len((g:={})or[[(m:=f(\"([a-z]+ [a-z]+) bag\",l)),[g.setdefault(x,[]).append(m[0])for x in m[1:]]]for l in plines]and(dfs:=lambda p:set.union({p},*map(dfs,g.get(p,[]))))(\"shiny gold\"))-1\n",
"\n",
"solve1m()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2.63 ms ± 188 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n",
"2.94 ms ± 77.8 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit solve1()\n",
"%timeit solve1m()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Puzzle 2"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"34862"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def solve2():\n",
" g = {}\n",
" for line in plines:\n",
" a = re.match(r\"^([a-z]+ [a-z]+) bags\", line).group(1)\n",
" b = re.findall(r\"(\\d+) ([a-z]+ [a-z]+) bags?\", line)\n",
" g.setdefault(a, []).extend(b)\n",
" cnt = -1\n",
" q = [(1, \"shiny gold\")]\n",
" while q:\n",
" n, p = q.pop(0)\n",
" \n",
" cnt += n\n",
" q += [(n * int(a), b) for a, b in g.get(p, [])]\n",
" return cnt\n",
"\n",
"solve2()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"34862"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve2m=lambda r=__import__(\"re\"):(g:={})or[g.setdefault(r.match(r\"^(\\w+ \\w+)\",l).group(1),[]).extend(r.findall(r\"(\\d+) (\\w+ \\w+) bag\",l))for l in plines]and(dfs:=lambda p:1+sum(dfs(q)*int(n)for n,q in g.get(p,[])))(\"shiny gold\")-1\n",
"\n",
"solve2m()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3 ms ± 301 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n",
"3.08 ms ± 113 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit solve2()\n",
"%timeit solve2m()"
]
}
],
"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
}

43
Python/2020/07.py Normal file
View file

@ -0,0 +1,43 @@
from lib import *
input = read_input(2020, 7)
lines = input.splitlines()
g = {}
for line in lines:
a, *b = re.findall(r"([a-z]+ [a-z]+) bags?", line)
for x in b:
g.setdefault(x, []).append(a)
cnt = -1
q = ["shiny gold"]
visited = set()
while q:
p = q.pop(0)
if p in visited:
continue
visited.add(p)
cnt += 1
q += g.get(p, [])
print(cnt)
g = {}
for line in lines:
a = re.match(r"^([a-z]+ [a-z]+) bags", line).group(1)
b = re.findall(r"(\d+) ([a-z]+ [a-z]+) bags?", line)
g.setdefault(a, []).extend(b)
cnt = -1
q = [(1, "shiny gold")]
while q:
n, p = q.pop(0)
cnt += n
q += [(n * int(a), b) for a, b in g.get(p, [])]
print(cnt)

View file

@ -1,240 +0,0 @@
{
"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 = 2020, 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": [
"1654"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def solve1():\n",
" acc = 0\n",
" pc = 0\n",
" seen = set()\n",
" while pc < len(plines):\n",
" if pc in seen:\n",
" break\n",
" seen.add(pc)\n",
" \n",
" a, b = plines[pc].split()\n",
" if a == \"acc\":\n",
" acc += int(b)\n",
" pc += 1\n",
" elif a == \"jmp\":\n",
" pc += int(b)\n",
" elif a == \"nop\":\n",
" pc += 1\n",
" return acc\n",
"\n",
"solve1()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1654"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve1m=lambda p=[0],a=[0]:[*iter(lambda:(p[-1]not in p[:-1]and(k:=plines[p[-1]].split(),a.append(a[-1]+(k[0]==\"acc\")*int(k[1])),p.append(p[-1]+(int(k[1])if\"jmp\"==k[0] else 1)))),False)]and a[-1]\n",
"\n",
"solve1m()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"145 µs ± 5.14 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n",
"2.22 µs ± 85.6 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
]
}
],
"source": [
"%timeit solve1()\n",
"%timeit solve1m()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Puzzle 2"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"833"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def simulate(flip):\n",
" acc = 0\n",
" pc = 0\n",
" seen = set()\n",
" while pc < len(plines):\n",
" if pc in seen:\n",
" return None\n",
" seen.add(pc)\n",
" \n",
" a, b = plines[pc].split()\n",
" if pc == flip:\n",
" if a == \"nop\":\n",
" a = \"jmp\"\n",
" elif a == \"jmp\":\n",
" a = \"nop\"\n",
" if a == \"acc\":\n",
" acc += int(b)\n",
" pc += 1\n",
" elif a == \"jmp\":\n",
" pc += int(b)\n",
" elif a == \"nop\":\n",
" pc += 1\n",
" \n",
" return acc\n",
"\n",
"\n",
"def solve2():\n",
" for i in range(len(plines)):\n",
" if (out := simulate(i)) is not None:\n",
" return out\n",
"\n",
"solve2()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"833"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve2m=lambda:next(iter(y[1]for x in range(len(puzzle))if(y:=(lambda f,p=[0],a=[0],s=set():[*iter(lambda:((c:=p[-1])<len(plines)and c not in s and(s.add(c),k:=plines[c].split(),k[0]==\"acc\"and a.append(a.pop()+int(k[1])),p.append(p.pop()+(int(k[1])if\"jmp\"==k[0]and f!=c or\"nop\"==k[0]and f==c else 1)))),False)]and(p[-1]>=len(plines),a[-1]))(x))[0]))\n",
"\n",
"solve2m()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"29.9 ms ± 740 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n",
"54.3 ms ± 3.72 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
]
}
],
"source": [
"%timeit solve2()\n",
"%timeit solve2m()"
]
}
],
"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
}

68
Python/2020/08.py Normal file
View file

@ -0,0 +1,68 @@
from lib import *
input = read_input(2020, 8)
lines = input.splitlines()
acc = 0
pc = 0
seen = set()
while pc < len(lines):
if pc in seen:
break
seen.add(pc)
a, b = lines[pc].split()
if a == "acc":
acc += int(b)
pc += 1
elif a == "jmp":
pc += int(b)
elif a == "nop":
pc += 1
print(acc)
def simulate(flip):
acc = 0
pc = 0
seen = set()
while pc < len(lines):
if pc in seen:
return None
seen.add(pc)
a, b = lines[pc].split()
if pc == flip:
if a == "nop":
a = "jmp"
elif a == "jmp":
a = "nop"
if a == "acc":
acc += int(b)
pc += 1
elif a == "jmp":
pc += int(b)
elif a == "nop":
pc += 1
return acc
for i in range(len(lines)):
if (out := simulate(i)) is not None:
print(out)
break

View file

@ -1,218 +0,0 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"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 = 2020, 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": [
"31161678"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nums = list(map(int, plines))\n",
"\n",
"def test(i, num):\n",
" seen = set()\n",
" for j in range(25):\n",
" x = nums[j+i]\n",
" if num - x != x and num - x in seen:\n",
" return True\n",
" seen.add(x)\n",
" return False\n",
"\n",
"def solve1():\n",
" for i, x in enumerate(nums[25:]):\n",
" if not test(i, x):\n",
" return x\n",
"\n",
"solve1()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"31161678"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve1m=lambda n=[*map(int,plines)]:next(iter(x for i,x in enumerate(n[25:])if(s:=set())or 1-any((2*y-x and x-y in s)or s.add(y)for y in n[i:i+25])))\n",
"\n",
"solve1m()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.45 ms ± 56.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n",
"1.65 ms ± 86.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%timeit solve1()\n",
"%timeit solve1m()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Puzzle 2"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5453868"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def solve2():\n",
" target = solve1()\n",
" for a in range(len(nums)):\n",
" cum = 0\n",
" for b in range(len(nums) - a):\n",
" cum += nums[a+b]\n",
" if b >= 1 and cum == target:\n",
" x = nums[a:a+b+1]\n",
" return min(x) +max(x)\n",
" elif cum > target:\n",
" break\n",
"\n",
"solve2()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5453868"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve2m=lambda _=(n:=[*map(int,plines)]),t=next(iter(x for i,x in enumerate(n[25:])if(s:=set())or 1-any((2*y-x and x-y in s)or s.add(y)for y in n[i:i+25]))):next(iter(min(n[a:b])+max(n[a:b])for a in range(len(n))for b in range(a+2,len(n))if(s:=sum(n[a:b])==t)))\n",
"\n",
"solve2m()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"17.7 ms ± 1.76 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n",
"1.86 s ± 24.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit solve2()\n",
"%timeit solve2m()"
]
}
],
"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
}

35
Python/2020/09.py Normal file
View file

@ -0,0 +1,35 @@
from lib import *
input = read_input(2020, 9)
nums = ints(input)
def test(i, num):
seen = set()
for j in range(25):
x = nums[j + i]
if num - x != x and num - x in seen:
return True
seen.add(x)
return False
for i, x in enumerate(nums[25:]):
if not test(i, x):
target = x
break
print(target)
for a in range(len(nums)):
cum = 0
for b in range(len(nums) - a):
cum += nums[a + b]
if b >= 1 and cum == target:
x = nums[a : a + b + 1]
print(min(x) + max(x))
exit()
elif cum > target:
break

View file

@ -1,219 +0,0 @@
{
"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 = 2020, 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": [
"1885"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from collections import Counter\n",
"\n",
"nums = sorted(map(int, plines))\n",
"\n",
"def solve1():\n",
" prev = 0\n",
" counter = Counter()\n",
" for x in nums:\n",
" d = x - prev\n",
" counter[d] += 1\n",
" prev = x\n",
" counter[3] += 1\n",
" return counter[1] * counter[3]\n",
"\n",
"solve1()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1885"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve1m=lambda n=sorted(map(int,plines)):(X:=[b-a for a,b in zip([0]+n,n)].count)(1)*(X(3)+1)\n",
"\n",
"solve1m()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"28.5 µs ± 707 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n",
"13.7 µs ± 401 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
]
}
],
"source": [
"%timeit solve1()\n",
"%timeit solve1m()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Puzzle 2"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2024782584832"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dp = {}\n",
"def count(idx, joltage):\n",
" if idx == len(nums):\n",
" return joltage == nums[-1]\n",
" if nums[idx] - joltage > 3:\n",
" return 0\n",
" \n",
" if (idx, joltage) not in dp:\n",
" dp[(idx, joltage)] = count(idx + 1, joltage) + count(idx + 1, nums[idx])\n",
" return dp[(idx, joltage)]\n",
"\n",
"def solve2():\n",
" dp.clear()\n",
" return count(0, 0)\n",
"\n",
"solve2()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2024782584832"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve2m=lambda n=sorted(map(int,plines)):(d:={})or(c:=lambda i,j:j==n[-1]if i==len(n)else(0 if 3+j<n[i]else(d[(i,j)]if(i,j)in d else d.setdefault((i,j),c(i+1,j)+c(i+1,n[i])))))(0,0)\n",
"\n",
"solve2m()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"175 µs ± 4.6 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n",
"184 µs ± 8.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%timeit solve2()\n",
"%timeit solve2m()"
]
}
],
"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
}

37
Python/2020/10.py Normal file
View file

@ -0,0 +1,37 @@
from lib import *
input = read_input(2020, 10)
from collections import Counter
nums = sorted(ints(input))
prev = 0
counter = Counter()
for x in nums:
d = x - prev
counter[d] += 1
prev = x
counter[3] += 1
print(counter[1] * counter[3])
dp = {}
def count(idx, joltage):
if idx == len(nums):
return joltage == nums[-1]
if nums[idx] - joltage > 3:
return 0
if (idx, joltage) not in dp:
dp[(idx, joltage)] = count(idx + 1, joltage) + count(idx + 1, nums[idx])
return dp[(idx, joltage)]
print(count(0, 0))

View file

@ -1,254 +0,0 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"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 = 2020, 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": [
"2113"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def solve1():\n",
" graph = {}\n",
" active = set()\n",
" for y, line in enumerate(plines):\n",
" for x, c in enumerate(line):\n",
" if c == \".\":\n",
" continue\n",
" \n",
" graph[(x, y)] = []\n",
" for dy in range(-1, 2):\n",
" for dx in range(-1, 2):\n",
" if dx == 0 == dy:\n",
" continue\n",
" if 0 <= (i := y+dy) < len(plines) and 0 <= (j := x+dx) < len(line) and plines[i][j] != \".\":\n",
" graph[(x, y)].append((j, i))\n",
"\n",
" while True:\n",
" new_active = set()\n",
" \n",
" for p, qs in graph.items():\n",
" cnt = sum(q in active for q in qs)\n",
" if p not in active and not cnt or p in active and cnt < 4:\n",
" new_active.add(p)\n",
" \n",
" if active == new_active:\n",
" break\n",
" active = new_active\n",
"\n",
" return len(active)\n",
"\n",
"solve1()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2113"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve1m=lambda a=set(),g={(x,y):[(x+q,y+p)for p in[-1,0,1]for q in[-1,0,1]if(p or q)*(0<=y+p<len(plines))*(0<=x+q<len(l))and plines[y+p][x+q]!=\".\"]for y,l in enumerate(plines)for x,c in enumerate(l)if\".\"!=c}:next(len(a)for()in iter(tuple,1)if a==(a:={p for p,q in g.items()if not(c:=len(a&{*q}))and{p}-a or a&{p}and c<4}))\n",
"\n",
"solve1m()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.25 s ± 131 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"1 s ± 36.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit solve1()\n",
"%timeit solve1m()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Puzzle 2"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1865"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def solve2():\n",
" graph = {}\n",
" active = set()\n",
" for y, line in enumerate(plines):\n",
" for x, c in enumerate(line):\n",
" if c == \".\":\n",
" continue\n",
" \n",
" graph[(x, y)] = []\n",
" for dy in range(-1, 2):\n",
" for dx in range(-1, 2):\n",
" if dx == 0 == dy:\n",
" continue\n",
" k = 1\n",
" while 0 <= (i := y+k*dy) < len(plines) and 0 <= (j := x+k*dx) < len(line):\n",
" if plines[i][j] != \".\":\n",
" graph[(x, y)].append((j, i))\n",
" break\n",
" k += 1\n",
"\n",
" while True:\n",
" new_active = set()\n",
" \n",
" for p, qs in graph.items():\n",
" cnt = sum(q in active for q in qs)\n",
" if p not in active and not cnt or p in active and cnt < 5:\n",
" new_active.add(p)\n",
" \n",
" if active == new_active:\n",
" break\n",
" active = new_active\n",
"\n",
" return len(active)\n",
"\n",
"solve2()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1865"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve2m=lambda a=set(),g={(x,y):[f for p in[-1,0,1]for q in[-1,0,1]if(p or q)*(k:=1)and(f:=next((r*(x+k*q,y+k*p)for()in iter(tuple,0)if not(r:=(0<=y+k*p<len(plines))*(0<=x+k*q<len(l)))or plines[y+k*p][x+k*q]!=\".\"or(k:=k+1)*0),0))]for y,l in enumerate(plines)for x,c in enumerate(l)if\".\"!=c}:next(len(a)for()in iter(tuple,1)if a==(a:={p for p,q in g.items()if not(c:=len(a&{*q}))and{p}-a or a&{p}and c<5}))\n",
"\n",
"solve2m()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.07 s ± 40.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"914 ms ± 68.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit solve2()\n",
"%timeit solve2m()"
]
}
],
"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
}

62
Python/2020/11.py Normal file
View file

@ -0,0 +1,62 @@
from lib import *
input = read_input(2020, 11)
lines = input.splitlines()
graph = {}
active = set()
for y, line in enumerate(lines):
for x, c in enumerate(line):
if c == ".":
continue
graph[(x, y)] = []
for dy in range(-1, 2):
for dx in range(-1, 2):
if dx == 0 == dy:
continue
if 0 <= (i := y + dy) < len(lines) and 0 <= (j := x + dx) < len(line) and lines[i][j] != ".":
graph[(x, y)].append((j, i))
while True:
new_active = set()
for p, qs in graph.items():
cnt = sum(q in active for q in qs)
if p not in active and not cnt or p in active and cnt < 4:
new_active.add(p)
if active == new_active:
break
active = new_active
print(len(active))
graph = {}
active = set()
for y, line in enumerate(lines):
for x, c in enumerate(line):
if c == ".":
continue
graph[(x, y)] = []
for dy in range(-1, 2):
for dx in range(-1, 2):
if dx == 0 == dy:
continue
k = 1
while 0 <= (i := y + k * dy) < len(lines) and 0 <= (j := x + k * dx) < len(line):
if lines[i][j] != ".":
graph[(x, y)].append((j, i))
break
k += 1
while True:
new_active = set()
for p, qs in graph.items():
cnt = sum(q in active for q in qs)
if p not in active and not cnt or p in active and cnt < 5:
new_active.add(p)
if active == new_active:
break
active = new_active
print(len(active))

View file

@ -1,232 +0,0 @@
{
"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 = 2020, 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": [
"590"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def solve1():\n",
" x = y = 0\n",
" dx, dy = 1, 0\n",
" for line in plines:\n",
" n = int(line[1:])\n",
" cmd = line[0]\n",
" if cmd == \"N\": y -= n\n",
" if cmd == \"E\": x += n\n",
" if cmd == \"S\": y += n\n",
" if cmd == \"W\": x -= n\n",
" if cmd == \"F\":\n",
" x += dx*n\n",
" y += dy*n\n",
" if cmd == \"L\":\n",
" for _ in range(n//90%4):\n",
" dx, dy = dy, -dx\n",
" if cmd == \"R\":\n",
" for _ in range(n//90%4):\n",
" dx, dy = -dy, dx\n",
" return abs(x) + abs(y)\n",
"\n",
"solve1()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"590"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve1m=lambda x=0,y=0,p=1,q=0:[(c:=l[0],n:=int(l[1:]),y:=y+n*(c==\"S\")-n*(c==\"N\")+n*q*(c==\"F\"),x:=x+n*(c==\"E\")-n*(c==\"W\")+n*p*(c==\"F\"),[(k:=q,q:=p-p*2*(c==\"L\"),p:=k-k*2*(c==\"R\"))for()in(c in\"LR\")*n//90*[()]])for l in plines]and abs(x)+abs(y)\n",
"\n",
"solve1m()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"564 µs ± 26.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n",
"1.28 ms ± 106 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%timeit solve1()\n",
"%timeit solve1m()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Puzzle 2"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"42013"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def solve2():\n",
" wx, wy = 10, -1\n",
" sx = sy = 0\n",
" for line in plines:\n",
" n = int(line[1:])\n",
" cmd = line[0]\n",
" if cmd == \"N\": wy -= n\n",
" if cmd == \"E\": wx += n\n",
" if cmd == \"S\": wy += n\n",
" if cmd == \"W\": wx -= n\n",
" if cmd == \"F\":\n",
" sx += wx * n\n",
" sy += wy * n\n",
" if cmd == \"L\":\n",
" for _ in range(n//90%4):\n",
" wx, wy = wy, -wx\n",
" if cmd == \"R\":\n",
" for _ in range(n//90%4):\n",
" wx, wy = -wy, wx\n",
" return abs(sx) + abs(sy)\n",
"\n",
"solve2()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"42013"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve2m=lambda x=0,y=0,p=10,q=-1:[(c:=l[0],n:=int(l[1:]),p:=p+n*(c==\"E\")-n*(c==\"W\"),q:=q+n*(c==\"S\")-n*(c==\"N\"),x:=x+n*p*(c==\"F\"),y:=y+n*q*(c==\"F\"),[(k:=q,q:=p-p*2*(c==\"L\"),p:=k-k*2*(c==\"R\"))for()in(c in\"LR\")*n//90*[()]])for l in plines]and abs(x)+abs(y)\n",
"\n",
"solve2m()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"574 µs ± 5.26 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n",
"1.32 ms ± 28.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%timeit solve2()\n",
"%timeit solve2m()"
]
}
],
"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
}

56
Python/2020/12.py Normal file
View file

@ -0,0 +1,56 @@
from lib import *
input = read_input(2020, 12)
lines = input.splitlines()
x = y = 0
dx, dy = 1, 0
for line in lines:
n = int(line[1:])
cmd = line[0]
if cmd == "N":
y -= n
if cmd == "E":
x += n
if cmd == "S":
y += n
if cmd == "W":
x -= n
if cmd == "F":
x += dx * n
y += dy * n
if cmd == "L":
for _ in range(n // 90 % 4):
dx, dy = dy, -dx
if cmd == "R":
for _ in range(n // 90 % 4):
dx, dy = -dy, dx
print(abs(x) + abs(y))
wx, wy = 10, -1
sx = sy = 0
for line in lines:
n = int(line[1:])
cmd = line[0]
if cmd == "N":
wy -= n
if cmd == "E":
wx += n
if cmd == "S":
wy += n
if cmd == "W":
wx -= n
if cmd == "F":
sx += wx * n
sy += wy * n
if cmd == "L":
for _ in range(n // 90 % 4):
wx, wy = wy, -wx
if cmd == "R":
for _ in range(n // 90 % 4):
wx, wy = -wy, wx
print(abs(sx) + abs(sy))

View file

@ -1,229 +0,0 @@
{
"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 = 2020, 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": [
"1835"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def solve1():\n",
" target = int(plines[0])\n",
" nums = [int(n) for n in plines[1].split(\",\") if n != \"x\"]\n",
" t, n = min([((target // n + 1) * n - target, n) for n in nums])\n",
" return t * n\n",
"\n",
"solve1()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1835"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve1m=lambda T=int(plines[0]):int.__mul__(*min(((T//i+1)*i-T,i)for n in plines[1].split(\",\")if\"x\"!=n and(i:=int(n))))\n",
"\n",
"solve1m()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"9.16 µs ± 204 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n",
"8.95 µs ± 211 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
]
}
],
"source": [
"%timeit solve1()\n",
"%timeit solve1m()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Puzzle 2"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"247086664214628"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from functools import reduce\n",
"\n",
"def extended_gcd(a, b): \n",
" if a == 0 : \n",
" return b, 0, 1\n",
" \n",
" gcd, x1, y1 = extended_gcd(b%a, a) \n",
" \n",
" x = y1 - (b//a) * x1 \n",
" y = x1 \n",
" \n",
" return gcd, x, y \n",
"\n",
"def chinese_remainder(n, a):\n",
" s = 0\n",
" prod = reduce(int.__mul__, n)\n",
" for n_i, a_i in zip(n, a):\n",
" p = prod // n_i\n",
" s += a_i * extended_gcd(p, n_i)[1] * p\n",
" return s % prod\n",
" \n",
"def solve2():\n",
" nums = plines[1].split(\",\")\n",
" n = []\n",
" a = []\n",
" for i, x in enumerate(nums):\n",
" if x == \"x\":\n",
" continue\n",
" x = int(x)\n",
" n.append(x)\n",
" a.append(x - i)\n",
" return chinese_remainder(n, a)\n",
"\n",
"solve2()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"247086664214628"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve2m=lambda:(G:=(lambda a,b:(b,0,1)if a==0 else((r:=G(b%a,a))[0],r[2]-(b//a)*r[1],r[1])))and(lambda n,a:(q:=__import__(\"math\").prod(n))and sum(y*(p:=q//x)*G(p,x)[1]for x,y in zip(n,a))%q)(*zip(*[(k,k-i)for i,x in enumerate(plines[1].split(\",\"))if\"x\"!=x and(k:=int(x))]))\n",
"\n",
"solve2m()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"36.7 µs ± 356 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n",
"40.2 µs ± 577 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n"
]
}
],
"source": [
"%timeit solve2()\n",
"%timeit solve2m()"
]
}
],
"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
}

23
Python/2020/13.py Normal file
View file

@ -0,0 +1,23 @@
from lib import *
input = read_input(2020, 13)
lines = input.splitlines()
target = int(lines[0])
nums = [int(n) for n in lines[1].split(",") if n != "x"]
t, n = min([((target // n + 1) * n - target, n) for n in nums])
print(t * n)
nums = lines[1].split(",")
n = []
a = []
for i, x in enumerate(nums):
if x == "x":
continue
x = int(x)
n.append(x)
a.append(x - i)
print(chinese_remainder(n, a))

View file

@ -1,228 +0,0 @@
{
"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 = 2020, 14\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": [
"14839536808842"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import re\n",
"\n",
"def solve1():\n",
" mem = {}\n",
" mask = \"X\" * 36\n",
" s = 0\n",
" for line in plines:\n",
" if (match := re.match(r\"^mask = ([01X]+)$\", line)):\n",
" mask = match[1]\n",
" elif (match := re.match(r\"^mem\\[(\\d+)\\] = (\\d+)$\", line)):\n",
" a, v = map(int, match.groups())\n",
" s -= mem.get(a, 0)\n",
" mem[a] = int(\"\".join(x if m==\"X\" else m for m, x in zip(mask, bin(v)[2:].zfill(36))), 2)\n",
" s += mem[a]\n",
" return s\n",
"\n",
"solve1()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"14839536808842"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve1m=lambda r=__import__(\"re\").match,m=\"X\"*36:sum({int(g[1]):int(\"\".join([n,x][n==\"X\"]for n,x in zip(m,f\"{int(g[2]):036b}\")),2)for l in plines if(g:=r(r\"^mem\\[(\\d+)\\] = (\\d+)$\",l))or(m:=l.split()[2])*0}.values())\n",
"\n",
"solve1m()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5.35 ms ± 192 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n",
"5.88 ms ± 139 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit solve1()\n",
"%timeit solve1m()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Puzzle 2"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4215284199669"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def solve2():\n",
" mem = {}\n",
" mask = \"X\" * 36\n",
" s = 0\n",
" for line in plines:\n",
" if (match := re.match(r\"^mask = ([01X]+)$\", line)):\n",
" mask = match[1]\n",
" elif (match := re.match(r\"^mem\\[(\\d+)\\] = (\\d+)$\", line)):\n",
" a, v = map(int, match.groups())\n",
" a = \"\".join(x if m==\"0\" else m for m, x in zip(mask, bin(a)[2:].zfill(36)))\n",
" floating = [i for i in range(36) if a[i]==\"X\"]\n",
" for i in range(1<<len(floating)):\n",
" ad = [*a]\n",
" for j in range(len(floating)):\n",
" ad[floating[j]] = min((1<<j) & i, 1)\n",
" ad = int(\"\".join(map(str, ad)), 2)\n",
" s -= mem.get(ad, 0)\n",
" mem[ad] = v\n",
" s += mem[ad]\n",
" return s\n",
"\n",
"\n",
"solve2()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4215284199669"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve2m=lambda r=__import__(\"re\").match,m=\"X\"*36:sum({((q:={t:str(min(1<<j&i,1))for j,t in enumerate(f)}),)and int(\"\".join(q.get(*i)for i in enumerate(a)),2):int(g[2])for l in plines if(g:=r(r\"^mem\\[(\\d+)\\] = (\\d+)$\",l))and(a:=\"\".join(k[k[0]==\"0\"]for k in zip(m,f\"{int(g[1]):036b}\")),f:=[i for i in range(36)if\"X\"==a[i]])or(m:=l.split()[2])*0 for i in range(1<<len(f))}.values())\n",
"\n",
"solve2m()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"873 ms ± 74.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"1.2 s ± 40.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit solve2()\n",
"%timeit solve2m()"
]
}
],
"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
}

42
Python/2020/14.py Normal file
View file

@ -0,0 +1,42 @@
from lib import *
input = read_input(2020, 14)
lines = input.splitlines()
mem = {}
mask = "X" * 36
s = 0
for line in lines:
if match := re.match(r"^mask = ([01X]+)$", line):
mask = match[1]
elif match := re.match(r"^mem\[(\d+)\] = (\d+)$", line):
a, v = map(int, match.groups())
s -= mem.get(a, 0)
mem[a] = int("".join(x if m == "X" else m for m, x in zip(mask, bin(v)[2:].zfill(36))), 2)
s += mem[a]
print(s)
mem = {}
mask = "X" * 36
s = 0
for line in lines:
if match := re.match(r"^mask = ([01X]+)$", line):
mask = match[1]
elif match := re.match(r"^mem\[(\d+)\] = (\d+)$", line):
a, v = map(int, match.groups())
a = "".join(x if m == "0" else m for m, x in zip(mask, bin(a)[2:].zfill(36)))
floating = [i for i in range(36) if a[i] == "X"]
for i in range(1 << len(floating)):
ad = [*a]
for j in range(len(floating)):
ad[floating[j]] = min((1 << j) & i, 1)
ad = int("".join(map(str, ad)), 2)
s -= mem.get(ad, 0)
mem[ad] = v
s += mem[ad]
print(s)

View file

@ -1,213 +0,0 @@
{
"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 = 2020, 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": [
"1618"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def solve1():\n",
" nums = list(map(int, puzzle.split(\",\")))\n",
" hist = {n: i for i, n in enumerate(nums[:-1])}\n",
" \n",
" n = nums[-1]\n",
" for turn in range(len(nums), 2020):\n",
" o = turn - 1 - hist[n] if n in hist else 0\n",
" hist[n] = turn - 1\n",
" n = o\n",
" return n\n",
"\n",
"solve1()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1618"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve1m=lambda N=[*map(int,puzzle.split(\",\"))]:(H:=dict(zip(N,range(len(N)-1))))and(n:=N[-1],)and[0 for t in range(len(N),2020)if(o:=t-1-H.get(n,t-1),H.update({n:t-1}),n:=o)*0]or n\n",
"\n",
"solve1m()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"498 µs ± 19.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n",
"1.17 ms ± 24.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%timeit solve1()\n",
"%timeit solve1m()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Puzzle 2"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 18.3 s, sys: 1.02 s, total: 19.4 s\n",
"Wall time: 20.2 s\n"
]
},
{
"data": {
"text/plain": [
"548531"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%time\n",
"\n",
"def solve2():\n",
" nums = list(map(int, puzzle.split(\",\")))\n",
" hist = {n: i for i, n in enumerate(nums[:-1])}\n",
" \n",
" n = nums[-1]\n",
" for turn in range(len(nums), 30000000):\n",
" o = turn - 1 - hist[n] if n in hist else 0\n",
" hist[n] = turn - 1\n",
" n = o\n",
" return n\n",
"\n",
"solve2()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 27.1 s, sys: 687 ms, total: 27.7 s\n",
"Wall time: 28.3 s\n"
]
},
{
"data": {
"text/plain": [
"548531"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%time\n",
"\n",
"solve2m=lambda N=[*map(int,puzzle.split(\",\"))]:(H:=dict(zip(N,range(len(N)-1))))and(n:=N[-1],)and[0 for t in range(len(N),30000000)if(o:=t-1-H.get(n,t-1),H.update({n:t-1}),n:=o)*0]or n\n",
"\n",
"solve2m()"
]
}
],
"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
}

24
Python/2020/15.py Normal file
View file

@ -0,0 +1,24 @@
from lib import *
input = read_input(2020, 15)
nums = ints(input)
hist = {n: i for i, n in enumerate(nums[:-1])}
n = nums[-1]
for turn in range(len(nums), 2020):
o = turn - 1 - hist[n] if n in hist else 0
hist[n] = turn - 1
n = o
print(n)
hist = {n: i for i, n in enumerate(nums[:-1])}
n = nums[-1]
for turn in range(len(nums), 30000000):
o = turn - 1 - hist[n] if n in hist else 0
hist[n] = turn - 1
n = o
print(n)

View file

@ -1,243 +0,0 @@
{
"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 = 2020, 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": [
"25788"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def valid(rule, num):\n",
" ranges = rule.split(\": \")[1].split(\" or \")\n",
" for r in ranges:\n",
" x, y = map(int, r.split(\"-\"))\n",
" if x <= num <= y:\n",
" return True\n",
" return False\n",
"\n",
"def solve1():\n",
" rules, mt, nt = map(str.splitlines,puzzle.split(\"\\n\\n\"))\n",
" out = 0\n",
" nt.pop(0)\n",
" for ticket in nt:\n",
" for f in map(int, ticket.split(\",\")):\n",
" for r in rules:\n",
" if valid(r, f):\n",
" break\n",
" else:\n",
" out += f\n",
" return out\n",
"\n",
"solve1()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"25788"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve1m=lambda I=[*map(str.splitlines,puzzle.split(\"\\n\\n\"))]:sum(f*(1-any(x[0]<=f<=x[1]for r in I[0]for s in r.split(\": \")[1].split(\"or\")if(x:=[*map(int,s.split(\"-\"))])))for t in I[2][1:]for f in map(int,t.split(\",\")))\n",
"\n",
"solve1m()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"9.3 ms ± 324 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n",
"12.6 ms ± 561 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit solve1()\n",
"%timeit solve1m()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Puzzle 2"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3902565915559"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def solve2():\n",
" rules, mt, nt = map(str.splitlines, puzzle.split(\"\\n\\n\"))\n",
" mt = list(map(int, mt[1].split(\",\")))\n",
" nt = [list(map(int, ticket.split(\",\"))) for ticket in nt[1:]]\n",
" \n",
" nt = [ticket for ticket in nt if all(any(valid(r, f) for r in rules) for f in ticket)]\n",
" \n",
" possible_allocations = [set(range(len(rules))) for i in range(len(rules))]\n",
" for ticket in nt:\n",
" for field in range(len(rules)):\n",
" for rule in range(len(rules)):\n",
" if not valid(rules[rule], ticket[field]):\n",
" possible_allocations[rule].remove(field)\n",
"\n",
" allocations = [None] * len(rules)\n",
" while any(x is None for x in allocations):\n",
" for i in range(len(rules)):\n",
" if len(possible_allocations[i]) == 1:\n",
" j = possible_allocations[i].pop()\n",
" allocations[i] = j\n",
" for k in range(len(rules)):\n",
" if j in possible_allocations[k]:\n",
" possible_allocations[k].remove(j)\n",
"\n",
" out = 1\n",
" for i, rule in enumerate(rules):\n",
" if not rule.startswith(\"departure\"):\n",
" continue\n",
" j = allocations[i]\n",
" out *= mt[j]\n",
" return out\n",
"\n",
"solve2()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3902565915559"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve2m=lambda I=[*map(str.splitlines,puzzle.split(\"\\n\\n\"))]:(n:=len(I[0]),Z:=set(range(n)),V:=lambda R,n:any(k[0]<=n<=k[1]for r in R.split(\":\")[1].split(\"or\")if(k:=[*map(int,r.split(\"-\"))])),X:=lambda x:[*map(int,x.split(\",\"))],M:=X(I[1][1]),w:=[{*Z}for _ in I[0]],W:=[None]*n,[w[r].remove(f)for t in map(X,I[2][1:])if all(any(V(r,f)for r in I[0])for f in t)for f in Z for r in Z if not V(I[0][r],t[f])],[(W.__setitem__(i,j:=w[i].pop()),[w[k].remove(j)for k in Z if j in w[k]])for _ in I[0]for i in Z if len(w[i])==1],__import__(\"math\").prod(M[W[i]]for i,R in enumerate(I[0])if\"departure\"==R[:9]))[9]\n",
"\n",
"solve2m()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"142 ms ± 10.6 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n",
"217 ms ± 15.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit solve2()\n",
"%timeit solve2m()"
]
}
],
"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
}

60
Python/2020/16.py Normal file
View file

@ -0,0 +1,60 @@
from lib import *
input = read_input(2020, 16)
def valid(rule, num):
ranges = rule.split(": ")[1].split(" or ")
for r in ranges:
x, y = map(int, r.split("-"))
if x <= num <= y:
return True
return False
rules, mt, nt = map(str.splitlines, input.split("\n\n"))
out = 0
nt.pop(0)
for ticket in nt:
for f in map(int, ticket.split(",")):
for r in rules:
if valid(r, f):
break
else:
out += f
print(out)
rules, mt, nt = map(str.splitlines, input.split("\n\n"))
mt = list(map(int, mt[1].split(",")))
nt = [list(map(int, ticket.split(","))) for ticket in nt[1:]]
nt = [ticket for ticket in nt if all(any(valid(r, f) for r in rules) for f in ticket)]
possible_allocations = [set(range(len(rules))) for i in range(len(rules))]
for ticket in nt:
for field in range(len(rules)):
for rule in range(len(rules)):
if not valid(rules[rule], ticket[field]):
possible_allocations[rule].remove(field)
allocations = [None] * len(rules)
while any(x is None for x in allocations):
for i in range(len(rules)):
if len(possible_allocations[i]) == 1:
j = possible_allocations[i].pop()
allocations[i] = j
for k in range(len(rules)):
if j in possible_allocations[k]:
possible_allocations[k].remove(j)
out = 1
for i, rule in enumerate(rules):
if not rule.startswith("departure"):
continue
j = allocations[i]
out *= mt[j]
print(out)

View file

@ -1,228 +0,0 @@
{
"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 = 2020, 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": [
"289"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"neigh = [(i, j, k) for i in [-1,0,1] for j in [-1,0,1] for k in [-1,0,1]]\n",
"\n",
"def solve1():\n",
" state = {(0, i, j) for i, line in enumerate(plines) for j, c in enumerate(line) if c == \"#\"}\n",
" for _ in range(6):\n",
" to_update = {(x+i, y+j, z+k) for (x, y, z) in state for (i,j,k) in neigh}\n",
" new_state = set()\n",
" for (x, y, z) in to_update:\n",
" active = (x, y, z) in state\n",
" cnt = sum((x+i, y+j, z+k) in state for (i, j, k) in neigh if i or j or k)\n",
" if active and cnt not in (2, 3):\n",
" active = False\n",
" elif not active and cnt == 3:\n",
" active = True\n",
" if active:\n",
" new_state.add((x, y, z))\n",
" state = new_state\n",
" return len(state)\n",
"\n",
"solve1()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"289"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve1m=lambda N=[(i,j,k)for i in[-1,0,1]for j in [-1,0,1]for k in[-1,0,1]]:(S:={(0,i,j)for i,l in enumerate(plines)for j,c in enumerate(l)if\"#\"==c},[(S:={(x,y,z)for(x,y,z)in{(x+i,y+j,z+k)for(x,y,z)in S for(i,j,k)in N}if(c:=sum((x+i,y+j,z+k)in S for(i,j,k)in N if i or j or k))in(2,3)*(a:=(x,y,z)in S)or(1-a,c)==(1,3)})for _ in\".\"*6])and len(S)\n",
"\n",
"solve1m()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"68.6 ms ± 10.8 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n",
"63.5 ms ± 1.82 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
]
}
],
"source": [
"%timeit solve1()\n",
"%timeit solve1m()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Puzzle 2"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2084"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"neigh = [(i, j, k, l) for i in [-1,0,1] for j in [-1,0,1] for k in [-1,0,1] for l in [-1,0,1]]\n",
"\n",
"def solve2():\n",
" state = {(0, 0, i, j) for i, line in enumerate(plines) for j, c in enumerate(line) if c == \"#\"}\n",
" for _ in range(6):\n",
" to_update = {(w+h, x+i, y+j, z+k) for (w, x, y, z) in state for (h,i,j,k) in neigh}\n",
" new_state = set()\n",
" for (w, x, y, z) in to_update:\n",
" active = (w, x, y, z) in state\n",
" cnt = sum((w+h, x+i, y+j, z+k) in state for (h, i, j, k) in neigh if h or i or j or k)\n",
" if active and cnt not in (2, 3):\n",
" active = False\n",
" elif not active and cnt == 3:\n",
" active = True\n",
" if active:\n",
" new_state.add((w, x, y, z))\n",
" state = new_state\n",
" return len(state)\n",
"\n",
"solve2()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2084"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve2m=lambda N=[(i,j,k,l)for i in[-1,0,1]for j in [-1,0,1]for k in[-1,0,1]for l in[-1,0,1]]:(S:={(0,0,i,j)for i,l in enumerate(plines)for j,c in enumerate(l)if\"#\"==c},[(S:={(w,x,y,z)for(w,x,y,z)in{(w+h,x+i,y+j,z+k)for(w,x,y,z)in S for(h,i,j,k)in N}if(c:=sum((w+h,x+i,y+j,z+k)in S for(h,i,j,k)in N if any([h,i,j,k])))in(2,3)*(a:=(w,x,y,z)in S)or(1-a,c)==(1,3)})for _ in\".\"*6])and len(S)\n",
"\n",
"solve2m()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2.06 s ± 61.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"2.65 s ± 19.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit solve2()\n",
"%timeit solve2m()"
]
}
],
"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
}

46
Python/2020/17.py Normal file
View file

@ -0,0 +1,46 @@
from lib import *
input = read_input(2020, 17)
lines = input.splitlines()
neigh = [(i, j, k) for i in [-1, 0, 1] for j in [-1, 0, 1] for k in [-1, 0, 1]]
state = {(0, i, j) for i, line in enumerate(lines) for j, c in enumerate(line) if c == "#"}
for _ in range(6):
to_update = {(x + i, y + j, z + k) for (x, y, z) in state for (i, j, k) in neigh}
new_state = set()
for x, y, z in to_update:
active = (x, y, z) in state
cnt = sum((x + i, y + j, z + k) in state for (i, j, k) in neigh if i or j or k)
if active and cnt not in (2, 3):
active = False
elif not active and cnt == 3:
active = True
if active:
new_state.add((x, y, z))
state = new_state
print(len(state))
neigh = [(i, j, k, l) for i in [-1, 0, 1] for j in [-1, 0, 1] for k in [-1, 0, 1] for l in [-1, 0, 1]]
state = {(0, 0, i, j) for i, line in enumerate(lines) for j, c in enumerate(line) if c == "#"}
for _ in range(6):
to_update = {(w + h, x + i, y + j, z + k) for (w, x, y, z) in state for (h, i, j, k) in neigh}
new_state = set()
for w, x, y, z in to_update:
active = (w, x, y, z) in state
cnt = sum((w + h, x + i, y + j, z + k) in state for (h, i, j, k) in neigh if h or i or j or k)
if active and cnt not in (2, 3):
active = False
elif not active and cnt == 3:
active = True
if active:
new_state.add((w, x, y, z))
state = new_state
print(len(state))

View file

@ -1,259 +0,0 @@
{
"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 = 2020, 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": [
"1408133923393"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def evaluate(expr):\n",
" new_expr = \"\"\n",
" cnt = 0\n",
" buffer = \"\"\n",
" for c in expr.replace(\" \", \"\"):\n",
" if c == \"(\":\n",
" if cnt:\n",
" buffer += c\n",
" cnt += 1\n",
" elif c == \")\":\n",
" cnt -= 1\n",
" if cnt:\n",
" buffer += c\n",
" else:\n",
" new_expr += str(evaluate(buffer))\n",
" buffer = \"\"\n",
" elif cnt:\n",
" buffer += c\n",
" else:\n",
" new_expr += c\n",
" \n",
" out = 0\n",
" buffer = 0\n",
" op = False\n",
" for c in new_expr + \" \":\n",
" if c.isnumeric():\n",
" buffer = buffer * 10 + int(c)\n",
" continue\n",
" \n",
" if op: out *= buffer\n",
" else: out += buffer\n",
" buffer = 0\n",
" op = c == \"*\"\n",
" \n",
" return out\n",
" \n",
"\n",
"def solve1():\n",
" return sum(map(evaluate, plines))\n",
"\n",
"solve1()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1408133923393"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve1m=lambda:sum(map((E:=(lambda e:(n:=\"\",x:=0,b:=\"\",[(k:=1,c==\"(\"and(x and(b:=b+c),x:=x+1,k:=0),c==\")\"and(x:=x-1,(b:=b+c)if x else (n:=n+str(E(b)),b:=\"\"),k:=0),k*x and(b:=b+c,k:=0),k and(n:=n+c))for c in e.replace(\" \",\"\")],o:=0,b:=0,x:=False,[(o:=[o+b,o*b][x],b:=0,x:=c==\"*\")for c in n+\" \"if not(c.isnumeric()and(b:=b*10+int(c),))],o)[-1])),plines))\n",
"\n",
"solve1m()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"7.74 ms ± 443 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n",
"14.3 ms ± 1.04 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit solve1()\n",
"%timeit solve1m()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Puzzle 2"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"314455761823725"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import math\n",
"\n",
"def evaluate(expr):\n",
" new_expr = \"\"\n",
" cnt = 0\n",
" buffer = \"\"\n",
" for c in expr.replace(\" \", \"\"):\n",
" if c == \"(\":\n",
" if cnt:\n",
" buffer += c\n",
" cnt += 1\n",
" elif c == \")\":\n",
" cnt -= 1\n",
" if cnt:\n",
" buffer += c\n",
" else:\n",
" new_expr += str(evaluate(buffer))\n",
" buffer = \"\"\n",
" elif cnt:\n",
" buffer += c\n",
" else:\n",
" new_expr += c\n",
" \n",
" return math.prod(sum(map(int, x.split(\"+\"))) for x in new_expr.split(\"*\"))\n",
" \n",
"\n",
"def solve2():\n",
" return sum(map(evaluate, plines))\n",
"\n",
"solve2()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"314455761823725"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve2m=lambda p=__import__(\"math\").prod:sum(map((E:=(lambda e:(n:=\"\",x:=0,b:=\"\",[(k:=1,c==\"(\"and(x and(b:=b+c),x:=x+1,k:=0),c==\")\"and(x:=x-1,(b:=b+c)if x else(n:=n+str(E(b)),b:=\"\"),k:=0),k*x and(b:=b+c,k:=0),k and(n:=n+c))for c in e.replace(\" \",\"\")],p(sum(map(int,x.split(\"+\")))for x in n.split(\"*\")))[-1])),plines))\n",
"\n",
"solve2m()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"7.48 ms ± 226 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n",
"12.4 ms ± 366 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit solve2()\n",
"%timeit solve2m()"
]
}
],
"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
}

71
Python/2020/18.py Normal file
View file

@ -0,0 +1,71 @@
from lib import *
input = read_input(2020, 18)
lines = input.splitlines()
def evaluate1(expr):
new_expr = ""
cnt = 0
buffer = ""
for c in expr.replace(" ", ""):
if c == "(":
if cnt:
buffer += c
cnt += 1
elif c == ")":
cnt -= 1
if cnt:
buffer += c
else:
new_expr += str(evaluate1(buffer))
buffer = ""
elif cnt:
buffer += c
else:
new_expr += c
out = 0
buffer = 0
op = False
for c in new_expr + " ":
if c.isnumeric():
buffer = buffer * 10 + int(c)
continue
if op:
out *= buffer
else:
out += buffer
buffer = 0
op = c == "*"
return out
print(sum(map(evaluate1, lines)))
def evaluate2(expr):
new_expr = ""
cnt = 0
buffer = ""
for c in expr.replace(" ", ""):
if c == "(":
if cnt:
buffer += c
cnt += 1
elif c == ")":
cnt -= 1
if cnt:
buffer += c
else:
new_expr += str(evaluate2(buffer))
buffer = ""
elif cnt:
buffer += c
else:
new_expr += c
return math.prod(sum(map(int, x.split("+"))) for x in new_expr.split("*"))
print(sum(map(evaluate2, lines)))

View file

@ -1,345 +0,0 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"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 = 2020, 19\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": [
"160"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import re\n",
"\n",
"def solve1():\n",
" raw_rules, messages = map(str.splitlines, puzzle.split(\"\\n\\n\"))\n",
" raw_rules = dict(line.split(\": \") for line in raw_rules)\n",
" rules = {}\n",
" \n",
" def make_rule(idx):\n",
" if idx in rules:\n",
" return rules[idx]\n",
" \n",
" out = []\n",
" for sub_rules in raw_rules[idx].split(\" | \"):\n",
" o = \"\"\n",
" for sr in sub_rules.split():\n",
" if x := re.match(r'\"(.+)\"', sr):\n",
" o += x[1]\n",
" else:\n",
" o += make_rule(sr)\n",
" out.append(o)\n",
" rules[idx] = \"(\" + \"|\".join(out) + \")\"\n",
" return rules[idx]\n",
" \n",
" pattern = re.compile(f\"^{make_rule('0')}$\")\n",
" return sum(bool(pattern.match(msg)) for msg in messages)\n",
"\n",
"solve1()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"160"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def solve1x():\n",
" rules, messages = map(str.splitlines, puzzle.split(\"\\n\\n\"))\n",
" rules = dict(line.split(\": \") for line in rules)\n",
" \n",
" def match_pattern(pattern, string):\n",
" if not pattern:\n",
" yield 0\n",
" return\n",
" \n",
" p, *pattern = pattern\n",
" \n",
" for j in [len(p)-2] * (p[1:-1] == string[:len(p)-2]) if p[0] == '\"' else match(p, string):\n",
" for k in match_pattern(pattern, string[j:]):\n",
" yield j + k\n",
" \n",
" def match(idx, string):\n",
" if not string:\n",
" return []\n",
" out = set()\n",
" for rule in rules[idx].split(\" | \"):\n",
" out.update(match_pattern(rule.split(), string))\n",
" return out\n",
" \n",
" return sum(len(msg) in match(\"0\", msg) for msg in messages)\n",
"\n",
"\n",
"solve1x()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"160"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve1m=lambda:(X:=[*map(str.splitlines,puzzle.split(\"\\n\\n\"))],R:=dict(l.split(\": \")for l in X[0]),m:=lambda p,s:[j+k for j in([len(p[0])-2]*(p[0][1:-1]==s[:len(p[0])-2])if p[0][0]=='\"'else M(p[0],s))for k in m(p[1:],s[j:])]if p else[0],M:=lambda i,s:set().union(*(m(r.split(),s)for r in R[i].split(\" | \")))if s else[])and sum(len(m)in M(\"0\",m)for m in X[1])\n",
"\n",
"solve1m()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"9.39 ms ± 915 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n",
"215 ms ± 19 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"304 ms ± 14.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit solve1()\n",
"%timeit solve1x()\n",
"%timeit solve1m()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Puzzle 2"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"357"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def solve2():\n",
" raw_rules, messages = map(str.splitlines, puzzle.split(\"\\n\\n\"))\n",
" raw_rules = dict(line.split(\": \") for line in raw_rules)\n",
" rules = {}\n",
" \n",
" def make_rule(idx):\n",
" if idx in rules:\n",
" return rules[idx]\n",
"\n",
" out = []\n",
" if idx == \"8\":\n",
" out.append(make_rule(\"42\") + \"+\")\n",
" elif idx == \"11\":\n",
" for k in range(1, 10):\n",
" out.append(make_rule(\"42\") * k + make_rule(\"31\") * k)\n",
" else:\n",
" for sub_rules in raw_rules[idx].split(\" | \"):\n",
" o = \"\"\n",
" for sr in sub_rules.split():\n",
" if x := re.match(r'\"(.+)\"', sr):\n",
" o += x[1]\n",
" else:\n",
" o += make_rule(sr)\n",
" out.append(o)\n",
" rules[idx] = \"(\" + \"|\".join(out) + \")\"\n",
" return rules[idx]\n",
" \n",
" pattern = re.compile(f\"^{make_rule('0')}$\")\n",
" return sum(bool(pattern.match(msg)) for msg in messages)\n",
"\n",
"solve2()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"357"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def solve2x():\n",
" rules, messages = map(str.splitlines, puzzle.split(\"\\n\\n\"))\n",
" rules = dict(line.split(\": \") for line in rules)\n",
" \n",
" rules[\"8\"] = \"42 | 42 8\"\n",
" rules[\"11\"] = \"42 31 | 42 11 31\"\n",
" \n",
" def match_pattern(pattern, string):\n",
" if not pattern:\n",
" yield 0\n",
" return\n",
" \n",
" p, *pattern = pattern\n",
" \n",
" for j in [len(p)-2] * (p[1:-1] == string[:len(p)-2]) if p[0] == '\"' else match(p, string):\n",
" for k in match_pattern(pattern, string[j:]):\n",
" yield j + k\n",
" \n",
" def match(idx, string):\n",
" if not string:\n",
" return []\n",
" out = set()\n",
" for rule in rules[idx].split(\" | \"):\n",
" out.update(match_pattern(rule.split(), string))\n",
" return sorted(out)\n",
" \n",
" return sum(len(msg) in match(\"0\", msg) for msg in messages)\n",
"\n",
"\n",
"solve2x()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"357"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve2m=lambda:(X:=[*map(str.splitlines,puzzle.split(\"\\n\\n\"))],R:={**dict(l.split(\": \")for l in X[0]),\"8\":\"42 | 42 8\",\"11\":\"42 31 | 42 11 31\"},m:=lambda p,s:[j+k for j in([len(p[0])-2]*(p[0][1:-1]==s[:len(p[0])-2])if p[0][0]=='\"'else M(p[0],s))for k in m(p[1:],s[j:])]if p else[0],M:=lambda i,s:set().union(*(m(r.split(),s)for r in R[i].split(\" | \")))if s else[])and sum(len(m)in M(\"0\",m)for m in X[1])\n",
"\n",
"solve2m()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"580 ms ± 14.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"1.88 s ± 147 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"2.44 s ± 104 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit solve2()\n",
"%timeit solve2x()\n",
"%timeit solve2m()"
]
}
],
"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
}

64
Python/2020/19.py Normal file
View file

@ -0,0 +1,64 @@
from lib import *
input = read_input(2020, 19)
raw_rules, messages = map(str.splitlines, input.split("\n\n"))
raw_rules = dict(line.split(": ") for line in raw_rules)
rules = {}
def make_rule1(idx):
if idx in rules:
return rules[idx]
out = []
for sub_rules in raw_rules[idx].split(" | "):
o = ""
for sr in sub_rules.split():
if x := re.match(r'"(.+)"', sr):
o += x[1]
else:
o += make_rule1(sr)
out.append(o)
rules[idx] = "(" + "|".join(out) + ")"
return rules[idx]
pattern = re.compile(f"^{make_rule1('0')}$")
print(sum(bool(pattern.match(msg)) for msg in messages))
raw_rules, messages = map(str.splitlines, input.split("\n\n"))
raw_rules = dict(line.split(": ") for line in raw_rules)
rules = {}
def make_rule2(idx):
if idx in rules:
return rules[idx]
out = []
if idx == "8":
out.append(make_rule2("42") + "+")
elif idx == "11":
for k in range(1, 10):
out.append(make_rule2("42") * k + make_rule2("31") * k)
else:
for sub_rules in raw_rules[idx].split(" | "):
o = ""
for sr in sub_rules.split():
if x := re.match(r'"(.+)"', sr):
o += x[1]
else:
o += make_rule2(sr)
out.append(o)
rules[idx] = "(" + "|".join(out) + ")"
return rules[idx]
pattern = re.compile(f"^{make_rule2('0')}$")
print(sum(bool(pattern.match(msg)) for msg in messages))

View file

@ -1,309 +0,0 @@
{
"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 = 2020, 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": [
"15670959891893"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import math\n",
"\n",
"\n",
"def edge_to_int(edge):\n",
" x1 = int(\"\".join(edge).replace(\".\", \"0\").replace(\"#\", \"1\"), 2)\n",
" x2 = int(\"\".join(reversed(edge)).replace(\".\", \"0\").replace(\"#\", \"1\"), 2)\n",
" return min(x1, x2)\n",
" \n",
"def solve1():\n",
" m = {}\n",
" for num, *lines in map(str.splitlines, puzzle.split(\"\\n\\n\")):\n",
" t = list(zip(*lines))\n",
" num = int(num.split()[1][:-1])\n",
" for e in map(edge_to_int, [lines[0], lines[-1], t[0], t[-1]]):\n",
" m.setdefault(e, []).append(num)\n",
" border = [v[0] for k, v in m.items() if len(v) == 1]\n",
" return math.prod(set([x for x in border if border.count(x) == 2]))\n",
"\n",
"solve1()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"15670959891893"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve1m=lambda E=lambda e:min(int(x:=\"\".join(e).translate({46:48,35:49}),2),int(x[::-1],2)):(m:={})or[m.setdefault(e,[]).append(int(n.split()[1][:-1]))for n,*L in map(str.splitlines,puzzle.split(\"\\n\\n\"))if(t:=list(zip(*L)))for e in map(E,[L[0],L[-1],t[0],t[-1]])]and(b:=[v for v,*x in m.values()if not x])and __import__(\"math\").prod({x for x in b if b.count(x)==2})\n",
"\n",
"solve1m()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2.47 ms ± 171 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n",
"2.1 ms ± 112 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit solve1()\n",
"%timeit solve1m()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Puzzle 2"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1964"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"op = [1, 0, 3, 2]\n",
"monster = \"\"\" # \n",
"# ## ## ###\n",
" # # # # # # \"\"\".splitlines()\n",
"\n",
"def rotflip(tile, o):\n",
" c = (3, 1)\n",
" k = True\n",
" while c != o:\n",
" if k:\n",
" tile = list(zip(*tile))\n",
" c = c[::-1]\n",
" else:\n",
" tile = [line[::-1] for line in tile]\n",
" c = (op[c[0]], c[1])\n",
" k = not k\n",
" return list(map(\"\".join, tile))\n",
"\n",
"\n",
"def solve2():\n",
" m = {}\n",
" tiles = {}\n",
" for num, *lines in map(str.splitlines, puzzle.split(\"\\n\\n\")):\n",
" t = list(zip(*lines))\n",
" num = int(num.split()[1][:-1])\n",
" tiles[num] = [line[1:-1] for line in lines[1:-1]]\n",
" for i, e in enumerate(map(edge_to_int, [lines[0], lines[-1], t[0], t[-1]])):\n",
" m.setdefault(e, []).append((i, num))\n",
" \n",
" G = {}\n",
" for (o1, t1), (o2, t2) in [v for k, v in m.items() if len(v) == 2]:\n",
" G.setdefault(t1, {})[o1] = (o2, t2)\n",
" G.setdefault(t2, {})[o2] = (o1, t1)\n",
" \n",
" corner = next(k for k, v in G.items() if len(v) == 2)\n",
"\n",
" border = {corner}\n",
" dim = []\n",
" for k in G[corner]:\n",
" c = corner\n",
" x = 1\n",
" while k in G[c]:\n",
" k, c = G[c][k]\n",
" k = op[k]\n",
" border.add(c)\n",
" x += 1\n",
" dim.append(x)\n",
" width, height = dim\n",
" \n",
" for k, v in G.items():\n",
" if k in border:\n",
" continue\n",
" for i in range(4):\n",
" v.setdefault(i, None)\n",
" \n",
" tiled_image = [[None] * width for _ in range(height)]\n",
" \n",
" queue = [(corner, 0, 0, *G[corner])]\n",
" visited = set()\n",
" while queue:\n",
" p, i, j, a, b = queue.pop(0)\n",
" \n",
" if p in visited: continue\n",
" visited.add(p)\n",
" \n",
" tiled_image[i][j] = rotflip(tiles[p], (a, b))\n",
" \n",
" for o1, u, v in [(a, i, j+1), (b, i+1, j)]:\n",
" if G[p].get(o1) is None:\n",
" continue\n",
" o2, q = G[p][o1]\n",
" if len(G[q]) >= 3:\n",
" G[q].pop(o2)\n",
"\n",
" if len(G[q]) <= 2:\n",
" if o1 == a:\n",
" x = op[o2]\n",
" y, = set(G[q]) - {x, o2}\n",
" else:\n",
" y = op[o2]\n",
" x, = set(G[q]) - {y, o2}\n",
" queue.append((q, u, v, x, y))\n",
"\n",
" image = []\n",
" for tile_line in tiled_image:\n",
" image += [\"\".join(line) for line in zip(*tile_line)]\n",
" \n",
" monster_parts = set()\n",
" for i in range(4):\n",
" for j in range(4):\n",
" if i in (j, op[j]):\n",
" continue\n",
" m = rotflip(monster, (i, j))\n",
" for y in range(len(image) - len(m) + 1):\n",
" for x in range(len(image[0]) - len(m[0]) + 1):\n",
" p = [(y+i, x+j) for i, line in enumerate(m) for j, c in enumerate(line) if c == \"#\"]\n",
" if all(image[i][j] == \"#\" for i, j in p):\n",
" monster_parts.update(p)\n",
" return sum(line.count(\"#\") for line in image) - len(monster_parts)\n",
"\n",
"\n",
"solve2()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1964"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve2m=lambda z=enumerate,Y=range,O=[1,0,3,2],E=lambda e:min(int(x:=\"\".join(e).translate({46:48,35:49}),2),int(x[::-1],2)):(R:=lambda t,o,c=(3,1):c==o and[*map(\"\".join,t)]or[3,2,0,1][c[0]]==c[1]and R([*zip(*t)],o,c[::-1])or R([l[::-1]for l in t],o,(O[c[0]],c[1])),m:={},P:={},[m.setdefault(e,[]).append((i,n))for N,*l in map(str.splitlines,puzzle.split(\"\\n\\n\"))if(t:=list(zip(*l)),n:=int(N.split()[1][:-1]),P.update({n:[L[1:-1]for L in l[1:-1]]}))for i,e in z(map(E,[l[0],l[-1],t[0],t[-1]]))],G:={},[(G.setdefault(t1,{}).update({o1:(o2,t2)}),G.setdefault(t2,{}).update({o2:(o1,t1)}))for(o1,t1),(o2,t2)in[v for k,v in m.items()if len(v)==2]],B:={(C:=next(k for k,v in G.items()if len(v)==2))},D:=[(U:=(lambda k,c:B.add(c)or(1+U(O[G[c][k][0]],G[c][k][1])if k in G[c]else 1)))(k,C)for k in G[C]],[v.setdefault(i,None)for k, v in G.items()if k not in B for i in Y(4)],T:=[[None]*D[0]for _ in Y(D[1])],Q:=[(C,0,0,*G[C])],V:=set(),next(0 for()in iter(tuple,0)if not Q or(p:=Q.pop(0),p[0]not in V and(V.add(p[0]),T[p[1]].__setitem__(p[2],R(P[p[0]],p[3:5])),[(3<=len(G[f[1]])and G[f[1]].pop(f[0]),2>=len(G[f[1]])and(A==p[3]and(x:=O[f[0]],y:=[*(set(G[f[1]])-{x,f[0]})][0])or(y:=O[f[0]],x:=[*(set(G[f[1]])-{y,f[0]})][0]),Q.append((f[1],u,v,x,y))))for A,u,v in[(p[3],p[1],p[2]+1),(p[4],p[1]+1,p[2])]if(f:=G[p[0]].get(A))]))*0),I:=[\"\".join(l)for t in T for l in zip(*t)])and sum(l.count(\"#\")for l in I)-len({s for i in Y(4)for j in Y(4)if i not in(j,O[j])and(m:=R([18*\" \"+\"# \",3*\"# #\"+\"##\",6*\" # \"+\" \"],(i,j)))for y in Y(len(I)-len(m)+1)for x in Y(len(I[0])-len(m[0])+1)if(p:=[(y+i,x+j)for i,l in z(m)for j,c in z(l)if\"#\"==c])if all(I[i][j]==\"#\"for i,j in p)for s in p})\n",
"\n",
"solve2m()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"643 ms ± 23.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"613 ms ± 12 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit solve2()\n",
"%timeit solve2m()"
]
}
],
"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
}

122
Python/2020/20.py Normal file
View file

@ -0,0 +1,122 @@
from lib import *
input = read_input(2020, 20)
def edge_to_int(edge):
x1 = int("".join(edge).replace(".", "0").replace("#", "1"), 2)
x2 = int("".join(reversed(edge)).replace(".", "0").replace("#", "1"), 2)
return min(x1, x2)
m = {}
for num, *lines in map(str.splitlines, input.strip().split("\n\n")):
t = list(zip(*lines))
num = int(num.split()[1][:-1])
for e in map(edge_to_int, [lines[0], lines[-1], t[0], t[-1]]):
m.setdefault(e, []).append(num)
border = [v[0] for k, v in m.items() if len(v) == 1]
print(math.prod(set([x for x in border if border.count(x) == 2])))
op = [1, 0, 3, 2]
monster = """ #
# ## ## ###
# # # # # # """.splitlines()
def rotflip(tile, o):
c = (3, 1)
k = True
while c != o:
if k:
tile = list(zip(*tile))
c = c[::-1]
else:
tile = [line[::-1] for line in tile]
c = (op[c[0]], c[1])
k = not k
return list(map("".join, tile))
m = {}
tiles = {}
for num, *lines in map(str.splitlines, input.strip().split("\n\n")):
t = list(zip(*lines))
num = int(num.split()[1][:-1])
tiles[num] = [line[1:-1] for line in lines[1:-1]]
for i, e in enumerate(map(edge_to_int, [lines[0], lines[-1], t[0], t[-1]])):
m.setdefault(e, []).append((i, num))
G = {}
for (o1, t1), (o2, t2) in [v for k, v in m.items() if len(v) == 2]:
G.setdefault(t1, {})[o1] = (o2, t2)
G.setdefault(t2, {})[o2] = (o1, t1)
corner = next(k for k, v in G.items() if len(v) == 2)
border = {corner}
dim = []
for k in G[corner]:
c = corner
x = 1
while k in G[c]:
k, c = G[c][k]
k = op[k]
border.add(c)
x += 1
dim.append(x)
width, height = dim
for k, v in G.items():
if k in border:
continue
for i in range(4):
v.setdefault(i, None)
tiled_image = [[None] * width for _ in range(height)]
queue = [(corner, 0, 0, *G[corner])]
visited = set()
while queue:
p, i, j, a, b = queue.pop(0)
if p in visited:
continue
visited.add(p)
tiled_image[i][j] = rotflip(tiles[p], (a, b))
for o1, u, v in [(a, i, j + 1), (b, i + 1, j)]:
if G[p].get(o1) is None:
continue
o2, q = G[p][o1]
if len(G[q]) >= 3:
G[q].pop(o2)
if len(G[q]) <= 2:
if o1 == a:
x = op[o2]
(y,) = set(G[q]) - {x, o2}
else:
y = op[o2]
(x,) = set(G[q]) - {y, o2}
queue.append((q, u, v, x, y))
image = []
for tile_line in tiled_image:
image += ["".join(line) for line in zip(*tile_line)]
monster_parts = set()
for i in range(4):
for j in range(4):
if i in (j, op[j]):
continue
m = rotflip(monster, (i, j))
for y in range(len(image) - len(m) + 1):
for x in range(len(image[0]) - len(m[0]) + 1):
p = [(y + i, x + j) for i, line in enumerate(m) for j, c in enumerate(line) if c == "#"]
if all(image[i][j] == "#" for i, j in p):
monster_parts.update(p)
print(sum(line.count("#") for line in image) - len(monster_parts))

View file

@ -1,243 +0,0 @@
{
"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 = 2020, 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": [
"2307"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from collections import Counter\n",
"\n",
"def solve1():\n",
" allergens = {}\n",
" ing = set()\n",
" cnt = Counter()\n",
" \n",
" for line in plines:\n",
" i, a = line.split(\" (contains \")\n",
" i = i.split()\n",
" \n",
" ing.update(i)\n",
" cnt.update(i)\n",
" \n",
" for x in a.strip(\")\").split(\", \"):\n",
" if x not in allergens:\n",
" allergens[x] = set(i)\n",
" else:\n",
" allergens[x] &= set(i)\n",
" \n",
" for k in allergens.values():\n",
" ing -= k\n",
" \n",
" return sum(cnt[x] for x in ing)\n",
" \n",
" \n",
"solve1()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2307"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve1m=lambda:(a:={},i:=[],[a.setdefault(x,[]).append(j)for l in plines if(k:=l.split(\" (contains \"),i.extend(j:=k[0].split()))for x in k[1].strip(\")\").split(\", \")],a:={0}.union(*[set(k[0]).intersection(*k)for k in a.values()]),sum(1-(x in a)for x in i))[4]\n",
"\n",
"solve1m()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.04 ms ± 26 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n",
"868 µs ± 17.6 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%timeit solve1()\n",
"%timeit solve1m()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Puzzle 2"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'cljf,frtfg,vvfjj,qmrps,hvnkk,qnvx,cpxmpc,qsjszn'"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def solve2():\n",
" allergens = {}\n",
" \n",
" for line in plines:\n",
" i, a = line.split(\" (contains \")\n",
" i = i.split()\n",
" \n",
" for x in a.strip(\")\").split(\", \"):\n",
" if x not in allergens:\n",
" allergens[x] = set(i)\n",
" else:\n",
" allergens[x] &= set(i)\n",
" \n",
" found = {}\n",
" while allergens:\n",
" for k, v in [*allergens.items()]:\n",
" if len(v) != 1:\n",
" continue\n",
" found[k], = v\n",
" for t in allergens.values():\n",
" if found[k] in t:\n",
" t.remove(found[k])\n",
" allergens.pop(k)\n",
" \n",
" return \",\".join(b for a, b in sorted(found.items()))\n",
" \n",
" \n",
"solve2()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'cljf,frtfg,vvfjj,qmrps,hvnkk,qnvx,cpxmpc,qsjszn'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve2m=lambda:(a:={},[a.setdefault(x,[]).append(k[0].split())for l in plines if(k:=l.split(\" (contains \"))for x in k[1].strip(\")\").split(\", \")],a:={k:{*v[0]}.intersection(*v)for k,v in a.items()},f:={},next([]for()in iter(list,0)if not a or([t.remove(g)for k,v in[*a.items()]if 1==len(v)and(f.setdefault(k,g:=[*v][0]),a.pop(k))for t in a.values()if g in t])*0))and\",\".join(x[1]for x in sorted(f.items()))\n",
"\n",
"solve2m()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"734 µs ± 28 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n",
"793 µs ± 25.4 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%timeit solve2()\n",
"%timeit solve2m()"
]
}
],
"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
}

49
Python/2020/21.py Normal file
View file

@ -0,0 +1,49 @@
from lib import *
input = read_input(2020, 21)
lines = input.splitlines()
allergens = {}
ing = set()
cnt = Counter()
for line in lines:
i, a = line.split(" (contains ")
i = i.split()
ing.update(i)
cnt.update(i)
for x in a.strip(")").split(", "):
if x not in allergens:
allergens[x] = set(i)
else:
allergens[x] &= set(i)
for k in allergens.values():
ing -= k
print(sum(cnt[x] for x in ing))
allergens = {}
for line in lines:
i, a = line.split(" (contains ")
i = i.split()
for x in a.strip(")").split(", "):
if x not in allergens:
allergens[x] = set(i)
else:
allergens[x] &= set(i)
found = {}
while allergens:
for k, v in [*allergens.items()]:
if len(v) != 1:
continue
(found[k],) = v
for t in allergens.values():
if found[k] in t:
t.remove(found[k])
allergens.pop(k)
print(",".join(b for a, b in sorted(found.items())))

View file

@ -1,232 +0,0 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"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 = 2020, 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": [
"32199"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def solve1():\n",
" p1, p2 = [[*map(int,x.splitlines()[1:])] for x in puzzle.split(\"\\n\\n\")]\n",
" while p1 and p2:\n",
" if p1[0] > p2[0]:\n",
" p1.append(p1.pop(0))\n",
" p1.append(p2.pop(0))\n",
" else:\n",
" p2.append(p2.pop(0))\n",
" p2.append(p1.pop(0))\n",
" return sum((i+1)*x for i, x in enumerate(reversed(p1 or p2)))\n",
"\n",
"solve1()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"32199"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve1m=lambda:(p:=[[*map(int,x.split()[2:])]for x in puzzle.split(\"\\n\\n\")],next(0 for()in iter(list,0)if not(p[0]and p[1])or p[(x:=(k:=[p[0].pop(0),p[1].pop(0)])[0]<k[1])].extend([k[x],k[1-x]])))and sum(x*(i+1)for i,x in enumerate((p[0]+p[1])[::-1]))\n",
"\n",
"solve1m()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"131 µs ± 1.99 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n",
"226 µs ± 8.06 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%timeit solve1()\n",
"%timeit solve1m()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Puzzle 2"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"33780"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def combat(p1, p2):\n",
" seen = set()\n",
" \n",
" while p1 and p2:\n",
" if (k := (tuple(p1), tuple(p2))) in seen:\n",
" return 0, None\n",
" seen.add(k)\n",
"\n",
" c1 = p1.pop(0)\n",
" c2 = p2.pop(0)\n",
" \n",
" if len(p1) >= c1 and len(p2) >= c2:\n",
" winner, _ = combat(p1[:c1], p2[:c2])\n",
" else:\n",
" winner = c2 > c1\n",
" \n",
" if not winner:\n",
" p1.append(c1)\n",
" p1.append(c2)\n",
" else:\n",
" p2.append(c2)\n",
" p2.append(c1)\n",
" \n",
" return bool(p2), p1 or p2\n",
" \n",
"\n",
"def solve2():\n",
" p1, p2 = [[*map(int,x.splitlines()[1:])] for x in puzzle.split(\"\\n\\n\")]\n",
" _, p = combat(p1, p2)\n",
" return sum((i+1)*x for i, x in enumerate(reversed(p)))\n",
"\n",
"solve2()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"33780"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve2m=lambda:(c:=lambda p,q:(s:=set())or next(f*(bool(q),p+q)or(0,)for()in iter(list,0)if (f:=not(p and q))or(k:=(tuple(p),tuple(q)))in s or s.add(k)or[p,q][(w:=c(p[:d[0]],q[:d[1]])[0]if(d:=[p.pop(0),q.pop(0)])[0]<=len(p)and d[1]<=len(q)else d[1]>d[0])].extend(d[::1-2*w])))and sum(x*(i+1)for i,x in enumerate((c(*[[*map(int,x.split()[2:])]for x in puzzle.split(\"\\n\\n\")])[1])[::-1]))\n",
"\n",
"solve2m()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.41 s ± 74.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"1.85 s ± 45.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit solve2()\n",
"%timeit solve2m()"
]
}
],
"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
}

41
Python/2020/22.py Normal file
View file

@ -0,0 +1,41 @@
from lib import *
input = read_input(2020, 22)
p1, p2 = [[*map(int, x.splitlines()[1:])] for x in input.split("\n\n")]
while p1 and p2:
if p1[0] > p2[0]:
p1.append(p1.pop(0))
p1.append(p2.pop(0))
else:
p2.append(p2.pop(0))
p2.append(p1.pop(0))
print(sum((i + 1) * x for i, x in enumerate(reversed(p1 or p2))))
def combat(p1, p2):
seen = set()
while p1 and p2:
if (k := (tuple(p1), tuple(p2))) in seen:
return 0, None
seen.add(k)
c1 = p1.pop(0)
c2 = p2.pop(0)
if len(p1) >= c1 and len(p2) >= c2:
winner, _ = combat(p1[:c1], p2[:c2])
else:
winner = c2 > c1
if not winner:
p1.append(c1)
p1.append(c2)
else:
p2.append(c2)
p2.append(c1)
return bool(p2), p1 or p2
p1, p2 = [[*map(int, x.splitlines()[1:])] for x in input.split("\n\n")]
_, p = combat(p1, p2)
print(sum((i + 1) * x for i, x in enumerate(reversed(p))))

View file

@ -1,236 +0,0 @@
{
"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 = 2020, 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": [
"'98645732'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def move(state):\n",
" current, moving, suffix = state[0], state[1:4], state[4:]\n",
" dst = suffix.index(max([x for x in suffix if int(x) < int(current)], default=max(suffix)))\n",
" state = current + suffix[:dst+1] + moving + suffix[dst+1:]\n",
" return state[1:] + state[0]\n",
"\n",
"def solve1():\n",
" state = puzzle\n",
" for _ in range(100):\n",
" state = move(state)\n",
" one = state.index(\"1\")\n",
" return state[one+1:] + state[:one]\n",
"\n",
"solve1()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'98645732'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve1m=lambda r=range:(n:=[*map(int,puzzle)],S:=dict(zip(n,n[1:]+n[:1])),c:=n[0],[0 for _ in r(100)if(u:=S[(t:=S[(s:=S[(f:=S[c])])])],d:=next(k for i in r(2,7)if(k:=(c-i)%len(S)+1)not in(f,s,t)),[S.__setitem__(*x)for x in[(c,u),(t,S[d]),(d,f)]],c:=S[c])*0])and((k:=lambda x:x-1 and str(x)+k(S[x])or\"\")(S[1]))\n",
"\n",
"solve1m()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"383 µs ± 27.6 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n",
"261 µs ± 19.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%timeit solve1()\n",
"%timeit solve1m()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Puzzle 2"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"689500518476"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def move(state, current):\n",
" first = state[current]\n",
" second = state[first]\n",
" third = state[second]\n",
" suffix = state[third]\n",
" \n",
" dst = current\n",
" while dst in (current, first, second, third):\n",
" dst -= 1\n",
" if not dst:\n",
" dst = len(state) - 1\n",
"\n",
" state[current] = suffix\n",
" state[third] = state[dst]\n",
" state[dst] = first\n",
" \n",
" return state[current]\n",
"\n",
"def solve2():\n",
" nums = list(map(int, puzzle))\n",
" \n",
" state = list(range(1, 1000002))\n",
" for a, b in zip([-1] + nums, nums + [len(nums) + 1]):\n",
" state[a] = b\n",
" \n",
" current = nums[0]\n",
" for _ in range(10000000):\n",
" current = move(state, current)\n",
" \n",
" first = state[1]\n",
" second = state[first]\n",
" return first * second\n",
"\n",
"solve2()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"689500518476"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve2m=lambda r=range:(n:=[*map(int,puzzle)],S:=[*r(1,1000002)],[S.__setitem__(*x)for x in zip([-1]+n,n+[len(n)+1])],c:=n[0],[0 for _ in r(10000000)if(u:=S[(t:=S[(s:=S[(f:=S[c])])])],d:=next(k for i in r(2,7)if(k:=(c-i)%(len(S)-1)+1)not in(f,s,t)),[S.__setitem__(*x)for x in[(c,u),(t,S[d]),(d,f)]],c:=S[c])*0])and(f:=S[1])*S[f]\n",
"\n",
"solve2m()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"11.4 s ± 234 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"29.2 s ± 606 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit solve2()\n",
"%timeit solve2m()"
]
}
],
"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
}

47
Python/2020/23.py Normal file
View file

@ -0,0 +1,47 @@
from lib import *
input = read_input(2020, 23)
def move1(state):
current, moving, suffix = state[0], state[1:4], state[4:]
dst = suffix.index(max([x for x in suffix if int(x) < int(current)], default=max(suffix)))
state = current + suffix[: dst + 1] + moving + suffix[dst + 1 :]
return state[1:] + state[0]
state = input.strip()
for _ in range(100):
state = move1(state)
one = state.index("1")
print(state[one + 1 :] + state[:one])
def move2(state, current):
first = state[current]
second = state[first]
third = state[second]
suffix = state[third]
dst = current
while dst in (current, first, second, third):
dst -= 1
if not dst:
dst = len(state) - 1
state[current] = suffix
state[third] = state[dst]
state[dst] = first
return state[current]
nums = list(map(int, input.strip()))
state = list(range(1, 1000002))
for a, b in zip([-1] + nums, nums + [len(nums) + 1]):
state[a] = b
current = nums[0]
for _ in range(10000000):
current = move2(state, current)
first = state[1]
second = state[first]
print(first * second)

View file

@ -1,228 +0,0 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import sys; sys.path.insert(0, \"..\")\n",
"\n",
"import aoc\n",
"\n",
"year, day = 2020, 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": [
"354"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"adj = {\"e\": (1, 0), \"w\": (-1, 0), \"se\": (1, 1), \"sw\": (0, 1), \"ne\": (0, -1), \"nw\": (-1, -1)}\n",
"\n",
"def get_tile(line):\n",
" x = y = 0\n",
" i = 0\n",
" while i < len(line):\n",
" if line[i] in \"ew\":\n",
" dx, dy = adj[line[i]]\n",
" i += 1\n",
" else:\n",
" dx, dy = adj[line[i:i+2]]\n",
" i += 2\n",
" x += dx\n",
" y += dy\n",
" return x, y\n",
"\n",
"def get_black():\n",
" black = set()\n",
" for line in plines:\n",
" x, y = get_tile(line)\n",
" if (x, y) in black:\n",
" black.remove((x, y))\n",
" else:\n",
" black.add((x, y))\n",
" return black\n",
" \n",
"def solve1():\n",
" return len(get_black())\n",
"\n",
"solve1()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"354"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve1m=lambda:(g:=(lambda l:((((t:=g(l[1:]))[0]+1-2*(l[0]==\"w\"),t[1])if l[0]in\"ew\"else((t:=g(l[2:]))[0]+(l[:2]==\"se\")-(l[:2]==\"nw\"),t[1]+1-2*(l[0]==\"n\")))if l else(0,0))),b:=set(),[b:=b^{g(l)}for l in plines])and len(b)\n",
"\n",
"solve1m()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.68 ms ± 243 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n",
"13 ms ± 337 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit solve1()\n",
"%timeit solve1m()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Puzzle 2"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3608"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def neighbours(x, y):\n",
" return {(x+dx, y+dy) for dx, dy in adj.values()}\n",
"\n",
"def solve2():\n",
" black = get_black()\n",
" \n",
" for _ in range(100):\n",
" to_update = [(p, q) for x, y in black for p, q in neighbours(x, y) | {(x, y)}]\n",
" flip_white = set()\n",
" flip_black = set()\n",
" for x, y in to_update:\n",
" cnt = len(black & neighbours(x, y))\n",
" alive = (x, y) in black\n",
" if alive and cnt not in (1, 2):\n",
" flip_white.add((x, y))\n",
" elif not alive and cnt == 2:\n",
" flip_black.add((x, y))\n",
" black -= flip_white\n",
" black |= flip_black\n",
" \n",
" return len(black)\n",
"\n",
"solve2()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3608"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solve2m=lambda:(g:=(lambda l:((((t:=g(l[1:]))[0]+1-2*(l[0]==\"w\"),t[1])if l[0]in\"ew\"else((t:=g(l[2:]))[0]+(l[:2]==\"se\")-(l[:2]==\"nw\"),t[1]+1-2*(l[0]==\"n\")))if l else(0,0))),n:=(lambda x,y:{(x+p,y+q)for p,q in[(1,0),(-1,0),(1,1),(0,1),(0,-1),(-1,-1)]}),s:=set(),[s:=s^{g(l)}for l in plines],[0 for _ in\"_\"*100if(f:=set(),F:=set(),[(a*(c not in(1,2))and f.add((x,y)),2==c*(1-a)and F.add((x,y)))for p,q in s for x,y in{(p,q)}|n(p,q)if(c:=len(s&n(x,y)),a:=(x,y)in s)],s:=s-f|F)])and len(s)\n",
"\n",
"solve2m()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4.21 s ± 158 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"4.4 s ± 246 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit solve2()\n",
"%timeit solve2m()"
]
}
],
"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
}

58
Python/2020/24.py Normal file
View file

@ -0,0 +1,58 @@
from lib import *
input = read_input(2020, 24)
lines = input.splitlines()
adj = {"e": (1, 0), "w": (-1, 0), "se": (1, 1), "sw": (0, 1), "ne": (0, -1), "nw": (-1, -1)}
def get_tile(line):
x = y = 0
i = 0
while i < len(line):
if line[i] in "ew":
dx, dy = adj[line[i]]
i += 1
else:
dx, dy = adj[line[i : i + 2]]
i += 2
x += dx
y += dy
return x, y
def get_black():
black = set()
for line in lines:
x, y = get_tile(line)
if (x, y) in black:
black.remove((x, y))
else:
black.add((x, y))
return black
print(len(get_black()))
def neighbours(x, y):
return {(x + dx, y + dy) for dx, dy in adj.values()}
black = get_black()
for _ in range(100):
to_update = [(p, q) for x, y in black for p, q in neighbours(x, y) | {(x, y)}]
flip_white = set()
flip_black = set()
for x, y in to_update:
cnt = len(black & neighbours(x, y))
alive = (x, y) in black
if alive and cnt not in (1, 2):
flip_white.add((x, y))
elif not alive and cnt == 2:
flip_black.add((x, y))
black -= flip_white
black |= flip_black
print(len(black))

View file

@ -1,126 +0,0 @@
{
"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 = 2020, 25\n",
"\n",
"puzzle = aoc.setup(year, day)\n",
"plines = puzzle.splitlines()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"16457981"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def solve():\n",
" *nums, = map(int, plines)\n",
" \n",
" k = 1\n",
" i = 0\n",
" while k not in nums:\n",
" k = (k * 7) % 20201227\n",
" i += 1\n",
"\n",
" return pow(nums[1 - nums.index(k)], i, 20201227)\n",
"\n",
"solve()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"16457981"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solvem=lambda k=1,i=0:(n:=[*map(int,plines)],next(0 for()in iter(list,0)if k in n or(k:=(k*7)%20201227,i:=i+1)*0))and pow(n[1-n.index(k)],i,20201227)\n",
"\n",
"solvem()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"667 ms ± 29.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"1.35 s ± 79.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit solve()\n",
"%timeit solvem()"
]
}
],
"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
}

15
Python/2020/25.py Normal file
View file

@ -0,0 +1,15 @@
from lib import *
input = read_input(2020, 25)
lines = input.splitlines()
(*nums,) = map(int, lines)
k = 1
i = 0
while k not in nums:
k = (k * 7) % 20201227
i += 1
print(pow(nums[1 - nums.index(k)], i, 20201227))

View file

@ -1,137 +0,0 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"=== Day 01 ===\n",
"Part 1: 259716\n",
"Part 2: 120637440\n",
"\n",
"=== Day 02 ===\n",
"Part 1: 424\n",
"Part 2: 747\n",
"\n",
"=== Day 03 ===\n",
"Part 1: 294\n",
"Part 2: 5774564250\n",
"\n",
"=== Day 04 ===\n",
"Part 1: 200\n",
"Part 2: 116\n",
"\n",
"=== Day 05 ===\n",
"Part 1: 938\n",
"Part 2: 696\n",
"\n",
"=== Day 06 ===\n",
"Part 1: 6782\n",
"Part 2: 3596\n",
"\n",
"=== Day 07 ===\n",
"Part 1: 124\n",
"Part 2: 34862\n",
"\n",
"=== Day 08 ===\n",
"Part 1: 1654\n",
"Part 2: 833\n",
"\n",
"=== Day 09 ===\n",
"Part 1: 31161678\n",
"Part 2: 5453868\n",
"\n",
"=== Day 10 ===\n",
"Part 1: 1885\n",
"Part 2: 2024782584832\n",
"\n",
"=== Day 11 ===\n",
"Part 1: 2113\n",
"Part 2: 1865\n",
"\n",
"=== Day 12 ===\n",
"Part 1: 590\n",
"Part 2: 42013\n",
"\n",
"=== Day 13 ===\n",
"Part 1: 1835\n",
"Part 2: 247086664214628\n",
"\n",
"=== Day 14 ===\n",
"Part 1: 14839536808842\n",
"Part 2: 4215284199669\n",
"\n",
"=== Day 15 ===\n",
"Part 1: 1618\n",
"Part 2: 548531\n",
"\n",
"=== Day 16 ===\n",
"Part 1: 25788\n",
"Part 2: 3902565915559\n",
"\n",
"=== Day 17 ===\n",
"Part 1: 289\n",
"Part 2: 2084\n",
"\n",
"=== Day 18 ===\n",
"Part 1: 1408133923393\n",
"Part 2: 314455761823725\n",
"\n",
"=== Day 19 ===\n",
"Part 1: 160\n",
"Part 2: 357\n",
"\n",
"=== Day 20 ===\n",
"Part 1: 15670959891893\n",
"Part 2: 1964\n",
"\n",
"=== Day 21 ===\n",
"Part 1: 2307\n",
"Part 2: cljf,frtfg,vvfjj,qmrps,hvnkk,qnvx,cpxmpc,qsjszn\n",
"\n",
"=== Day 22 ===\n",
"Part 1: 32199\n",
"Part 2: 33780\n",
"\n",
"=== Day 23 ===\n",
"Part 1: 98645732\n",
"Part 2: 689500518476\n",
"\n",
"=== Day 24 ===\n",
"Part 1: 354\n",
"Part 2: 3608\n",
"\n",
"=== Day 25 ===\n",
"Part 1: 16457981\n",
"\n",
"real\t1m28.162s\n",
"user\t1m23.327s\n",
"sys\t0m1.405s\n"
]
}
],
"source": [
"time python oneliners.py"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Bash",
"language": "bash",
"name": "bash"
},
"language_info": {
"codemirror_mode": "shell",
"file_extension": ".sh",
"mimetype": "text/x-sh",
"name": "bash"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

View file

@ -22,10 +22,10 @@
## [2020](https://adventofcode.com/2020) ([<img height=18 src=".assets/py.svg"> Python](Python/2020): 25/25 | [<img height=18 src=".assets/rs.svg"> Rust](Rust/2020): 8/25)
|Mo|Tu|We|Th|Fr|Sa|Su|
|-|-|-|-|-|-|-|
||[**1**](https://adventofcode.com/2020/day/1) [<img height=12 src=".assets/rs.svg">](Rust/2020/01.rs "Rust solution for 2020/01") [<img height=12 src=".assets/py.svg">](Python/2020/01.ipynb "Python solution for 2020/01")|[**2**](https://adventofcode.com/2020/day/2) [<img height=12 src=".assets/rs.svg">](Rust/2020/02.rs "Rust solution for 2020/02") [<img height=12 src=".assets/py.svg">](Python/2020/02.ipynb "Python solution for 2020/02")|[**3**](https://adventofcode.com/2020/day/3) [<img height=12 src=".assets/rs.svg">](Rust/2020/03.rs "Rust solution for 2020/03") [<img height=12 src=".assets/py.svg">](Python/2020/03.ipynb "Python solution for 2020/03")|[**4**](https://adventofcode.com/2020/day/4) [<img height=12 src=".assets/rs.svg">](Rust/2020/04.rs "Rust solution for 2020/04") [<img height=12 src=".assets/py.svg">](Python/2020/04.ipynb "Python solution for 2020/04")|[**5**](https://adventofcode.com/2020/day/5) [<img height=12 src=".assets/rs.svg">](Rust/2020/05.rs "Rust solution for 2020/05") [<img height=12 src=".assets/py.svg">](Python/2020/05.ipynb "Python solution for 2020/05")|[**6**](https://adventofcode.com/2020/day/6) [<img height=12 src=".assets/rs.svg">](Rust/2020/06.rs "Rust solution for 2020/06") [<img height=12 src=".assets/py.svg">](Python/2020/06.ipynb "Python solution for 2020/06")|
|[**7**](https://adventofcode.com/2020/day/7) [<img height=12 src=".assets/rs.svg">](Rust/2020/07.rs "Rust solution for 2020/07") [<img height=12 src=".assets/py.svg">](Python/2020/07.ipynb "Python solution for 2020/07")|[**8**](https://adventofcode.com/2020/day/8) [<img height=12 src=".assets/rs.svg">](Rust/2020/08.rs "Rust solution for 2020/08") [<img height=12 src=".assets/py.svg">](Python/2020/08.ipynb "Python solution for 2020/08")|[**9**](https://adventofcode.com/2020/day/9) [<img height=12 src=".assets/py.svg">](Python/2020/09.ipynb "Python solution for 2020/09")|[**10**](https://adventofcode.com/2020/day/10) [<img height=12 src=".assets/py.svg">](Python/2020/10.ipynb "Python solution for 2020/10")|[**11**](https://adventofcode.com/2020/day/11) [<img height=12 src=".assets/py.svg">](Python/2020/11.ipynb "Python solution for 2020/11")|[**12**](https://adventofcode.com/2020/day/12) [<img height=12 src=".assets/py.svg">](Python/2020/12.ipynb "Python solution for 2020/12")|[**13**](https://adventofcode.com/2020/day/13) [<img height=12 src=".assets/py.svg">](Python/2020/13.ipynb "Python solution for 2020/13")|
|[**14**](https://adventofcode.com/2020/day/14) [<img height=12 src=".assets/py.svg">](Python/2020/14.ipynb "Python solution for 2020/14")|[**15**](https://adventofcode.com/2020/day/15) [<img height=12 src=".assets/py.svg">](Python/2020/15.ipynb "Python solution for 2020/15")|[**16**](https://adventofcode.com/2020/day/16) [<img height=12 src=".assets/py.svg">](Python/2020/16.ipynb "Python solution for 2020/16")|[**17**](https://adventofcode.com/2020/day/17) [<img height=12 src=".assets/py.svg">](Python/2020/17.ipynb "Python solution for 2020/17")|[**18**](https://adventofcode.com/2020/day/18) [<img height=12 src=".assets/py.svg">](Python/2020/18.ipynb "Python solution for 2020/18")|[**19**](https://adventofcode.com/2020/day/19) [<img height=12 src=".assets/py.svg">](Python/2020/19.ipynb "Python solution for 2020/19")|[**20**](https://adventofcode.com/2020/day/20) [<img height=12 src=".assets/py.svg">](Python/2020/20.ipynb "Python solution for 2020/20")|
|[**21**](https://adventofcode.com/2020/day/21) [<img height=12 src=".assets/py.svg">](Python/2020/21.ipynb "Python solution for 2020/21")|[**22**](https://adventofcode.com/2020/day/22) [<img height=12 src=".assets/py.svg">](Python/2020/22.ipynb "Python solution for 2020/22")|[**23**](https://adventofcode.com/2020/day/23) [<img height=12 src=".assets/py.svg">](Python/2020/23.ipynb "Python solution for 2020/23")|[**24**](https://adventofcode.com/2020/day/24) [<img height=12 src=".assets/py.svg">](Python/2020/24.ipynb "Python solution for 2020/24")|[**25**](https://adventofcode.com/2020/day/25) [<img height=12 src=".assets/py.svg">](Python/2020/25.ipynb "Python solution for 2020/25")|26|27|
||[**1**](https://adventofcode.com/2020/day/1) [<img height=12 src=".assets/rs.svg">](Rust/2020/01.rs "Rust solution for 2020/01") [<img height=12 src=".assets/py.svg">](Python/2020/01.py "Python solution for 2020/01")|[**2**](https://adventofcode.com/2020/day/2) [<img height=12 src=".assets/rs.svg">](Rust/2020/02.rs "Rust solution for 2020/02") [<img height=12 src=".assets/py.svg">](Python/2020/02.py "Python solution for 2020/02")|[**3**](https://adventofcode.com/2020/day/3) [<img height=12 src=".assets/rs.svg">](Rust/2020/03.rs "Rust solution for 2020/03") [<img height=12 src=".assets/py.svg">](Python/2020/03.py "Python solution for 2020/03")|[**4**](https://adventofcode.com/2020/day/4) [<img height=12 src=".assets/rs.svg">](Rust/2020/04.rs "Rust solution for 2020/04") [<img height=12 src=".assets/py.svg">](Python/2020/04.py "Python solution for 2020/04")|[**5**](https://adventofcode.com/2020/day/5) [<img height=12 src=".assets/rs.svg">](Rust/2020/05.rs "Rust solution for 2020/05") [<img height=12 src=".assets/py.svg">](Python/2020/05.py "Python solution for 2020/05")|[**6**](https://adventofcode.com/2020/day/6) [<img height=12 src=".assets/rs.svg">](Rust/2020/06.rs "Rust solution for 2020/06") [<img height=12 src=".assets/py.svg">](Python/2020/06.py "Python solution for 2020/06")|
|[**7**](https://adventofcode.com/2020/day/7) [<img height=12 src=".assets/rs.svg">](Rust/2020/07.rs "Rust solution for 2020/07") [<img height=12 src=".assets/py.svg">](Python/2020/07.py "Python solution for 2020/07")|[**8**](https://adventofcode.com/2020/day/8) [<img height=12 src=".assets/rs.svg">](Rust/2020/08.rs "Rust solution for 2020/08") [<img height=12 src=".assets/py.svg">](Python/2020/08.py "Python solution for 2020/08")|[**9**](https://adventofcode.com/2020/day/9) [<img height=12 src=".assets/py.svg">](Python/2020/09.py "Python solution for 2020/09")|[**10**](https://adventofcode.com/2020/day/10) [<img height=12 src=".assets/py.svg">](Python/2020/10.py "Python solution for 2020/10")|[**11**](https://adventofcode.com/2020/day/11) [<img height=12 src=".assets/py.svg">](Python/2020/11.py "Python solution for 2020/11")|[**12**](https://adventofcode.com/2020/day/12) [<img height=12 src=".assets/py.svg">](Python/2020/12.py "Python solution for 2020/12")|[**13**](https://adventofcode.com/2020/day/13) [<img height=12 src=".assets/py.svg">](Python/2020/13.py "Python solution for 2020/13")|
|[**14**](https://adventofcode.com/2020/day/14) [<img height=12 src=".assets/py.svg">](Python/2020/14.py "Python solution for 2020/14")|[**15**](https://adventofcode.com/2020/day/15) [<img height=12 src=".assets/py.svg">](Python/2020/15.py "Python solution for 2020/15")|[**16**](https://adventofcode.com/2020/day/16) [<img height=12 src=".assets/py.svg">](Python/2020/16.py "Python solution for 2020/16")|[**17**](https://adventofcode.com/2020/day/17) [<img height=12 src=".assets/py.svg">](Python/2020/17.py "Python solution for 2020/17")|[**18**](https://adventofcode.com/2020/day/18) [<img height=12 src=".assets/py.svg">](Python/2020/18.py "Python solution for 2020/18")|[**19**](https://adventofcode.com/2020/day/19) [<img height=12 src=".assets/py.svg">](Python/2020/19.py "Python solution for 2020/19")|[**20**](https://adventofcode.com/2020/day/20) [<img height=12 src=".assets/py.svg">](Python/2020/20.py "Python solution for 2020/20")|
|[**21**](https://adventofcode.com/2020/day/21) [<img height=12 src=".assets/py.svg">](Python/2020/21.py "Python solution for 2020/21")|[**22**](https://adventofcode.com/2020/day/22) [<img height=12 src=".assets/py.svg">](Python/2020/22.py "Python solution for 2020/22")|[**23**](https://adventofcode.com/2020/day/23) [<img height=12 src=".assets/py.svg">](Python/2020/23.py "Python solution for 2020/23")|[**24**](https://adventofcode.com/2020/day/24) [<img height=12 src=".assets/py.svg">](Python/2020/24.py "Python solution for 2020/24")|[**25**](https://adventofcode.com/2020/day/25) [<img height=12 src=".assets/py.svg">](Python/2020/25.py "Python solution for 2020/25")|26|27|
|28|29|30|31||||
## [2019](https://adventofcode.com/2019) ([<img height=18 src=".assets/py.svg"> Python](Python/2019): 25/25)