Skip to content

Instantly share code, notes, and snippets.

View redspider's full-sized avatar

Richard Clark redspider

  • Red Spider Limited
  • Wellington, New Zealand
View GitHub Profile
# I went for speed on this one, it was a mess
HEIGHT = len(INPUT.split("\n"))
WIDTH = len(INPUT.split("\n")[0])
grid = dict()
for y, line in enumerate(INPUT.split("\n")):
for x, c in enumerate(line):
grid[(x, y)] = c
adapters = [int(x) for x in INPUT.split("\n")]
# Part 1
last = 0
differences = []
for n in sorted(adapters):
differences.append(n - last)
last = n
differences.append(3)
import operator
from itertools import accumulate, combinations, product
INPUT="""..."""
PREAMBLE = 25
values = [int(n) for n in INPUT.split("\n")]
def run_program(program):
acc = 0
pointer = 0
seen = set()
while pointer not in seen:
op, value = program[pointer]
seen.add(pointer)
if op == 'nop':
pointer += 1
from functools import reduce
print("Part 1")
print(sum([len(set(list(bunch.replace("\n", "")))) for bunch in INPUT.split("\n\n")]))
print("Part 2")
print(sum(len(reduce(set.intersection, [set(line) for line in bunch.split("\n")])) for bunch in INPUT.split("\n\n")))
def partition(start, end, top):
if top:
return start, start + (end - start) // 2
return start + (end - start) // 2 + 1, end
def score_seat(boarding_pass):
start_row = 0
end_row = 127
import re
REQUIRED_KEYS = [
'byr',
'iyr',
'eyr',
'hgt',
'hcl',
'ecl',
'pid'
WIDTH = len(INPUT.split("\n")[0])
# Part 1
trees = 0
for i, line in enumerate(INPUT.split("\n")):
if line[i * 3 % WIDTH] == '#':
trees += 1
print(trees)
valid = 0
# Part 1
for line in INPUT.split("\n"):
m = re.match(r"(\d+)-(\d+) (\S): (\S+)", line)
(low, high, character, password) = m.groups()
if int(low) <= password.count(character) <= int(high):
valid += 1
import itertools
import math
numbers = [int(x) for x in INPUT.split("\n")]
# Part 1
print([math.prod(match) for match in itertools.combinations(numbers, 2) if sum(match) == 2020])
# Part 2
print([math.prod(match) for match in itertools.combinations(numbers, 3) if sum(match) == 2020])