示例#1
0
def part_a():
    pz_input = ReadInput(3).data
    wire_segments = []
    min_dist = None
    for wire in pz_input:
        x = 0
        y = 0
        new_segments = []
        moves = wire.split(",")
        for move in moves:
            if move == "":
                continue
            new_x = x
            new_y = y
            if move[0] == "R":
                new_x += int(move[1:])
            elif move[0] == "L":
                new_x -= int(move[1:])
            elif move[0] == "U":
                new_y += int(move[1:])
            elif move[0] == "D":
                new_y -= int(move[1:])
            new_segments.append((x, y, new_x, new_y))
            x = new_x
            y = new_y
        wire_segments.append(new_segments)
    for i in wire_segments[0]:
        for j in wire_segments[1]:
            does_intersect, dist, point = check_intersect(i, j)
            if does_intersect and (min_dist is None or dist < min_dist) and dist > 0:
                min_dist = dist
    return min_dist
示例#2
0
def part_b():
    pz_input = ReadInput(3).data
    wire_segments = []
    min_dist = None
    for wire in pz_input:
        x = 0
        y = 0
        new_segments = []
        moves = wire.split(",")
        for move in moves:
            if move == "":
                continue
            new_x = x
            new_y = y
            if move[0] == "R":
                new_x += int(move[1:])
            elif move[0] == "L":
                new_x -= int(move[1:])
            elif move[0] == "U":
                new_y += int(move[1:])
            elif move[0] == "D":
                new_y -= int(move[1:])
            new_segments.append((x, y, new_x, new_y))
            x = new_x
            y = new_y
        wire_segments.append(new_segments)
    for i in wire_segments[0]:
        for j in wire_segments[1]:
            does_intersect, dist, point = check_intersect(i, j)
            if does_intersect:
                wire_1 = 0
                wire_2 = 0
                for m in wire_segments[0]:
                    if m[0] == m[2] and m[2] == point[0] and min(m[1], m[3]) <= point[1] <= max(m[1], m[3]):
                        wire_1 += abs(point[1] - m[1])
                        break
                    elif m[1] == m[3] and m[3] == point[1] and min(m[0], m[2]) <= point[0] <= max(m[0], m[2]):
                        wire_1 += abs(point[0] - m[0])
                        break
                    else:
                        wire_1 += abs(m[2] - m[0]) + abs(m[3] - m[1])
                for n in wire_segments[1]:
                    if n[0] == n[2] and n[2] == point[0] and min(n[1], n[3]) <= point[1] <= max(n[1], n[3]):
                        wire_2 += abs(point[1] - n[1])
                        break
                    elif n[1] == n[3] and n[3] == point[1] and min(n[0], n[2]) <= point[0] <= max(n[0], n[2]):
                        wire_2 += abs(point[0] - n[0])
                        break
                    else:
                        wire_2 += abs(n[2] - n[0]) + abs(n[3] - n[1])
                if min_dist is None or wire_1 + wire_2 < min_dist:
                    min_dist = wire_1 + wire_2
    return min_dist
示例#3
0
from PuzzleInput import ReadInput
import math
from collections import defaultdict

pz_input = ReadInput(14).data
# pz_input = ["157 ORE => 5 NZVS", "165 ORE => 6 DCFZ", "44 XJWVT, 5 KHKGT, 1 QDVJ, 29 NZVS, 9 GPVTF, 48 HKGWZ => 1 FUEL", "12 HKGWZ, 1 GPVTF, 8 PSHF => 9 QDVJ", "179 ORE => 7 PSHF", "177 ORE => 5 HKGWZ", "7 DCFZ, 7 PSHF => 2 XJWVT", "165 ORE => 2 GPVTF", "3 DCFZ, 7 NZVS, 5 HKGWZ, 10 PSHF => 8 KHKGT"]
# pz_input = ["2 VPVL, 7 FWMGM, 2 CXFTF, 11 MNCFX => 1 STKFG", "17 NVRVD, 3 JNWZP => 8 VPVL", "53 STKFG, 6 MNCFX, 46 VJHF, 81 HVMC, 68 CXFTF, 25 GNMV => 1 FUEL", "22 VJHF, 37 MNCFX => 5 FWMGM", "139 ORE => 4 NVRVD", "144 ORE => 7 JNWZP", "5 MNCFX, 7 RFSQX, 2 FWMGM, 2 VPVL, 19 CXFTF => 3 HVMC", "5 VJHF, 7 MNCFX, 9 VPVL, 37 CXFTF => 6 GNMV", "145 ORE => 6 MNCFX", "1 NVRVD => 8 CXFTF", "1 VJHF, 6 MNCFX => 4 RFSQX", "176 ORE => 6 VJHF"]
# pz_input = ["171 ORE => 8 CNZTR", "7 ZLQW, 3 BMBT, 9 XCVML, 26 XMNCP, 1 WPTQ, 2 MZWV, 1 RJRHP => 4 PLWSL", "114 ORE => 4 BHXH", "14 VRPVC => 6 BMBT", "6 BHXH, 18 KTJDG, 12 WPTQ, 7 PLWSL, 31 FHTLT, 37 ZDVW => 1 FUEL", "6 WPTQ, 2 BMBT, 8 ZLQW, 18 KTJDG, 1 XMNCP, 6 MZWV, 1 RJRHP => 6 FHTLT", "15 XDBXC, 2 LTCX, 1 VRPVC => 6 ZLQW", "13 WPTQ, 10 LTCX, 3 RJRHP, 14 XMNCP, 2 MZWV, 1 ZLQW => 1 ZDVW", "5 BMBT => 4 WPTQ", "189 ORE => 9 KTJDG", "1 MZWV, 17 XDBXC, 3 XCVML => 2 XMNCP", "12 VRPVC, 27 CNZTR => 2 XDBXC", "15 KTJDG, 12 BHXH => 5 XCVML", "3 BHXH, 2 VRPVC => 7 MZWV", "121 ORE => 7 VRPVC", "7 XCVML => 6 RJRHP", "5 BHXH, 4 VRPVC => 5 LTCX"]
# pz_input = ["9 ORE => 2 A", "8 ORE => 3 B", "7 ORE => 5 C", "3 A, 4 B => 1 AB", "5 B, 7 C => 1 BC", "4 C, 1 A => 1 CA", "2 AB, 3 BC, 4 CA => 1 FUEL"]

reactions = {}
for reaction in pz_input:
    if reaction == "":
        continue
    sides = reaction.split(" => ")
    ingredients = sides[0].split(", ")
    quantities = []
    for i in ingredients:
        stuff = i.split(" ")
        quantities.append((int(stuff[0]), stuff[1]))
    outputs = sides[1].split(" ")
    reactions[outputs[1]] = [int(outputs[0]), quantities]


def find_ore_cost(num_fuel):
    required = defaultdict(lambda: 0)
    extra = defaultdict(lambda: 0)
    required["FUEL"] = num_fuel  # 1877913
    finished = False
    while not finished:
        finished = True
示例#4
0
from PuzzleInput import ReadInput
import time
import numpy as np
from collections import defaultdict
import collections

pz_input = np.array([np.array(list(x)) for x in ReadInput(18).data])
# pz_input = np.array([list(x) for x in ["#########", "#[email protected]#", "#########"]])
# pz_input = np.array([list(x) for x in ["########################", "#[email protected].#", "######################.#", "#d.....................#", "########################"]])
# pz_input = np.array([list(x) for x in ["########################", "#...............b.C.D.f#", "#.######################", "#[email protected]#", "########################"]])
# pz_input = np.array([list(x) for x in ["#################", "#i.G..c...e..H.p#", "########.########", "#j.A..b...f..D.o#", "########@########", "#k.E..a...g..B.n#", "########.########", "#l.F..d...h..C.m#", "#################"]])
# pz_input = np.array([list(x) for x in ["########################", "#@..............ac.GI.b#", "###d#e#f################", "###A#B#C################", "###g#h#i################", "########################"]])
# pz_input = np.array([list(x) for x in ["#################################################################################", "#.......#...#...#...........#.....#...#.#.....#m......#.......#.....#........u..#", "#####.#.#.#.#.#.###.#######.###.#.#.#.#.#.###.###.#####.#.###.###.#.###########.#", "#.....#...#...#.....#.#.....#...#...#...#.#....e#.......#...#.....#.....#.......#", "#.###################.#.#####.#########.#.#####.###########.###########.#.#######", "#.#...#...#...........#.#.....#.......#.#.....#.....#.....#...#.......#.#.#.....#", "#.#S#.###.#.#####.#####.#.###.#.###.###.#####.#####.#.###.###.#.#.#####.#.#.###.#", "#.#.#...#.#...#.#.......#.#.#.#.#...#...#.D.#.#...#...#.#.#...#.#.#.....#j..#...#", "#.#.###.#.###.#.#########.#.#.###.###.###.#.#.#.#.#####.#.#.###.###.#########.###", "#.#...#.#...#...#.#.........#...#.#...#.#.#...#.#.....#...#...#...#.#.#.....#...#", "#.###.#.###.###.#.#.#########.#.#.#.###.#.#####.#####.#.#####.###.#.#.#.#.#.###.#", "#...#.#c..#.....#.......#...#.#.#.#.#...#...#.....#.#.#.#.....#...#.#...#.#...#.#", "#.###.###.#.#############.#.###.#.#.#.#.###.#####.#.#.#.#.#####.###.#.###.###.#.#", "#.#...#.#.#.........#.....#...#.#.....#.#.#...#.#...#.#...#...#.....#.#...#...K.#", "#.#.###.#.#########.#.#######.#.#########.###.#.###.#.#####.#.#.#######.#########", "#.......#.#.......#h..#.....#.#.........#...#.#.....#.......#...#.....#.#...#...#", "#.#######.#O#####.#######.#.#.#.#######.###.#.#.#######.#######.#.###F#.#.#.#.#.#", "#.#....y..#...#...#.....#.#.#.#.#.#.....#...#.#.#.....#.#.....#...#.#.#...#.#.#.#", "###.#####.###.#.###.###.#.###.#.#.#.###.#.#.#.###.###.#.#.###.#####.#.#.###.#.#.#", "#...#.....#.#.#...#.#.Z.#...#.#.#.#.#...#.#.#.....#.#.#.#.#.#...#.....#...#...#.#", "#.###.#####.#.###A#.#.###.#.#.#.#.#.#####.#########.#.#.#.#.###.#.#########.###.#", "#.#.#.#.....#.#.#...#.#...#...#...#.....#.#.........#.#.#.#.#...#.#.......#.#...#", "#.#.#.#####.#.#.#######.#########.#####.#.#.#####.#.#.#.#.#.#####.#.###.#.###.#.#", "#.#.#...#...#.#...#.....#.....#.......#.#.#.#.#...#.#.#.#.#.....#.#.#...#.....#.#", "#.#.###.#.#.#.###.#.#######.#.#####.###.#.#.#.#.#####.#.#.#.#.###.###.#########X#", "#.#...#...#.#...#.#...#.....#.....#.#...#.....#.#...#.#.#.#.#...#...#.....#.....#", "#.#W#.#####.###N#.###.#.#.#######.###.#.#######.#.#.#.###.#.###.###.#####.###.###", "#i#.#.....#.#.#.#...#.#.#.#.....#.....#.#..r..#...#.#...#.#.#.....#.#...#...#.#.#", "#.#######.#.#.#.#.###.###.#.###########.#.###.#.###.###.#.###.#####.#.#.###.#.#.#", "#.......#...#.#.#...#...#.#.............#.#.#.#.#.#...#...#...#.....#.#.....#.#.#", "#.#####.#.###.#.#.#.###.#.#.#############.#.#.#.#.###.#####.###.#####.#######.#.#", "#.#.#...#.....#.#.#...#..t#...#.#.......#.#.#.#...#.....#.....#...#...#.....#.#.#", "#.#.#.#######.#.#.###.#######.#.#.#.#####.#.#.#.###.###.#####.###.###.#.###.#.#.#", "#...#.#...#...#.#...#.....#.#.#...#.....#.#.#.#.#...#.........#.#...#...T.#.#...#", "###.#.#.#.#.###.###.#.###.#.#.#########.#.#.#.###.###########.#.###.#####.#.###.#", "#...#...#.#.#...#...#...#...#.......#...#.#.#...#.#.....#...#...#...#...#.#...#.#", "#.#######.###.###.#####.###########.#.###.#.###.#.#.###.#.#.###.#.###.#.#####.###", "#.#...#v#...#.#.#.....#.........#...#...#.#.......#...#..l#...#.#.....#.....#.P.#", "#.#.#.#.###.#.#.#####.#########.#.#####.#.###########.#######.#############.###.#", "#...#.....#...#...............#.............................#....b..............#", "#######################################.@.#######################################", "#...#.#.........#.....#...........#...........#...#.....#.......#...#.....Q.#...#", "#.#.#.#.#####.###.#.#.#.#####.###.#.###.#.###.#.#.#.###.#.#####.#.#.#.#.###.###.#", "#.#.#.#.#...#.....#.#.#.#.#...#...#...#.#.#.....#.#...#...#.....#.#.#.#...#.#...#", "#.#.#.#.#.#.#######.###.#.#.#####.###.#.#.#######.###.#####.#####.#.#.###.#.#.#.#", "#.#...#.#.#.....#.#.#.B...#.....#.#...#.#.....#.....#.#...#.......#.#.#...#...#.#", "#.#####.#.#####.#.#.#.#######.#.###.###.#.###.###.###.#.#####.#####.#.#.#######.#", "#.#...#q#..f#.....#...#.....#.#...#.#...#.#.#...#.#.....#...#.#...#...#.#.....#.#", "#.#.#.#.#.#.#######.###.###.###.#.#.#.###.#.###.###.#####.#.#.###.#####.#.###.#.#", "#...#.#.#.#...#...#p..#.#.#...#.#...#...#.....#...#.#.....#.#.....#...#...#...#.#", "#####.#.#####.#.#.###.#.#.###.#########.#####.###.#.#.#####.#####.###.#####.###.#", "#.....#.......#.#.#...#.#...#.#.......#.#...#.#.#...#.#...#.....#.....#.#...#.#.#", "#.###########.#.#.#####.###.#L#.#####R#.#.#.#.#.#####.#.#.#####.#####.#.#.###.#.#", "#...........#.#.#.......#...#...#...#...#.#.#.....#...#.#.....#...#...#.#...#...#", "#######.###.#.#.#########.#.#####.#.#######.#####.#.#######.#.###.#.###.###.###.#", "#.......#...#.#.#.........#.......#.....#...#.....#.#.....#.#...#.#...#...#...#.#", "#.#######.###.#.#.###.#################.#.###.#####.#.#.#.###.###.###.###.###.#.#", "#.#.........#.#.#...#.............#.....#.........#.#.#.#.....#...#.#.......#.#.#", "#.###.#######.#.###########.#####.#.###############.###.#####.#.###.#.#####.#.###", "#...#.#.......#.#.........#.#...#.#.#...#.......#...#...#.#...#.#.....#...#.#...#", "#.#G###.#######.#.#######.#.###.#.###.#.#.#####.#.###.###.#.###.#######.#.#####.#", "#.#...#....x#.#.#.#z#.....#.....#.....#.#.....#...#...#...#.#...#.......#.#...#.#", "#.###.#####.#.#.#.#.#########.#########.#####.#######.#.###.#####.#######.#.#.#.#", "#k#.#.....#...#...#.#.....#...#.......#.#...#.#.......#.#...#.....#.#...#...#.#.#", "#.#.#####.###.#####.#.###.#.###.#####.#.#.#.#.#.#.#####.#.###.#####.#.#.#####.#H#", "#.#.....#...#.....#.#...#.#.#...#...#...#.#...#.#.#.....#.#...#...#...#...#.#.#.#", "#.#.###.###.#####.#.###.#.#.#.###.#.###.#.#######.#.###.#.#.###.#.#.###.#Y#.#.#.#", "#...#...#.#...#...#.....#..o#.#.#.#.....#.#.......#.#...#...#...#...#...#...#..g#", "#####.###.#.###.#######.#####.#.#.#######.#####.###.###.#############.#####.###.#", "#...#.#...#.....#.....#.....#.#.#...#...#.......#.....#...#...........#.#...#...#", "#.#.#.###.#######.###.#######.#.###.#.###########.###.###.#.###########.#.###.###", "#.#.#.#.....#.#.....#.#.......#...#.#...#...#...#...#.#...#.......#...#.I.#.#...#", "#.#.#.#.###.#.#.###.#.#.#######.#.#.###.#.###.#.#.###.#.#########.###.#.###.###.#", "#.#.#.#.#...#s....#.#.#.......#a#.#.....#.#...#.#.#...#.....#.....#...#.....#.#.#", "#.###M#U###.#####.#.#.###.###.###.#####.#.#.###.#.#.#.#####.#.#####.#V#####.#.#.#", "#.....#...#...#...#.#...#...#.#...#....n#.#...#d..#.#.#.J.#...#.....#...#..w#.#.#", "#.#######.###.#####.###.#####.#.###.#####.###.#####.#.#.#.###.#####.#####.###.#.#", "#.......#.#.#...#...#.#.....#.#...#.#...#...#.#.#...#.#.#.#...#...#...........#.#", "#######.#.#.###E#.###.#####.#.#.#.#.###.#.###.#.#.#####.#.#####.#.#############.#", "#.........#.......#.........C.#.#.......#.......#.......#.......#...............#", "################################################################################"]])

# np.set_printoptions(edgeitems=85, linewidth=100000)
# print("\n".join([row.tostring().decode("utf-8") for row in pz_input]).replace(".", " "))


def find_best_path(pf):
    for key in pf.get_remaining_keys():
        new_child = pf.copy()
        made_it = new_child.path_to(key)
        if made_it:
            find_best_path(new_child)


def get_dist_to(curr, finish):
    return abs(curr[0] - finish[0]) + abs(curr[1] - finish[1])


def dist_between(start, finish, map):
示例#5
0
from PuzzleInput import ReadInput

pz_input = ReadInput(7).data


def part_a():
    rules = {}
    for rule in pz_input:
        if rule == "":
            continue
        pieces = rule.split(" -> ")
        logic = pieces[0].split(" ")
        if len(logic) == 1:
            rules[pieces[1]] = [logic[0]]
        elif len(logic) == 2:
            rules[pieces[1]] = [logic[1], "NOT"]
        elif len(logic) == 3:
            rules[pieces[1]] = [logic[0], logic[1], logic[2]]
        else:
            raise ValueError("Invalid Length:", logic)
    while True:
        for k in rules:
            v = rules[k]
            if len(v) == 1:
                if isinstance(v[0], str) and v[0].isnumeric():
                    rules[k] = [int(v[0])]
                elif isinstance(v[0], str) and len(
                        rules[v[0]]) == 1 and isinstance(rules[v[0]][0], int):
                    rules[k] = [rules[v[0]][0]]
            elif len(v) == 2:
                if len(rules[v[0]]) == 1 and isinstance(rules[v[0]][0],
示例#6
0
from PuzzleInput import ReadInput
from collections import defaultdict

pz_input = ReadInput(13).data


def part_a():
    modifiers = defaultdict(lambda: {})
    for case in pz_input:
        if case == "":
            continue
        pieces = case.split(" ")
        modifiers[pieces[0]][pieces[10][:-1]] = int(pieces[3]) * (1 if pieces[2] == "gain" else -1)
    permutations = []
    current_perm = ["Alice"]
    while len(current_perm) < len(modifiers):
        for person in modifiers:
            if person not in current_perm:
                new_perm = current_perm.copy()
                new_perm.append(person)
                permutations.append(new_perm)
        current_perm = permutations.pop(0)
    change = []
    for perm in permutations:
        val = 0
        for i in range(len(perm)):
            val += modifiers[perm[i]][perm[(i + 1) % len(modifiers)]]
            val += modifiers[perm[i]][perm[(i - 1) % len(modifiers)]]
        change.append(val)
    return max(change)
示例#7
0
from PuzzleInput import ReadInput
from collections import defaultdict

pz_input = ReadInput(9).data[0].split(",")


class Intcode:
    logic = []

    def __init__(self, logic):
        self.logic = defaultdict(lambda: 0)
        for i in range(len(logic)):
            self.logic[i] = int(logic[i])
        self.working_copy = self.logic.copy()
        self.i = 0
        self.relative_base = 0

    def reset(self):
        self.working_copy = self.logic.copy()
        self.i = 0

    def get_value(self, logic, mode, i):
        if mode == 0:
            return logic[logic[i]]
        elif mode == 1:
            return logic[i]
        elif mode == 2:
            return logic[self.relative_base + logic[i]]

    def run(self, input):
        a_input = self.working_copy
示例#8
0
from PuzzleInput import ReadInput
import networkx as nx

pz_input = ReadInput(20).data


def is_letter(char):
    return ord(char) in range(ord("A"), ord("Z"))


def part_a():
    graph = nx.Graph()
    portals = {}
    for row in range(len(pz_input)):
        if pz_input[row] == "":
            continue
        for col in range(len(pz_input[row])):
            if pz_input[row][col] == "." and (row, col) not in graph:
                graph.add_node((row, col))
            elif is_letter(pz_input[row][col]):
                # Check down
                if is_letter(
                        pz_input[row + 1][col]) and pz_input[row +
                                                             2][col] == ".":
                    if (row + 2, col) not in graph:
                        graph.add_node((row + 2, col))
                    portal = pz_input[row][col] + pz_input[row + 1][col]
                    if portal in portals:
                        graph.add_edge((row, col), portals[portal])
                        del portals[portal]
                    else:
示例#9
0
from PuzzleInput import ReadInput
from collections import defaultdict
import numpy as np

pz_input = [int(x) for x in ReadInput(17).data[0].split(",")]


class Intcode:
    logic = []

    def __init__(self, logic):
        self.logic = defaultdict(lambda: 0)
        for i in range(len(logic)):
            self.logic[i] = int(logic[i])
        self.working_copy = self.logic.copy()
        self.i = 0
        self.relative_base = 0
        self.input_idx = 0

    def reset(self):
        self.working_copy = self.logic.copy()
        self.i = 0

    def get_value(self, logic, mode, i):
        if mode == 0:
            return logic[logic[i]]
        elif mode == 1:
            return logic[i]
        elif mode == 2:
            return logic[self.relative_base + logic[i]]
示例#10
0
from PuzzleInput import ReadInput

pz_input = ReadInput(2).data[0].split(",")

# Part 1
# pz_input[1] = 12
# pz_input[2] = 2
# for i in range(0, len(pz_input), 4):
#     command = int(pz_input[i])
#     if command == 99:
#         break
#     loc_1 = int(pz_input[i + 1])
#     loc_2 = int(pz_input[i + 2])
#     loc_3 = int(pz_input[i + 3])
#     pz_input[loc_3] = (int(pz_input[loc_1]) + int(pz_input[loc_2])) if command == 1 else (int(pz_input[loc_1]) * int(pz_input[loc_2]))

# print(pz_input)

# Part 2
for n in range(0, 100):
    for v in range(0, 100):
        cp_input = pz_input.copy()
        cp_input[1] = n
        cp_input[2] = v
        for i in range(0, len(cp_input), 4):
            command = int(cp_input[i])
            if command == 99:
                break
            loc_1 = int(cp_input[i + 1])
            loc_2 = int(cp_input[i + 2])
            loc_3 = int(cp_input[i + 3])
示例#11
0
from PuzzleInput import ReadInput
import numpy as np

pz_input = ReadInput(12).data


class Moon:
    def __init__(self, x, y, z):
        self.location = np.array([x, y, z])
        self.initial_location = np.array([x, y, z])
        self.velocity = np.array([0, 0, 0])
        self.initial_velocity = np.array([0, 0, 0])
        self.diffs = np.array([0, 0, 0])

    def compare_with(self, moon):
        self.diffs += (moon.location > self.location).astype(int)
        self.diffs -= (moon.location < self.location).astype(int)

    def apply_diffs(self):
        self.velocity += self.diffs
        self.diffs = np.array([0, 0, 0])

    def move(self):
        self.location += self.velocity
        return np.array(
            np.logical_and(self.location == self.initial_location,
                           self.velocity == self.initial_velocity))

    def calculate_energy(self):
        return np.absolute(self.location).sum() * np.absolute(
            self.velocity).sum()
示例#12
0
from PuzzleInput import ReadInput
import numpy as np

pz_input = ReadInput(6).data


def part_a():
    grid = np.ones((1000, 1000))
    grid *= -1

    for instruction in pz_input:
        bits = instruction.split(" ")
        if bits[0] == "turn":
            corner_1 = bits[2].split(",")
            corner_2 = bits[4].split(",")
            grid[int(corner_1[0]):int(corner_2[0]) + 1,
                 int(corner_1[1]):int(corner_2[1]) +
                 1] = 1 if bits[1] == "on" else -1
        elif bits[0] == "toggle":
            corner_1 = bits[1].split(",")
            corner_2 = bits[3].split(",")
            grid[int(corner_1[0]):int(corner_2[0]) + 1,
                 int(corner_1[1]):int(corner_2[1]) + 1] *= -1
    grid += 1
    return np.count_nonzero(grid)


def part_b():
    grid = np.zeros((1000, 1000))

    for instruction in pz_input:
示例#13
0
from PuzzleInput import ReadInput
from collections import defaultdict

pz_input = ReadInput(9).data


def part_a():
    routes = defaultdict(lambda: {})
    for route in pz_input:
        if route == "":
            continue
        pieces = route.split(" = ")
        dist = int(pieces[1])
        destinations = pieces[0].split(" to ")
        routes[destinations[0]][destinations[1]] = dist
        routes[destinations[1]][destinations[0]] = dist
    paths = [([key], 0) for key in routes.keys()]
    finished_paths = []
    while len(paths) > 0:
        current_path = paths.pop()
        path = current_path[0]
        dist = current_path[1]
        for key in routes[path[-1]]:
            if key not in path:
                new_path = path.copy()
                new_path.append(key)
                new_dist = dist + routes[path[-1]][key]
                if len(new_path) == len(routes):
                    finished_paths.append((new_path, new_dist))
                else:
                    paths.append((new_path, new_dist))
示例#14
0
from PuzzleInput import ReadInput

pz_input = ReadInput(3).data[0]


def part_a():
    visited = [(0, 0)]
    x = 0
    y = 0
    for move in pz_input:
        if move == "^":
            y += 1
        elif move == "v":
            y -= 1
        elif move == ">":
            x += 1
        elif move == "<":
            x -= 1
        else:
            raise IndexError(move)
        if (x, y) not in visited:
            visited.append((x, y))
    return len(visited)


def part_b():
    visited = [(0, 0)]
    loc = [0, 0, 0, 0]
    santa = True
    for move in pz_input:
        if move == "^":
示例#15
0
from PuzzleInput import ReadInput

pz_input = ReadInput(1).data[0]


def part_a():
    floor = 0
    for char in pz_input:
        if char == ")":
            floor -= 1
        elif char == "(":
            floor += 1
    return floor


def part_b():
    floor = 0
    for i in range(len(pz_input)):
        if pz_input[i] == ")":
            floor -= 1
        elif pz_input[i] == "(":
            floor += 1
        if floor == -1:
            return i + 1
    return floor


if __name__ == "__main__":
    print("Part A", part_a())
    print("Part B", part_b())
示例#16
0
from PuzzleInput import ReadInput

pz_input = ReadInput(5).data


def part_a():
    nice = 0
    for kid in pz_input:
        vowels = len([letter for letter in kid if letter in ["a", "e", "i", "o", "u"]])
        double = False
        for i in range(len(kid)-1):
            if kid[i] == kid[i+1]:
                double = True
                break
        forbidden = False
        for i in range(len(kid)-1):
            if kid[i:i+2] in ["ab", "cd", "pq", "xy"]:
                forbidden = True
                break
        if vowels >= 3 and double and not forbidden:
            nice += 1
    return nice


def part_b():
    nice = 0
    for kid in pz_input:
        repeat = False
        for i in range(len(kid)-2):
            if kid[i:i+2] in kid[i+2:]:
                repeat = True
示例#17
0
from PuzzleInput import ReadInput

pz_input = ReadInput(8).data


def part_a():
    code_total = 0
    for line in pz_input:
        i = 0
        while i < len(line):
            if line[i] == "\"":
                code_total += 1
                i += 1
            elif line[i] == "\\":
                if line[i + 1] == "x":
                    code_total += 3
                    i += 4
                else:
                    code_total += 1
                    i += 2
            else:
                i += 1
    return code_total


def part_b():
    # pz_input = ["\"\"", "\"abc\"", "\"aaa\\\"aaa\"", "\"\\x27\""]
    total = 0
    for line in pz_input:
        if line == "":
            continue
示例#18
0
from PuzzleInput import ReadInput
import re

pz_input = [x for x in ReadInput(19).data if len(x) > 0]


def try_replace(string, conversions):
    conversions.sort(key=lambda c: len(c[0]), reverse=True)
    for conv in conversions:
        all_matches = [(x.start(0), x.end(0))
                       for x in re.finditer(conv[0], string)]
        if len(all_matches) == 0:
            continue
        for match in all_matches:
            new_molecule = string[:match[0]] + conv[1] + string[match[1]:]
            if new_molecule == "e":
                return 0
            else:
                result = try_replace(new_molecule, conversions)
                if result >= 0:
                    return result + 1
    return -1


def part_a():
    conversions = [x.split(" => ") for x in pz_input[:-1]]
    molecule = pz_input[-1]
    results = []
    for conv in conversions:
        all_matches = [(x.start(0), x.end(0))
                       for x in re.finditer(conv[0], molecule)]
示例#19
0
from PuzzleInput import ReadInput
import networkx as nx

pz_input = ReadInput(22).data


def deal_new(stack):
    stack.reverse()
    return stack


def cut_n(stack, n):
    first = stack[n:]
    second = stack[:n]
    return first + second


def increment_n(stack, n):
    new_stack = [0] * len(stack)
    for i in range(len(stack)):
        new_stack[(i*n) % len(stack)] = stack[i]
    return new_stack


def part_a():
    stack = [x for x in range(10007)]
    for line in pz_input:
        if line == "":
            continue
        pieces = line.split(" ")
        if pieces[0] == "cut":
示例#20
0
from PuzzleInput import ReadInput

pz_input = [int(x) for x in ReadInput(17).data[:-2]]


def part_a():
    init_combo = []
    for i in range(len(pz_input)):
        init_combo.append([i])
    for i in range(1, len(pz_input)):
        curr_combos = [x for x in init_combo if len(x) == i]
        print("Creating size %s (%s to expand)" % (i + 1, len(curr_combos)))
        for combo in curr_combos:
            for j in range(len(pz_input)):
                if j not in combo:
                    new_combo = combo.copy()
                    new_combo.append(j)
                    new_combo.sort()
                    values = [pz_input[x] for x in new_combo]
                    if new_combo not in init_combo and sum(values) <= 150:
                        init_combo.append(new_combo)
    valid_total = 0
    for combo in init_combo:
        total = 0
        for x in combo:
            total += pz_input[x]
        if total == 150:
            valid_total += 1
    return valid_total