Initial commit with days 1, 2, and 3

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2021-12-03 12:36:35 -08:00
commit adb46f38a8
8 changed files with 4326 additions and 0 deletions

166
.gitignore vendored Normal file
View File

@@ -0,0 +1,166 @@
# Created by https://www.toptal.com/developers/gitignore/api/python,vim
# Edit at https://www.toptal.com/developers/gitignore?templates=python,vim
### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
### Vim ###
# Swap
[._]*.s[a-v][a-z]
!*.svg # comment out if you don't need vector files
[._]*.sw[a-p]
[._]s[a-rt-v][a-z]
[._]ss[a-gi-z]
[._]sw[a-p]
# Session
Session.vim
Sessionx.vim
# Temporary
.netrwhist
*~
# Auto-generated tag files
tags
# Persistent undo
[._]*.un~
# End of https://www.toptal.com/developers/gitignore/api/python,vim

11
README.md Normal file
View File

@@ -0,0 +1,11 @@
# Advent of code 2021
You should be able to run any of them by cd'ing into the directory and executing the script and
passing the input file in through STDIN.
Example:
```
cd day02
./day02.py < input.txt
```

32
day01/day01.py Executable file
View File

@@ -0,0 +1,32 @@
#!/usr/bin/env python3
import sys
from typing import Sequence
def part1(lines: Sequence[int]):
inc = 0
last = lines[0]
for line in lines[1:]:
if line > last:
inc += 1
last = line
print(f"{inc} increments")
def part2(lines: Sequence[int]):
WINDOW_SIZE = 3
last = sum(lines[0:WINDOW_SIZE])
inc = 0
for i in range(1, len(lines) - WINDOW_SIZE + 1):
window = sum(lines[i:i+WINDOW_SIZE])
if window > last:
inc += 1
last = window
print(f"{inc} increments")
lines = [int(line) for line in sys.stdin if line]
print("Part 1")
part1(lines)
print("Part 2")
part2(lines)

2000
day01/input.txt Normal file

File diff suppressed because it is too large Load Diff

47
day02/day02.py Executable file
View File

@@ -0,0 +1,47 @@
#!/usr/bin/env python3
import sys
from typing import Sequence, Tuple
def part1(lines: Sequence[Tuple[str, int]]):
horz = 0
depth = 0
for direction, amount in lines:
if direction == "forward":
horz += amount
elif direction == "down":
depth += amount
elif direction == "up":
depth -= amount
else:
assert False, f"not sure how to handle {direction}"
print(f"horizontal position: {horz}")
print(f"depth: {depth}")
print(f"product: {depth * horz}")
def part2(lines: Sequence[Tuple[str, int]]):
horz = 0
depth = 0
aim = 0
for direction, amount in lines:
if direction == "forward":
horz += amount
depth += aim * amount
elif direction == "down":
aim += amount
elif direction == "up":
aim -= amount
else:
assert False, f"not sure how to handle {direction}"
print(f"horizontal position: {horz}")
print(f"depth: {depth}")
print(f"product: {depth * horz}")
lines = [(direction, int(amount)) for direction, amount in map(str.split, sys.stdin)]
print("Part 1")
part1(lines)
print("Part 2")
part2(lines)

1000
day02/input.txt Normal file

File diff suppressed because it is too large Load Diff

70
day03/day03.py Executable file
View File

@@ -0,0 +1,70 @@
#!/usr/bin/env python3
import copy
import sys
from typing import Sequence
def part1(lines: Sequence[int], bits=12):
gamma = 0
epsilon = 0
for bit in range(bits):
mask = 1 << (bits - bit - 1)
ones = 0
for line in lines:
if mask & line:
ones += 1
zeroes = len(lines) - ones
if ones > zeroes:
gamma |= mask
else:
epsilon |= mask
# Fun thing - epsilon rate is just the inversion of the gamma rate.
# However, python doesn't seem to support signed inversion so we just calculate it above. boo
print(f"gamma: {gamma} {bin(gamma)}")
print(f"epsilon: {epsilon} {bin(epsilon)}")
print(f"product: {gamma * epsilon}")
def part2(lines: Sequence[int], bits=12):
oxygen = copy.deepcopy(lines)
carbon = copy.deepcopy(lines)
for bit in range(bits):
# Find the most common bit
if len(oxygen) > 1:
mask = 1 << (bits - bit - 1)
ones = 0
for line in oxygen:
if mask & line:
ones += 1
zeroes = len(oxygen) - ones
if ones >= zeroes:
# Filter out oxygen numbers that don't have a 1 in this position
oxygen = [line for line in oxygen if line & mask]
else:
oxygen = [line for line in oxygen if not (line & mask)]
# Now do this on the carbon array
if len(carbon) > 1:
ones = 0
for line in carbon:
if mask & line:
ones += 1
zeroes = len(carbon) - ones
if ones >= zeroes:
# Keep the *least* common
carbon = [line for line in carbon if not (line & mask)]
else:
carbon = [line for line in carbon if line & mask]
oxygen, = oxygen
carbon, = carbon
print(f"Oxygen: {oxygen}")
print(f"Carbon: {carbon}")
print(f"Product: {oxygen * carbon}")
lines = [int(line, 2) for line in sys.stdin]
print("Part 1")
part1(lines)
print("Part 2")
part2(lines)

1000
day03/input.txt Normal file

File diff suppressed because it is too large Load Diff