def part2(dpath, fname='input.txt'): particles = [ particle for particle in parse_file( utils.get_input_by_line(utils.get_fpath(dpath, fname))) ] for i in range(1000): #for p in particles: #print(p) #print() particles = [(tuple(p), tuple(v), tuple(c)) for p, v, c in map(update, particles)] #for p in particles: #print(p) #print() counter = collections.Counter([p for p, v, a in particles]) counter_not_1 = {k: v for k, v in counter.items() if v != 1} if counter_not_1: for remove_p in counter_not_1.keys(): particles = [(p, v, c) for p, v, c in particles if p != remove_p] #print('removed') return len(particles)
def part2(dpath, fname='input.txt'): bridges = [ b for b in generate_bridges( get_components(utils.get_fpath(dpath, fname))) if b ] bridges_length = {tuple(b): len(b) for b in bridges} max_length = max(bridges_length.values()) #print(max_length) bridges_max_length = { k: v for k, v in bridges_length.items() if v == max_length } #print(bridges_max_length) if len(bridges_max_length) != 1: bridges_strength = { tuple(b): get_strength(b) for b in bridges_max_length } max_strength = max(bridges_strength.values()) return max_strength #bs = {b: s for b, s in bridges_strength.items() if s == max_strength} #print(bs) else: # not really necessary, but... raise NotImplementedError
def part2(dpath, fname='input.txt', n=10000000): cluster = Cluster2(utils.get_fpath(dpath, fname)) while True: if cluster._activity == n: return cluster._infection #print(cluster) cluster.update()
def part1(dpath, fname='input.txt'): bridges = [ b for b in generate_bridges( get_components(utils.get_fpath(dpath, fname))) if b ] bridges_strength = {tuple(b): get_strength(b) for b in bridges} max_strength = max(bridges_strength.values()) return max_strength
def part1(dpath, fname='input.txt', n=10000): # activity: 10000, clean2infected: 5587, infected2clean: 4413 cluster = Cluster(utils.get_fpath(dpath, fname)) while True: if cluster._activity == n: return cluster._clean_to_infected #print(cluster) cluster.update()
def part2(dpath): # 1000 runs -- repeats every 44 moves # 32 runs == ahgpjdkcbfmneloi n = 1000000000 % 44 # 32 programs = 'abcdefghijklmnop' # TODO: find how to do this with generators for inputs in itertools.repeat( tuple(solver1.get_input(utils.get_fpath(dpath))), n): programs = solver1.solve(programs, inputs) return programs
def test_count_groups(): # arrange graph = solver.build_graph( solver.parse_lines( utils.get_input_by_line( utils.get_fpath('solutions/day12', 'test_input.txt')))) # act ngroups = solver.count_groups(graph) # assert assert ngroups == 2
def part1(dpath, fname='input.txt'): d = dict() fpath = utils.get_fpath(dpath, fname) #print(fpath) for particle in parse_file(utils.get_input_by_line(fpath)): #print(particle, distance(particle)) particle_ = particle for i in range(1000): particle_ = update(particle_) #print(particle_, distance(particle_)) d[tuple([tuple(l) for l in particle])] = distance(particle_) l = [k for k, v in d.items() if min(d.values()) == v] if len(l) == 1: p, v, a = l[0] #print('p=< {},{},{}>, v=< {},{},{}>, a=< {},{},{}>'.format(*p, *v, *a)) # p=< 642,-2979,-1903>, v=< 90,-425,-270>, a=< -11,34,23> # p=< 642,-2979,-1903>, v=< 90,-425,-270>, a=<-11,34,23> # p=<642,-2979,-1903>, v=<90,-425,-270>, a=<-11,34,23> return [p for p in parse_file(utils.get_input_by_line(fpath))].index( (list(p), list(v), list(a)))
def solver(func, dpath): return sum(1 for valid in filter(func, get_input(utils.get_fpath(dpath))))
def part1(dpath): cp = CoProcessor(debug=True) return cp.run(utils.get_fpath(dpath))
def part1(dpath): return solver1.solve('abcdefghijklmnop', solver1.get_input(utils.get_fpath(dpath)))
def part2(dpath): pruned = solver.prune_exclamations( solver.get_stream(utils.get_fpath(dpath))) return sum(count_garbage_size(pruned))
def part1(dpath): o, m = solver1(parse_input(utils.get_fpath(dpath))) return o
def part1(dpath): lengths = get_lengths(utils.get_fpath(dpath)) state = {'list': list(range(0, 256)), 'position': 0, 'skip size': 0} return solver(state, lengths)
def part2(dpath): directions = get_input(utils.get_fpath(dpath)) distances = calculate_distances(directions) return max(distances)
def part2(dpath): raise NotImplementedError # takes too long, so I need to check if there is a repetition point -- none found, need to test this better... # optimize the program -- hmmm cp = CoProcessor(debug=False) return cp.run(utils.get_fpath(dpath))
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import pytest import solutions.day25.solver as solver import solutions.utils as utils @pytest.mark.parametrize('test_input, test_output', [ ( utils.get_input_by_line(utils.get_fpath('solutions/day25', 'test_input.txt')), { 'tape': {}, 'cursor': 0, 'states': { 'A': {0: (1, 1, 'B'), 1: (0, -1, 'B')}, 'B': {0: (1, -1, 'A'), 1: (1, 1, 'A')}, }, 'steps': 6, 'current state': 'A', } ), ]) def test_parse_blueprint(test_input, test_output): assert solver.parse_blueprint(test_input) == test_output @pytest.mark.parametrize('test_input, test_output', [ ( {
def part1(dpath): lines = utils.get_input_by_line(utils.get_fpath(dpath)) machine = parse_blueprint(lines) return execute(machine)
def _run(dpath, func): return func(_get_input(utils.get_fpath(dpath)))
def part1(dpath): directions = get_input(utils.get_fpath(dpath)) dist = calculate_distance(directions) return dist
def part2(dpath): return solver2(utils.get_input_by_line(utils.get_fpath(dpath)))
def part1(dpath): groups = prune_garbage( prune_exclamations(get_stream(utils.get_fpath(dpath)))) return sum(score_groups(groups))
def part1(dpath): graph = build_graph( parse_lines(utils.get_input_by_line(utils.get_fpath(dpath)))) connected = check_connections(graph) return sum(True for k, v in connected.items() if v)
def part2(dpath): return solver2(list(parse_input(utils.get_fpath(dpath))))
def part1(dpath): return solver1(parse_input(utils.get_fpath(dpath)))