Exemplo n.º 1
0
def parse_input(path):
    data = read_input(path)
    boot_code = []
    for line in data:
        instruction, move = line.split()
        boot_code.append((instruction, int(move)))
    return boot_code
Exemplo n.º 2
0
def parse_rules(path):
    data = read_input(path)
    rules = {}
    for rule in data:
        key = re.search("^([a-z\ ]*) bags contain", rule).group(1)
        contents = re.findall("([0-9]) ([a-z\ ]*) bag", rule)
        rules[key] = {c[1]: int(c[0]) for c in contents}

    return rules
Exemplo n.º 3
0
def parse_doc(path):
    data = read_input(path)
    cur_entry = ""
    groups = []
    for l in data:
        if l == "":
            groups.append(cur_entry)
            cur_entry = ""
        else:
            cur_entry += l + " "
    groups.append(cur_entry)  # last entry
    return groups
Exemplo n.º 4
0
def parse_doc(path):
    data = read_input(path)
    cur_entry = ""
    people = []
    for l in data:
        if l == "":
            doc = {
                item.split(":")[0]: item.split(":")[1]
                for item in cur_entry.split()
            }
            people.append(doc)
            cur_entry = ""
        else:
            cur_entry += l + " "
    doc = {
        item.split(":")[0]: item.split(":")[1]
        for item in cur_entry.split()
    }
    people.append(doc)  # last entry
    return people
Exemplo n.º 5
0
from aoc.tools import read_input


def ski(right, down):
    x, y = 0, 0
    width, length = len(forrest_pattern[0]), len(forrest_pattern)
    route = []
    while y < length:
        block = forrest_pattern[y][x]
        route.append(block)

        x += right
        y += down
        if x >= width:  # extend map in x direction
            x -= width
    return route.count("#")


forrest_pattern = read_input("2020/data/day03.txt")

print(ski(3, 1))

print(ski(1, 1) * \
      ski(3, 1) * \
      ski(5, 1) * \
      ski(7, 1) * \
      ski(1, 2))
Exemplo n.º 6
0
from aoc.tools import read_input

data = read_input("2020/data/day19.txt")

test = """0: 4 1 5
1: 2 3 | 3 2
2: 4 4 | 5 5
3: 4 5 | 5 4
4: "a"
5: "b"

ababbb
bababa
abbbab
aaabbb
aaaabbb

""".splitlines()


def parse_info(data):
    rules = {}
    messages = []
    for l in data:
        if ":" in l:
            key, instr = l.split(": ")
            key = int(key)
            if instr[0] == '"':
                rules[key] = instr[1]
            else:
                content = [
Exemplo n.º 7
0
from aoc.tools import read_input

test = """mxmxvkd kfcds sqjhc nhms (contains dairy, fish)
trh fvjkl sbzzf mxmxvkd (contains dairy)
sqjhc fvjkl (contains soy)
sqjhc mxmxvkd sbzzf (contains fish)
""".splitlines()

data = read_input("input")


def parse_data(data):
    food_list = []
    for l in data:
        l = l.replace(",", "")
        food, allergy = l.split(" (contains ")
        food = food.split()
        allergy = allergy.split(")")[0].split()
        food_list.append((food, allergy))
    return food_list


food_list = parse_data(data)

ingredient_dict = {}
allergen_dict = {}

for item in food_list:
    allergens = item[-1]
    ingredients = set(item[0])
    for ingredient in ingredients:
Exemplo n.º 8
0
from aoc.tools import read_input


expense_report = read_input("2020/data/day01.txt")

# part 1
def find_sum(numbers, target_sum=2020):
    for i, n in enumerate(numbers):
        for m in numbers[i:]:
            if int(m) + int(n) == target_sum:
                return int(m) * int(n)

print(find_sum(expense_report))

# part 2
while expense_report:
    n = expense_report.pop()
    remainder = 2020 - int(n)
    try:
        answer = find_sum(expense_report, remainder) * int(n)
    except TypeError:
        continue
print(answer)
Exemplo n.º 9
0
from aoc.tools import read_input

path = "2020/data/day11.txt"


def parse_seat_map(data):
    # get coordinate
    seats = {(x, y): state
             for x, row in enumerate(data) for y, state in enumerate(row)}
    return seats


seat_map = parse_seat_map(read_input(path))

test_map = [
    "L.LL.LL.LL", "LLLLLLL.LL", "L.L.L..L..", "LLLL.LL.LL", "L.LL.LL.LL",
    "L.LLLLL.LL", "..L.L.....", "LLLLLLLLLL", "L.LLLLLL.L", "L.LLLLL.LL"
]

test_map = parse_seat_map(test_map)


def check_neighbour(location, seat_map, part2=False):
    state = seat_map[location]
    # check surroundings
    surroundings = [(-1, 1), (-1, 0), (-1, -1), (0, 1), (0, -1), (1, 1),
                    (1, 0), (1, -1)]
    tiles = []
    for dx, dy in surroundings:
        n = (location[0] + dx, location[1] + dy)
        while part2 and n in seat_map and seat_map[n] == ".":
Exemplo n.º 10
0
def parse_instructions(path):
    # get coordinate
    data = read_input(path)
    return [(row[0], int(row[1:])) for row in data]
Exemplo n.º 11
0
from aoc.tools import read_input
import re

path = "2020/data/day14.txt"

data = read_input(path)

mask = 36 * "X"
mem = {}
for line in data:
    cmd, _, val = line.split()
    if cmd == 'mask':
        mask = val
    else:
        val = "{:036b}".format(int(val))
        result = "".join(m if m in "01" else v for v, m in zip(val, mask))
        addr = re.search("^mem\[(\d+)\]", cmd).group(1)
        mem[int(addr)] = int(result, 2)

print(sum(mem.values()))


def find_floats(results):
    for addr in results:
        i = addr.find('X')
        if i == -1:
            return [addr]
        zero = find_floats([addr[:i] + str(0) + addr[i + 1:]])
        one = find_floats([addr[:i] + str(1) + addr[i + 1:]])
        return zero + one
Exemplo n.º 12
0
from aoc.tools import read_input


def parse_passcode(data):
    passcode_book = []
    for l in data:
        l = l.split()
        ub, lb = map(int, l[0].split("-"))
        entry = {"rule": (ub, lb),
                 "word": l[1].split(":")[0],
                 "password": l[2]}
        passcode_book.append(entry)

    return passcode_book

data = read_input("2020/data/day02.txt")
passcode_book = parse_passcode(data)

# part 1
count = 0
for entry in passcode_book:
    word_occurence = entry["password"].count(entry["word"])

    if (word_occurence >= entry["rule"][0]) and (word_occurence <= entry["rule"][1]):
        count += 1

print(count)

# prart 2
count = 0
for entry in passcode_book:
Exemplo n.º 13
0
from aoc.tools import read_input
from itertools import combinations


path = "2020/data/day09.txt"
data = list(map(int ,read_input(path)))

preamble = 25

def check_sum(target, search_range):
    all_combos = list(combinations(search_range, 2))
    while all_combos:
        a, b = all_combos.pop()
        if target == a + b:
            return True
    return False

def find_rule_breaker(data, preamble):
    fit_the_rule = True
    i = 0
    while fit_the_rule:
        target = data[preamble + i]
        search_range = data[i : i + preamble]
        i += 1
        fit_the_rule = check_sum(target, search_range)
    return target

rule_breaker = find_rule_breaker(data, preamble)
print(rule_breaker)

window_size = 2
Exemplo n.º 14
0
def parse_rules(path):
    time, ids = read_input(path)
    return int(time), ids.split(",")