from advent import AdventProblem def part_1(numbers): for n1 in numbers: for n2 in numbers: if n1 + n2 == 2020: return n1 * n2 def part_2(numbers): for n1 in numbers: for n2 in numbers: for n3 in numbers: if n1 + n2 + n3 == 2020: return n1 * n2 * n3 if __name__ == '__main__': part1 = AdventProblem(1, 1, lambda l: int(l)) part1.add_solution(part_1) part1.run() part2 = AdventProblem(1, 2, lambda l: int(l)) part2.add_solution(part_2) part2.run()
@_('expr TIMES expr') def expr(self, p): return p.expr0 * p.expr1 @_('LPAREN expr RPAREN') def expr(self, p): return p.expr @_('NUMBER') def expr(self, p): return int(p.NUMBER) def part_2(inputs): lexer = CalcLexer() parser = CalcParser() total = 0 for input in inputs: total += parser.parse(lexer.tokenize(input)) return total if __name__ == '__main__': part1 = AdventProblem(18, 1, preprocess) part1.add_solution(part_1) part1.run() part2 = AdventProblem(18, 2, lambda x: x) part2.add_solution(part_2) part2.run()
waypoint_pos = (waypoint_pos[1], -waypoint_pos[0]) return waypoint_pos def part_2(instrs): dirs = ['E', 'N', 'W', 'S'] reversed_dirs = list(reversed(dirs)) pos = [0, 0] waypoint_pos = [10, 1] for instr, steps in instrs: if instr in dirs: waypoint_pos = step_in_dir(waypoint_pos, instr, steps) elif instr == 'F': pos[0] += waypoint_pos[0] * steps pos[1] += waypoint_pos[1] * steps elif instr == 'L': waypoint_pos = move_left(waypoint_pos, steps) elif instr == 'R': waypoint_pos = move_left(waypoint_pos, 360 - steps) return abs(pos[1]) + abs(pos[0]) if __name__ == '__main__': part1 = AdventProblem(12, 1, preprocess) part1.add_solution(part_1) part1.run() part2 = AdventProblem(12, 2, preprocess) part2.add_solution(part_2) part2.run()
while a > 1: q = a // b a, b = b, a % b x0, x1 = x1 - q * x0, x0 if x1 < 0: x1 += b0 return x1 sum = 0 prod = reduce(lambda a, b: a * b, n) for n_i, a_i in zip(n, a): p = prod // n_i sum += a_i * mul_inv(p, n_i) * p return sum % prod def part_2(inputs): time, buses = inputs modulos = [b[1] for b in buses] remainders = [b[1] - b[0] for b in buses] return chinese_remainder(modulos, remainders) if __name__ == '__main__': part1 = AdventProblem(13, 1, preprocess, 'file') part1.add_solution(part_1) part1.run() part2 = AdventProblem(13, 2, preprocess, 'file') part2.add_solution(part_2) part2.run()
bad_value_found = True break if not bad_value_found: good_tickets.append(ticket) taken = set() field_mapping = defaultdict(list) for rule in rules: for slot in range(len(my_ticket)): if slot in taken: continue if all(rule(tx[slot]) for tx in good_tickets): field_mapping[rule.field].append(slot) if len(field_mapping[rule.field]) == 1: taken.add(slot) for k, v in field_mapping.items(): print(k, v) # SOLVED BY HAND FROM HERE! if __name__ == '__main__': part1 = AdventProblem(16, 1, preprocess, "file") part1.add_solution(part_1) part1.run() part2 = AdventProblem(16, 2, preprocess, "file") part2.add_solution(part_2) part2.run()
if letter == 'B': row[0] = (row[1] + row[0] + 1) // 2 for letter in seat[-3:]: if letter == 'L': col[1] = (col[1] + col[0] - 1) // 2 if letter == 'R': col[0] = (col[1] + col[0] + 1) // 2 return row[0] * 8 + col[0] def part_1(seats): return max(convert_to_seat_id(seat) for seat in seats) def part_2(seats): seat_ids = sorted([convert_to_seat_id(seat) for seat in seats]) for s1, s2 in zip(seat_ids[:-1], seat_ids[1:]): if s2 - s1 > 1: return s1 + 1 if __name__ == '__main__': part1 = AdventProblem(5, 1, lambda x: x) part1.add_solution(part_1) part1.run() part2 = AdventProblem(5, 2, lambda x: x) part2.add_solution(part_2) part2.run()
def part_1(joltages): joltages = sorted(joltages) differences = [j2 - j1 for j1, j2 in zip(joltages[:-1], joltages[1:])] dist = Counter(differences) return (1 + dist[1]) * (1 + dist[3]) def part_2(joltages): joltages = set(joltages) target = max(joltages) @functools.lru_cache(maxsize=1024) def count_charge_paths(curr): if curr == target: return 1 total = 0 for i in range(1, 4): if (curr + i) in joltages: total += count_charge_paths(curr + i) return total return count_charge_paths(0) if __name__ == '__main__': part1 = AdventProblem(10, 1, int) part1.add_solution(part_1) part1.run() part2 = AdventProblem(10, 2, int) part2.add_solution(part_2) part2.run()
from advent import AdventProblem def preprocess(line): return line def part_1(inputs): pass def part_2(inputs): pass if __name__ == '__main__': part1 = AdventProblem(3, 1, preprocess) part1.add_solution(part_1) part1.run() # part2 = AdventProblem(3, 2, preprocess) # part2.add_solution(part_2) # part2.run()