2016-12-09 - Explosives in Cyberspace
(original .ipynb)puzzle_input_str = open("puzzle_input/day9.txt").read() def parse_token(chars): c = chars.pop(0) if c != "(": return 1 count = [] while c != "x": count.append(c) c = chars.pop(0) times = [] while c != ")": times.append(c) c = chars.pop(0) count = int("".join(count[1:])) times = int("".join(times[1:])) n = 0 while n < count: chars.pop(0) n += 1 return count * times def part_one(input_str): pattern = [] chars = list(input_str) count = 0 while len(chars) > 0: count += parse_token(chars) return count print("part one:", part_one(puzzle_input_str))
part one: 115118
def chomp(message, n): submessage = [] while n > 0: submessage.append(message.pop(0)) n -= 1 return submessage def decompress_v2(message): n = 0 while len(message) > 0: c = message.pop(0) if c == "(": count = [] while c != "x": count.append(c) c = message.pop(0) times = [] while c != ")": times.append(c) c = message.pop(0) count = int("".join(count[1:])) times = int("".join(times[1:])) n += times * decompress_v2(chomp(message, count)) else: n += 1 return n def part_two(input_str): return decompress_v2(list(input_str)) assert 9 == part_two("(3x3)XYZ") assert 20 == part_two("X(8x2)(3x3)ABCY") assert 241920 == part_two("(27x12)(20x12)(13x14)(7x10)(1x12)A") assert 445 == part_two("(25x3)(3x3)ABC(2x3)XY(5x2)PQRSTX(18x9)(3x2)TWO(5x7)SEVEN") print("part two:", part_two(puzzle_input_str))
part two: 11107527530