def DELETE(self, repository_id): try: rm = rutils.get_repository_manager() data = rm.delete_repository(utilities.clean_id(repository_id)) return utilities.success() except Exception as ex: utilities.handle_exceptions(ex)
def DELETE(self, log_id, entry_id): try: logm = logutils.get_logging_manager() log = logm.get_log(utilities.clean_id(log_id)) log.delete_log_entry(utilities.clean_id(entry_id)) return utilities.success() except Exception as ex: utilities.handle_exceptions(ex)
def DELETE(self, log_id): try: logm = logutils.get_logging_manager() logm.delete_log(utilities.clean_id(log_id)) return utilities.success() except IllegalState as ex: modified_ex = type(ex)('Log is not empty.') utilities.handle_exceptions(modified_ex) except Exception as ex: utilities.handle_exceptions(ex)
def fill(square, images, current: int): if current == len(square) ** 2: success(square[0][0][0] * square[0][-1][0] * square[-1][0][0] * square[-1][-1][0]) quit() x, y = current % len(square), current // len(square) for id, image in images: for symmetric_image in yield_symmetric_images(image): # if we can add it if (y == 0 or down(square[y - 1][x][1], symmetric_image)) and ( x == 0 or right(square[y][x - 1][1], symmetric_image) ): #if square[0][0] is not None and square[0][0][0] == 1951: # print(symmetric_image) square[y][x] = (id, symmetric_image) fill(square, images - {(id, image)}, current + 1) square[y][x] = None
import sys sys.path.insert(0, "../") from utilities import success, get_input instructions = get_input() acc = 0 ptr = 0 visited = set() while True: if ptr in visited: success(acc) visited.add(ptr) opt, arg = instructions[ptr].split(" ") if opt == "acc": acc += int(arg) elif opt == "jmp": ptr += int(arg) continue ptr += 1
for value in values.split(" or "): lo, hi = value.split("-") rules[rule].append((int(lo), int(hi))) i += 1 my_ticket = input[i + 2] tickets = [] for ticket in input[i + 5 :]: tickets.append(list(map(int, ticket.split(",")))) def is_valid(ticket): for rule in rules: for lo, hi in rules[rule]: if lo <= value <= hi: return True return False error = 0 for ticket in tickets: for value in ticket: if not is_valid(value): error += value success(error)
elif term in operators: new_op, new_prec = operators[term] while len(operator_stack) > 0 and operator_stack[-1] in operators: stack_op, stack_prec = operators[operator_stack[-1]] if stack_prec >= new_prec: operator = operators[operator_stack.pop()][0] b, a = number_stack.pop(), number_stack.pop() number_stack.append(operator(a, b)) else: break operator_stack.append(term) while len(operator_stack) != 0: operator = operators[operator_stack.pop()][0] b, a = number_stack.pop(), number_stack.pop() number_stack.append(operator(a, b)) return number_stack[0] def tokenize(line): return " ( ".join(((" ) ".join(line.split(")"))).split("("))).split() success(sum(evaluate(tokenize(line)) for line in input))
for inst in get_input(): opt, val = inst.split(" = ") if opt == "mask": mask = val else: index = int(opt.split("[")[1].strip("]")) number = int(val) floats = [] for i, c in enumerate(reversed(mask)): if c != "X": if c == "1": index = index & (ones ^ (1 << i)) | 1 << i else: floats.append(i) for p in product((0, 1), repeat=len(floats)): v = index for f, b in zip(floats, p): v = v & (ones ^ (1 << f)) | b << f memory[v] = number total = 0 for i in memory: total += memory[i] success(total)
while i < len(tickets): ticket = tickets[i] for value in ticket: if not is_valid(value): del tickets[i] break else: i += 1 # for the number of rules assigned_rules = [None] * len(tickets[0]) for _ in tickets[0]: # examine each row, whether we can place it there for column in range(len(tickets[0])): possible = set(rule for rule in rules if rule not in assigned_rules) for row in range(len(tickets)): for rule in rules: if not is_valid(tickets[row][column], rule): possible -= {rule} if len(possible) == 1: assigned_rules[column] = list(possible)[0] prod = 1 for i, rule in enumerate(assigned_rules): if rule.startswith("departure"): prod *= my_ticket[i] success(prod)
total += 1 return total side = int(len(images)**(1 / 2)) result = fill([[None] * side for _ in range(side)], images, 0) water_side = side * (len(result[0][0][1]) - 2) waters = ["" for _ in range(water_side)] for i, a in enumerate(result): for id, b in a: for j, row in enumerate(b[1:-1]): waters[i * (len(result[0][0][1]) - 2) + j] += row[1:-1] for i in yield_symmetric_images(waters): result = count_monsters(i) if result != 0: total = 0 for row in waters: for c in row: if c == "#": total += 1 # assume they don't overlap success(total - 15 * result)
import sys sys.path.insert(0, "../") from utilities import success, get_input wait = int(get_input()[0]) times = get_input()[1].split(",") minimum_time = float("+inf") minimum_bus = 0 for time in times: if time == "x": continue time = int(time) if time - wait % time < minimum_time: minimum_time = time - wait % time minimum_bus = time success(minimum_time * minimum_bus)
_, x, y = get_extended_gcd(base, time) lcm = get_lcm(base, time) equations.append((lcm, -base * x * i)) while len(equations) != 1: e2, e1 = equations.pop(), equations.pop() a, b, s = e1[0], -e2[0], -(e1[1] - e2[1]) gcd, x, y = get_extended_gcd(a, b) a //= gcd b //= gcd s //= gcd j = -1 while True: if (b * j + s) % a == 0 and (b * j + s) // a > 0: break j -= 1 i = (b * j + s) // a lcm = get_lcm(a, b) equations.append((lcm, gcd * a * i + e1[1])) success(equations[0][1])
import sys sys.path.insert(0, "../") from utilities import success, get_input numbers = list(map(int, get_input(whole=True).split(","))) def rfind(numbers, n): for i in reversed(range(len(numbers))): if numbers[i] == n: return i return len(numbers) while len(numbers) < 2020: numbers.append(len(numbers) - 1 - rfind(numbers[:-1], numbers[-1])) success(numbers[-1])
sys.path.insert(0, "../") from utilities import success, get_input d, x, y = 0, 0, 0 delta = ((1, 0), (0, 1), (-1, 0), (0, -1)) for inst in get_input(): by = int(inst[1:]) if inst[0] == "E": x += by if inst[0] == "N": y += by if inst[0] == "S": y -= by if inst[0] == "W": x -= by if inst[0] == "R": by //= 90 d = (d - by) % 4 if inst[0] == "L": by //= 90 d = (d + by) % 4 if inst[0] == "F": x += delta[d][0] * by y += delta[d][1] * by success(abs(x) + abs(y))
pairings = [] foods = set() alergens = set() for line in input: ingredients, alg = line.split(" (contains ") pairings.append((ingredients.split(), alg.strip(")").split(", "))) foods = foods.union(set(pairings[-1][0])) alergens = alergens.union(set(pairings[-1][1])) canonical = [] while len(alergens) != 0: for alergen in alergens: possible_foods = set(foods) for f, a in pairings: if alergen in a: for food in foods: if food not in f: possible_foods -= {food} if len(possible_foods) == 1: canonical.append((list(possible_foods)[0], alergen)) alergens -= set([alergen]) foods -= possible_foods break success(",".join(a for a, b in sorted(canonical, key=lambda x: x[1])))
import sys sys.path.insert(0, "../") from utilities import success, get_input input = get_input(as_int=True) target = 2020 saw = set() for value in input: if target - value in saw: success(value * (target - value)) saw.add(value)
def pprint(lines): for line in lines: print("".join(line)) print() while True: new_lines = [[c for c in line] for line in lines] for y in range(len(new_lines)): for x in range(len(new_lines[0])): _, occupied = neighbours(x, y) if lines[y][x] == "L" and occupied == 0: new_lines[y][x] = "#" if lines[y][x] == "#" and occupied >= 4: new_lines[y][x] = "L" if lines == new_lines: break lines = new_lines count = 0 for y in range(len(new_lines)): for x in range(len(new_lines[0])): if lines[y][x] == "#": count += 1 success(count)
sys.path.insert(0, "../") from utilities import success, get_input d, xw, yw, xs, ys = 0, 10, 1, 0, 0 for inst in get_input(): by = int(inst[1:]) if inst[0] == "E": xw += by if inst[0] == "N": yw += by if inst[0] == "S": yw -= by if inst[0] == "W": xw -= by if inst[0] == "R": for i in range(by // 90): xw, yw = yw, -xw if inst[0] == "L": for i in range(by // 90): xw, yw = -yw, xw if inst[0] == "F": xs += xw * by ys += yw * by success(abs(xs) + abs(ys))
bags = {} for line in input: bag, spec = line.split(" bags contain ") for other in spec.split(", "): other = other.strip(".").strip("bag").strip("bags").strip() if other == "no other": bags[bag] = {} else: if bag not in bags: bags[bag] = {} count, o = other.split(" ", 1) bags[bag][o] = count def recursive_sum(bag): if len(bags[bag]) == 0: return 0 total = 0 for b, i in bags[bag].items(): total += recursive_sum(b) * int(i) + int(i) return total success(recursive_sum("shiny gold"))
import sys sys.path.insert(0, "../") from utilities import success, get_input input = get_input(as_int=True) target = 2020 saw = set() for i, v1 in enumerate(input): for v2 in input[i + 1:]: if target - v1 - v2 in saw: success(v1 * v2 * (target - v1 - v2)) saw.add(v1)
# skip zero delta if xd == yd == zd == ad == 0: continue yield (x + xd, y + yd, z + zd, a + ad) if yield_itself: yield cell for i in range(len(input)): for j in range(len(input[0])): if input[i][j] == "#": state.add((i, j, 0, 0)) for i in range(6): new_state = set() for cell in state: for c in neighbouring_cells(cell, yield_itself=True): # if the cell is alive if c in state: if 2 <= neighbours(c) <= 3: new_state.add(c) else: if neighbours(c) == 3: new_state.add(c) state = new_state success(len(state))
input = get_input() m = 0 for line in input: row = 0 col = 0 lo = 0 hi = 128 for c in line[:7]: avg = (lo + hi) // 2 if c == "F": hi = avg else: lo = avg row = lo lo = 0 hi = 8 for c in line[7:]: avg = (lo + hi) // 2 if c == "L": hi = avg else: lo = avg col = lo m = max(m, row * 8 + col) success(m)
def find_continuous_sum(target): for i in range(len(instructions)): for j in range(i + 1, len(instructions)): if sum(instructions[i:j]) == target: success(min(instructions[i:j]) + max(instructions[i:j]))
import sys sys.path.insert(0, "../") from utilities import success, get_input inst = sorted(get_input(as_int=True)) one_jolt = 1 three_jolt = 1 for i in range(len(inst) - 1): if inst[i + 1] - inst[i] == 1: one_jolt += 1 else: three_jolt += 1 success(one_jolt * three_jolt)
import sys sys.path.insert(0, "../") from utilities import success, get_input instructions = get_input(as_int=True) numbers = [] preamble = 25 def adds_up_to(target): for i in range(len(numbers)): for j in range(i + 1, len(numbers)): if numbers[i] + numbers[j] == target: return True return False for n in instructions[:preamble]: numbers.append(n) for n in instructions[preamble:]: if not adds_up_to(n): success(n) numbers.pop(0) numbers.append(n)
import sys sys.path.insert(0, "../") from utilities import success, get_input input = get_input() x, y = 0, 0 trees = 0 for y in range(1, len(input)): line = input[y] x = (x + 3) % len(line) if input[y][x] == "#": trees += 1 success(trees)
ids = list() for line in input: row = 0 col = 0 lo = 0 hi = 128 for c in line[:7]: avg = (lo + hi) // 2 if c == "F": hi = avg else: lo = avg row = lo lo = 0 hi = 8 for c in line[7:]: avg = (lo + hi) // 2 if c == "L": hi = avg else: lo = avg col = lo ids.append(row * 8 + col) ids = sorted(ids) for i in range(len(ids) - 1): if ids[i] != ids[i + 1] - 1: success(ids[i] + 1)
import sys sys.path.insert(0, "../") from utilities import success, get_input input = get_input() slopes = ((1, 1), (3, 1), (5, 1), (7, 1), (1, 2)) product = 1 for xd, yd in slopes: x, y = 0, 0 trees = 0 for y in range(yd, len(input), yd): line = input[y] x = (x + xd) % len(line) if input[y][x] == "#": trees += 1 product *= trees success(product)