Skip to content

Instantly share code, notes, and snippets.

from collections import defaultdict
memo = {}
def key(char, next_char, steps):
return '{0}{1}{2}'.format(char, next_char, steps)
def combine(current, left, right):
count = defaultdict(lambda: 0, left)
count[current] += 1
def key(x, y):
return "{0}-{1}".format(x, y)
def fold(points, axis, fold_point):
fold_changes = []
for point_key in points:
x, y = points[point_key]
if axis == 'y':
if y > fold_point:
fold_changes.append((x, y, x, fold_point - (y - fold_point)))
from collections import defaultdict
def is_small_cave(node):
if node == node.lower():
return True
def count_paths(node, paths, visited, allow_double_visit):
if node in visited:
# If we haven't used our double visit yet, let's use it on this branch.
from collections import defaultdict
def is_small_cave(node):
if node == node.lower():
return True
def count_paths(node, paths, visited):
if node in visited:
return 0
def key(x,y):
return "{0}-{1}".format(x, y)
def energize(octopi, x, y, flashed):
if y < 0 or y >= len(octopi) or x < 0 or x >= len(octopi[y]):
return
if key(x, y) in flashed:
return
octopi[y][x] += 1
if octopi[y][x] > 9:
scores = {
')': 1,
']': 2,
'}': 3,
'>': 4
}
expected = {
'(': ')',
'[': ']',
scores = {
')': 3,
']': 57,
'}': 1197,
'>': 25137
}
expected = {
'(': ')',
def is_low_point(heights, x, y):
adjacent = []
if x > 0:
adjacent.append(heights[y][x - 1])
if x < len(heights[y]) - 1:
adjacent.append(heights[y][x + 1])
if y > 0:
adjacent.append(heights[y - 1][x])
if y < len(heights) - 1:
adjacent.append(heights[y + 1][x])
def a_minus_b(a, b):
output = []
for char in a:
if char in b:
continue
output.append(char)
return output
def solve(train_data):
digits = train_data.split(' ')
def simulate(lanternfish, days):
new_lanternfish = [0] * 9
for i in range(days):
new_hatched = lanternfish[8]
for i in range(1, 9):
new_lanternfish[i] = lanternfish[i - 1]
new_lanternfish[0] = new_hatched
new_lanternfish[2] += new_hatched
tmp = lanternfish
lanternfish = new_lanternfish