This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scores = { | |
')': 1, | |
']': 2, | |
'}': 3, | |
'>': 4 | |
} | |
expected = { | |
'(': ')', | |
'[': ']', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import heapq | |
def adjacent(x, y, cave): | |
deltas = [(-1, 0), (0, -1), (1, 0), (0, 1)] | |
for delta_x, delta_y in deltas: | |
candidate_x = x + delta_x | |
candidate_y = y + delta_y | |
if (candidate_y >= 0 and candidate_y < len(cave) and | |
candidate_x >= 0 and candidate_x < len(cave[candidate_y])): | |
yield candidate_x, candidate_y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import heapq | |
def get(x, y, cave): | |
overflow = y // len(cave) | |
y = y % len(cave) | |
overflow += x // len(cave[y]) | |
x = x % len(cave[y]) | |
value = cave[y][x] | |
value += overflow | |
value %= 9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TAG_LITERAL = 4 | |
TYPE_BITS = 0 | |
class Stream: | |
def __init__(self, data): | |
self.data = data | |
self.ready_carry = 0 | |
self.carry_size = 0 | |
self.carry = 0 | |
self.nibbles = self._init_read() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TAG_LITERAL = 4 | |
TAG_SUM = 0 | |
TAG_PRODUCT = 1 | |
TAG_MIN = 2 | |
TAG_MAX = 3 | |
TAG_GT = 5 | |
TAG_LT = 6 | |
TAG_EQ = 7 | |
TYPE_BITS = 0 |