""" nums = [int(i) for i in lines] return solve_part1(nums, preamble) def search(nums, target): for start in range(len(nums)): for length in range(len(nums)): my_slice = nums[start:length] if sum(my_slice) == target: return my_slice raise Exception def part2(lines, preamble=25): """ >>> part2(load_example(__file__, '9'), 5) 62 """ nums = [int(i) for i in lines] target = solve_part1(nums, preamble) result = search(nums, target) return min(result) + max(result) if __name__ == "__main__": data = load_input(__file__, 2020, "9") print(part1(data)) print(part2(data))
grid, mxy = prepare_map(lines) cx = cy = mxy direction = 0 infection_counter = 0 for _ in range(n): current = grid[cx, cy] if current == CLEAN: grid[cx, cy] = WEAKENED direction = (direction + 3) % 4 elif current == WEAKENED: grid[cx, cy] = INFECTED infection_counter += 1 elif current == INFECTED: grid[cx, cy] = FLAGGED direction = (direction + 1) % 4 elif current == FLAGGED: grid[cx, cy] = CLEAN direction = (direction + 2) % 4 else: raise dx, dy = MOVEMENTS[direction] cx += dx cy += dy return infection_counter if __name__ == "__main__": data = load_input(__file__, 2017, "22") print(part1(data)) print(part2(data))
def calc_happiness(happiness, arrangement): return happiness[(arrangement[0], arrangement[-1])] + sum( happiness[(arrangement[i - 1], arrangement[i])] for i in range(1, len(arrangement))) def run(lines, with_me): """ >>> run(load_example(__file__, '13'), False) 330 """ guests, happiness = prepare_data(lines, with_me) return max( calc_happiness(happiness, arrangement) for arrangement in permutations(guests)) def part1(lines): return run(lines, with_me=False) def part2(lines): return run(lines, with_me=True) if __name__ == "__main__": data = load_input(__file__, 2015, "13") print(part1(data)) print(part2(data))
def find_loop(commands, initial_standing): standing = list(initial_standing) cache = {} i = 0 while True: s = "".join(standing) if s in cache: # works because cache[s] is 0 return cache, i cache[s] = i standing = dance(commands, standing) i += 1 def part2(lines, initial_standing="abcdefghijklmnop", count_dances=10 ** 9): """ >>> part2(load_example(__file__, "16"), "abcde", 10) 'ceadb' """ commands = lines[0].strip().split(",") cache, s = find_loop(commands, initial_standing) return list(cache.keys())[count_dances % s] if __name__ == "__main__": data = load_input(__file__, 2017, "16") print(part1(data)) print(part2(data))
count_three += 1 return count_two * count_three def count_diff(line1, line2): count = 0 common_str = "" for i in range(len(line1)): if line1[i] != line2[i]: count += 1 else: common_str += line1[i] return count, common_str def part2(lines): """ >>> part2(load_example(__file__, '2b')) 'fgij' """ for line1, line2 in combinations(lines, 2): count, common = count_diff(line1.strip(), line2.strip()) if count == 1: return common if __name__ == "__main__": data = load_input(__file__, 2018, "2") print(part1(data)) print(part2(data))
""" return 1 + ((y - 1) * y + (x - 1) * x) // 2 + x * (y - 1) def calc_manual_numbers(n): """ >>> calc_manual_numbers(1) 20151125 >>> calc_manual_numbers(2) 31916031 >>> calc_manual_numbers(3) 18749137 >>> calc_manual_numbers(21) 33511524 """ return (BASE * pow(FACTOR, n - 1, MOD)) % MOD def part1(lines): x, y = prepare_data(lines) return calc_manual_numbers(calc_number_on(x, y)) def part2(lines): pass if __name__ == "__main__": data = load_input(__file__, 2015, "25") print(part1(data))