zerosleeps

Since 2010

Advent of Code 2020 day 2

My Python solution for Advent of Code 2020 day 2. Taking the object-orientated path when I tackled part one didn’t really buy me much for part two, but I stand by my decision!

from pathlib import Path
import re
import unittest

def get_input():
    input_file = Path(__file__).parent / 'day_02_input.txt'

    with open(input_file) as file:
        return [line.strip() for line in file.readlines()]

class Password():
    def __init__(self, input):
        match = re.match('^(\d+)-(\d+) (\w): (\w+)$', input)
        self.min = int(match[1])
        self.max = int(match[2])
        self.letter = match[3]
        self.password = match[4]

    def valid_password(self):
        c = self.password.count(self.letter)
        if c >= self.min and c <= self.max:
            return True
        else:
            return False

    def new_valid_password(self):
        if (self.password[self.min - 1] == self.letter) ^ (self.password[self.max - 1] == self.letter):
            return True
        else:
            return False

class TestExamples(unittest.TestCase):
    def setUp(self):
        example_list = """1-3 a: abcde
                   1-3 b: cdefg
                   2-9 c: ccccccccc"""
        self.passwords = [Password(p.strip()) for p in example_list.splitlines()]

    def test_part_one(self):
        self.assertEqual(len([p for p in self.passwords if p.valid_password()]), 2)

    def test_part_two(self):
        self.assertEqual(len([p for p in self.passwords if p.new_valid_password()]), 1)

if __name__ == '__main__':
    passwords = [Password(p) for p in get_input()]
    print(f'Part one: {len([p for p in passwords if p.valid_password()])}')
    print(f'Part two: {len([p for p in passwords if p.new_valid_password()])}')

Advent of Code 2020 day 1

My Python solution for Advent of Code 2020 day 1. I’ve cleaned this up a wee bit since submitting my answers, but the essence of it hasn’t changed. I did use itertools.permutations at first, which happened to work because the loop bails out as soon as it finds a solution, but I changed it to itertools.combinations for readability.

from pathlib import Path
from itertools import combinations
from math import prod
import unittest

def get_input():
    input_file = Path(__file__).parent / 'day_01_input.txt'

    with open(input_file) as file:
        return [int(line.strip()) for line in file.readlines()]

def run(input, length):
    for p in combinations(input, length):
        if sum(p) == 2020:
            return(prod(p))

class TestExamples(unittest.TestCase):
    def setUp(self):
        self.input = [1721,979,366,299,675,1456]

    def test_part_one(self):
        self.assertEqual(run(self.input, 2), 514579)

    def test_part_two(self):
        self.assertEqual(run(self.input, 3), 241861950)

if __name__ == '__main__':
    print(f'Part one: {run(get_input(), 2)}')
    print(f'Part two: {run(get_input(), 3)}')

News is bad for you

I’m currently reading “How to Make the World Add Up” by Tim Harford, which references an article titled “News is bad for you – and giving up reading it will make you happier” by Rolf Dobelli. This quote is from that Guardian article:

News items are bubbles popping on the surface of a deeper world. Will accumulating facts help you understand the world? Sadly, no. The relationship is inverted. The important stories are non-stories: slow, powerful movements that develop below journalists’ radar but have a transforming effect. The more “news factoids” you digest, the less of the big picture you will understand.

Here in late 2020 the stories and advice this one contains are more relevant than ever.

How to think about lockdowns

This video from CGP Grey is gloriously logical and well-reasoned, especially the first 7 minutes. A worthy reminder that (almost) everyone is doing the best they can given the information they have at the time.

Shout out to this quip in response to “How will lockdown affect education in the future?”:

I think this quarantine has made plain some of the necessary lies of civilisation around education, particularly higher education, but, I leave what those are as an exercise for the viewer. For now.

Yessir.