2015-12-19 - Medicine for Rudolph
(original .ipynb)puzzle_input_str = open("./puzzle_input/day19.txt").read() test_input_str = """H => HO H => OH O => HH HOH""" def parse_rule(rule_str): tokens = rule_str.split(" ") return tokens[0], tokens[2] def modifications(source, replacement, molecule): source_len = len(source) for i in range(1+len(molecule)-source_len) : if source == molecule[i:i+source_len]: yield molecule[:i] + replacement + molecule[i+source_len:] def part_one(input_str): rules_str, molecule = input_str.split("\n\n") rules = [parse_rule(rule_str) for rule_str in rules_str.split("\n")] molecules = set() for source, replacement in rules: for modified_molecule in modifications(source, replacement, molecule): molecules.add(modified_molecule) return len(molecules) assert 4 == part_one(test_input_str) print("part one:", part_one(puzzle_input_str))
part one: 535
def part_two(input_str): pass # print("part two:", part_two(puzzle_input_str))