Advent of Code 2021

I’ll be solving these in Python.

Update after day 3: seems I grossly overestimated the difficulty of these problems. I expected perhaps some algorithms or thinking, as I believe I recall such problems from the past AoCs. But these problems so far are just “problems of expression.” I.e. they’re more exercises in finding the correct language idioms to use or something like that. Perhaps better for people just starting out to learn the basics of programming?

Update after day 8: OK, I take that back. The problems do get a little bit more involved, there’s a little math here and there, and day 7 I (sort of) failed the first time I attempted it, so I have no reason to act cocky.

Some common prelude code I put in aoc-common.py to allow me to download the website data remotely. I opted out of having it also post the answer back.

import requests
from pathlib import Path

class aoc:
  def __init__(self, cookie_file='cookie.txt'):
    self.sess = requests.Session()
    for line in (Path(__file__).parent / cookie_file).open('rt'):
      if '=' in line:
        name, val = line.strip().split('=', 1)
        self.sess.cookies[name] = val
    self._cache = {}

  def input_text(self, day, year=2021):
    url = f'https://adventofcode.com/{year}/day/{day}/input'
    if url not in self._cache:
      self._cache[url] = self.sess.get(url)
    return self._cache[url].text