return term[0] def part1_regex(input): # Alternative approach with regexes # works well for part 1 but hard to expand for part 2 gr = input.get_groups() rules = gr[0] messages = gr[1] rule0 = make_rule0_regex_for_pt1(rules) r = re.compile(f"^{rule0}$") valid = [msg for msg in messages if r.match(msg)] return len(valid) e.run_tests(1, part1_regex) e.run_main(1, part1_regex) def replace_rules_for_pt2(nonterm): nonterm[8] = [[42], [42, 8]] nonterm[11] = [[42, 31], [42, 11, 31]] def msg_valid(msg, term, nonterm): q = deque() q.append((0, [0])) while q: index, rules = q.popleft() rule = rules[0] if rule in term:
-2""", 0, None) e.T("""-1 -2 -3""", -6, None) e.T("""+1 -2 +3 +1""", None, 2) def part1(input): ints = [int(x) for x in input.get_valid_lines()] return sum(ints) e.run_tests(1, part1) e.run_main(1, part1) def part2(input): ints = [int(x) for x in input.get_valid_lines()] c = 0 s = set() while True: for x in ints: c += x if c in s: return c s.add(c)
def loop(start, loops): v = 1 for _ in range(loops): v = (v * start) % 20201227 return v def part1(input): ln = input.get_valid_lines() A = int(ln[0]) B = int(ln[1]) # A = 7 ** n mod 20201227 # B = 7 ** m mod 20201227 # 20201227 is prime n = get_loop_number(A) m = get_loop_number(B) print(f"Loop numbers: {n}, {m}") return loop(A, m) e.run_tests(1, part1) e.run_main(1, part1) def part2(input): pass # e.run_tests(2, part2) # e.run_main(2, part2)