def main(): lines = utils.read_input('input18.txt') area = Area(lines) simulation = Simulation(area) values = [] n = 700 # This should be enough to find a period. for second in range(n): trees = area.number_of(Area.TREES) lumberyards = area.number_of(Area.LUMBERYARD) values.append(trees * lumberyards) simulation.tick() print('Part 1:', values[10]) # Not very efficient and robust but functional period finder. period = 1 while True: if values[-period:] == values[-2 * period:-period]: break period += 1 print(period) print(values[-period:][(1000000000 - n) % period]) pylab.plot(values, 'k.') pylab.show()
def main(): player_count, last_marble = [ int(s) for s in utils.read_input('input9.txt')[0].split() if s.isdigit() ] game = Game(player_count) game.play_until(last_marble) print(game.high_score) game = Game(player_count) game.play_until(100 * last_marble) print(game.high_score)
def __init__(self, file_name): pattern = r'Step ([A-Z]) must be finished before step ([A-Z]) can begin.' expr = re.compile(pattern) graph = defaultdict(lambda: {'succ': set(), 'prec': set()}) for line in utils.read_input(file_name): match = expr.match(line) node_source, node_target = match.groups() graph[node_source]['succ'].add(node_target) graph[node_target]['prec'].add(node_source) self._graph = graph self._order = [] self._processed = set() self._todo = set(node for node in graph if len(graph[node]['prec']) == 0)
def __init__(self): lines = utils.read_input('input10.txt') num_points = len(lines) self._x = numpy.zeros(num_points, dtype=numpy.int) self._y = numpy.zeros(num_points, dtype=numpy.int) self._vx = numpy.zeros(num_points, dtype=numpy.int) self._vy = numpy.zeros(num_points, dtype=numpy.int) expr = re.compile(r'^position=<(.*)> velocity=<(.*)>$') for i, line in enumerate(lines): match = expr.match(line) position = [i for i in match.groups()[0].split(',')] velocity = [int(i) for i in match.groups()[1].split(',')] self._x[i], self._y[i] = position self._vx[i], self._vy[i] = velocity self._step_count = 0
def main(): lines = utils.read_input('input13.txt') map = Map(lines) traffic = Traffic(map, lines) while True: try: while True: traffic.tick() except CrashException as crash: print('Crash at ({0},{1})'.format(crash.col, crash.row)) for cart in crash.carts: traffic.remove(cart) if len(traffic.carts) == 1: if not traffic.tick_finished: traffic.tick() print('Last standing cart at ({0},{1})'.format( traffic.carts[0].col, traffic.carts[0].row)) break
def main(): lines = utils.read_input('input12.txt') initial_state = parse.compile('initial state: {}').parse(lines[0])[0] plants = set(i for i, c in enumerate(initial_state) if c == '#') patterns = set() parser = parse.compile("{} => {}") for note in lines[1:]: result = parser.parse(note) if result[1] == '#': patterns.add(tuple(i-2 for i, c in enumerate(result[0]) if c == '#')) prev_diff, curr_diff = None, None prev_sum, curr_sum = None, sum(plants) generation = 1 while generation != 1000: new_plants = set() for pot in range(min(plants) - 2, max(plants) + 3): pattern = tuple(i for i in range(-2, 3) if i+pot in plants) if pattern in patterns: new_plants.add(pot) plants = new_plants if generation == 20: print('Part 1:', sum(plants)) prev_sum, curr_sum = curr_sum, sum(plants) prev_diff, curr_diff = curr_diff, curr_sum - prev_sum if prev_diff == curr_diff: break generation += 1 diff = curr_diff future_sum = curr_sum + diff * (50000000000 - generation) print('Part 2:', future_sum)
def main(): freq_changes = [int(line) for line in utils.read_input('input1.txt')] print('Part 1:', final_frequency(freq_changes)) print('Part 2:', repeated_frequency(freq_changes))
def main(): ids = utils.read_input('input2.txt') print('Part 1:', checksum(ids)) print('Part 2:', correct(ids))
def main(): data = deque(int(i) for i in utils.read_input('input8.txt')[0].split()) root = make_tree(data) print(meta_sum(root)) print(node_value(root))