コード例 #1
0
def part_1():

    position = [0, 0]
    facing = "E"
    print("Initial:", position[X], position[Y], facing)

    lines = AOC.get_input_lines(12, AOC.format_strip)
    for line in lines:

        action = str(line[:1])
        value = int(line[1:])

        if action in compass_str:
            position[X] += compass[action][X] * value
            position[Y] += compass[action][Y] * value

        if action == "F":
            position[X] += compass[facing][X] * value
            position[Y] += compass[facing][Y] * value

        if action in rotate_str:
            steps = int(int(rotation[action] * value) / 90)
            total_steps = len(compass_str)
            facing = compass_str[(total_steps + compass_str.find(facing) +
                                  steps) % total_steps]

        print("Position: {},{} Direction: {}".format(position[X], position[Y],
                                                     facing))
    print("Manhattan Distance:", abs(position[X]) + abs(position[Y]))
コード例 #2
0
def part_2():
    def rotate_90(xy: [], clockwise: int) -> []:
        return [clockwise * xy[1], -clockwise * xy[0]]

    ship = [0, 0]
    waypoint_relative = [10, 1]
    print("Initial:", ship[X], ship[Y])

    lines = AOC.get_input_lines("12", AOC.format_strip)
    for line in lines:

        action = str(line[:1])
        value = int(line[1:])

        if action in compass_str:
            waypoint_relative[X] += compass[action][X] * value
            waypoint_relative[Y] += compass[action][Y] * value

        if action == "F":
            ship[X] += waypoint_relative[X] * value
            ship[Y] += waypoint_relative[Y] * value

        if action in rotate_str:
            for _ in range(0, int(int(abs(rotation[action]) * value) / 90)):
                waypoint_relative = rotate_90(waypoint_relative,
                                              rotation[action])

        print("Position: {},{} Waypoint: {} {}".format(ship[X], ship[Y],
                                                       waypoint_relative[X],
                                                       waypoint_relative[Y]))
    print("Manhattan Distance:", abs(ship[X]) + abs(ship[Y]))
コード例 #3
0
ファイル: Day13.py プロジェクト: drantscript/AdventOfCode2020
def part_2():

    def for_calculate(buses : []):

        # buses = [(17,0),(13,2),(19,3)]
        print(buses) 

        STEP = 0
        OFFSET = 1
        
        time = 0
        step = buses[0][STEP]
        
        for i in range(1, len(buses)):

            while not (time + buses[i][OFFSET]) % buses[i][STEP] == 0:
                time += step

            step *= buses[i][STEP]
    
        print("Result:", time)

    lines = AOC.get_input_lines("13", AOC.format_strip)
    slots = lines[1].split(",")
    buses = []

    for i in range(0, len(slots)):
        if slots[i] != "x":
            buses.append((int(slots[i]),i))

    print("Buses: {}".format(buses))

    for_calculate(buses)
コード例 #4
0
ファイル: Day11.py プロジェクト: drantscript/AdventOfCode2020
def part_2():

    lines = AOC.get_input_lines("11", AOC.format_strip)
    old_seats = []
    for line in lines:
        old_seats.append(list(line))

    anything_changed = True
    while anything_changed:

        new_seats = [x[:] for x in old_seats]
        anything_changed = False

        for y in range(0, len(old_seats)):
            for x in range(0, len(old_seats[y])):
                if old_seats[y][x] != FLOOR:

                    new_seat = occupy_seat_part2(x, y, old_seats)

                    if new_seat != new_seats[y][x]:
                        new_seats[y][x] = new_seat
                        anything_changed = True

        old_seats = [x[:] for x in new_seats]

    print("Final:")
    for row in range(0, len(new_seats)):
        print("".join(new_seats[row]))
    print("")

    print("Occupied:", count_occupied(new_seats))
    return
コード例 #5
0
ファイル: Day14.py プロジェクト: drantscript/AdventOfCode2020
def part_1():

    lines = AOC.get_input_lines(14, AOC.format_strip)

    full_memory = {}
    current_mask = int_to_bits(0)

    for line in lines:

        # Mask setter
        if "mask = " in line:
            current_mask = line.replace("mask = ", "")

        # Memory setter
        if "mem" in line:
            mem_index = int(line.replace("mem" + "[", "").partition("] = ")[0])
            mem_value = int(line.replace("mem" + "[", "").partition("] = ")[2])
            full_memory[mem_index] = apply_mask(current_mask,
                                                int_to_bits(mem_value))

    total = 0
    for value in full_memory.keys():
        total += bits_to_int(full_memory[value])
    print("Total:", total)

    return
コード例 #6
0
def get_code() -> []:

    lines = AOC.get_input_lines(8, AOC.format_strip)
    code = []
    for line in lines:
        line.replace("+","")
        split = line.split(" ")
        code.append(split)

    return code
コード例 #7
0
def part_1():

    lines = AOC.get_input_lines(15, AOC.format_strip)
    numbers = lines[0].split(",")

    # part_1
    total_count = 2020

    # part_2
    total_count = 30000000

    # test
    #numbers = ["0","3","6"]
    #total_count = 10

    turn = 1
    spoken = []
    spoken_history = {}

    def add_or_append_history(number: str):
        if number in spoken_history.keys():
            spoken_history[number].append(turn)
        else:
            spoken_history[number] = [turn]

    for number in numbers:

        spoken.append(number)
        add_or_append_history(number)
        turn += 1

    while turn <= total_count:

        number = 0
        if len(spoken_history[spoken[-1]]) > 1:
            number = spoken_history[spoken[-1]][-1] - spoken_history[
                spoken[-1]][-2]

        # print("Turn {}, Number {}, Repeat {}".format(turn, number, last_was_repeat))
        spoken.append(str(number))
        add_or_append_history(str(number))

        if turn % 10000000 == 0:
            print(turn, float(turn) / 30000000, "%")

        turn += 1

    print("Spoken:", spoken)
    print("Last:", spoken[-1])
コード例 #8
0
ファイル: Day6.py プロジェクト: drantscript/AdventOfCode2020
def part_1():
    lines = AOC.get_input_lines(6, AOC.format_strip)
    groups = []
    current_group = ""
    for line in lines:
        if line == "":
            groups.append(current_group)
            current_group = ""
        else:
            for c in line:
                if c not in current_group:
                    current_group += c

    groups.append(current_group)
    return groups
コード例 #9
0
ファイル: Day10.py プロジェクト: drantscript/AdventOfCode2020
def part_1():
    lines = AOC.get_input_lines("10", AOC.format_to_int)
    lines.sort()

    diffs = [0, 0, 0]
    diffs[lines[0] - 1] += 1  # outlet to first adapter

    for i in range(0, len(lines) - 1):

        diff = lines[i + 1] - lines[i]
        diffs[diff - 1] += 1

    diffs[2] += 1  # last adapter to device

    print("Diffs:", diffs)
    print("Result: ", diffs[0] * diffs[2])
コード例 #10
0
ファイル: Day14.py プロジェクト: drantscript/AdventOfCode2020
def part_2():

    lines = AOC.get_input_lines(14, AOC.format_strip)

    full_memory = {}
    current_mask = int_to_bits(0)

    for line in lines:

        # Mask setter
        if "mask = " in line:
            current_mask = line.replace("mask = ", "")
            # print(current_mask)

        # Memory setter
        if "mem" in line:
            mem_index = int(line.replace("mem" + "[", "").partition("] = ")[0])
            mem_value = int(line.replace("mem" + "[", "").partition("] = ")[2])

            base_address = int_to_bits(mem_index)
            masked_address = apply_mask_v2(current_mask, base_address)
            # print(masked_address)

            addresses = []

            def generate_floating_addresses(from_address: str) -> []:
                if from_address.find("X") != -1:
                    new_0 = from_address.replace("X", "0", 1)
                    new_1 = from_address.replace("X", "1", 1)
                    generate_floating_addresses(new_0)
                    generate_floating_addresses(new_1)
                else:
                    addresses.append(from_address)

            generate_floating_addresses(masked_address)
            # print(addresses)

            for address in addresses:
                full_memory[address] = mem_value

    total = 0
    for value in full_memory.keys():
        total += full_memory[value]
    print("Total:", total)

    return
コード例 #11
0
ファイル: Day13.py プロジェクト: drantscript/AdventOfCode2020
def part_1():
    lines = AOC.get_input_lines(13, AOC.format_strip)
    earliest_timestamp = int(lines[0])
    buses = list(map(int, lines[1].replace(",x","").split(",")))

    print(earliest_timestamp)
    print(buses)

    best_wait = -1
    best_bus = -1

    for bus in buses:
        wait = -(earliest_timestamp % bus) + bus
        print("Bus {} Wait {}".format(bus, wait))
        if wait < best_wait or best_wait == -1:
            best_wait = wait
            best_bus = bus

    print("Result: ", best_bus * best_wait)
コード例 #12
0
ファイル: Day6.py プロジェクト: drantscript/AdventOfCode2020
def part_2():
    lines = AOC.get_input_lines(6, AOC.format_strip)
    groups = []
    current_group = ""
    new_line = True
    for line in lines:
        if line == "":
            groups.append(current_group)
            current_group = ""
            new_line = True
        else:
            if new_line:
                current_group = line
                new_line = False
            else:
                test_group = current_group
                for c in test_group:
                    if c not in line:
                        current_group = current_group.replace(c, "")

    groups.append(current_group)
    return groups
コード例 #13
0
ファイル: Day4.py プロジェクト: drantscript/AdventOfCode2020
def check_passports():

    lines = AOC.get_input_lines(4, AOC.format_strip)

    # generate passports
    passports = []
    passport_entries = []
    for line in lines:

        if (line == ""):
            passports.append(passport_entries[:])
            passport_entries.clear()
        else:
            chunks = line.split()
            for chunk in chunks:
                passport_entries.append(chunk)

    passports.append(passport_entries)

    # generate passport requirements
    def in_range(value: str, min: int, max: int):
        return int(value) >= min and int(value) <= max

    def pass_hgt(value: str):
        if ("cm" in value):
            return in_range(value.replace("cm", ""), 150, 193)
        elif ("in" in value):
            return in_range(value.replace("in", ""), 59, 76)
        return False

    def pass_hcl(value: str):
        if (value[0] == "#"):
            if (len(sub := value.replace("#", "")) == 6):
                for c in sub:
                    if (c not in "0123456789abcdef"):
                        return False
                return True
        return False
コード例 #14
0
ファイル: Day17.py プロジェクト: drantscript/AdventOfCode2020
import AOC

INACTIVE = False
ACTIVE = True

X = 0
Y = 1
Z = 2
W = 3

STATE = 0
DESIRED = 1

input = "17"  # 17
lines = AOC.get_input_lines(input, AOC.format_strip)

for line in lines:
    print(line)


def text_to_dimension(input: []) -> {}:

    dimension = {}
    for i, line in enumerate(lines):
        y = i
        for j, char in enumerate(line):
            x = j
            z = 0
            w = 0
            dimension[(x, y, z,
コード例 #15
0
ファイル: Day9.py プロジェクト: drantscript/AdventOfCode2020
        test_total = lines[current_index]

        while test_total < target:
            current_index += 1
            test_total += lines[current_index]

        if target == test_total:
            print("Found sum values:", lines[i:current_index + 1])

            min = 0
            max = 0
            for value in lines[i:current_index + 1]:
                if value < min or min == 0:
                    min = value
                if value > max or max == 0:
                    max = value

            print("Min:", min, "Max:", max)
            print("Result:", min + max)

            return

    print("ERROR no sum found")
    return


# Main
lines = AOC.get_input_lines(9, AOC.format_to_int)
part_1(lines)
part_2(lines)
コード例 #16
0
ファイル: Day10.py プロジェクト: drantscript/AdventOfCode2020
def part_2():
    def find_jump_indices(i: int, all: []) -> []:

        jump_indices = []
        if (test := i + 1) < len(all):
            if all[test] - all[i] <= 3:
                jump_indices.append(test)
        if (test := i + 2) < len(all):
            if all[test] - all[i] <= 3:
                jump_indices.append(test)
        if (test := i + 3) < len(all):
            if all[test] - all[i] <= 3:
                jump_indices.append(test)
        return jump_indices

    lines = AOC.get_input_lines("10", AOC.format_to_int)
    lines.sort()

    lines.insert(0, 0)
    lines.append(lines[len(lines) - 1] + 3)

    # TEST
    # lines = [0,1,2,3,6]

    print(lines)

    branches_from_here = [0] * len(lines)
    branches_from_here[len(lines) - 1] = 1

    for current_index in range((len(lines) - 1), -1, -1):
        indices = find_jump_indices(current_index, lines)