示例#1
0
def main_1():
    keys = read_input(INPUT_FILE_PATH)

    num_2 = sum([1 if has_n_occurences(key, 2) else 0 for key in keys])
    num_3 = sum([1 if has_n_occurences(key, 3) else 0 for key in keys])

    print("Result: " + str(num_2 * num_3))
示例#2
0
def main_part1():
    freq = 0

    deltas = read_input(INPUT_FILE_PATH)

    for delta in deltas:
        freq = apply_delta(freq, parse_delta(delta))

    print("Part 1 result: " + str(freq))
示例#3
0
def main_2():
    ids = read_input(INPUT_FILE_PATH)
    for idx in range(len(ids)):
        for rest_idx in range(idx + 1, len(ids)):
            curr_id = ids[idx]
            rest_id = ids[rest_idx]

            if string_dist(curr_id, rest_id) == 1:
                print(common_string(curr_id, rest_id))
示例#4
0
def main():
    chain = read_input(INPUT_FILE_PATH)[0]
    chain = chain[:len(chain) - 1]  # remove '\n'

    new_chain = process_all(chain)
    print('Part 1: ' + str(len(new_chain)))

    lens = [len(process_all(remove_poly(chain, poly)))
            for poly in 'abcdefghijklmnopqrstuvwyxz']
    print('Part 2: ' + str(max(lens)))
示例#5
0
def main_1():
    lines = shared.read_input('day6-input.txt')
    coords = [parse_coord(line) for line in lines]

    a = {}
    a1 = areas(coords, 1)
    a2 = areas(coords, 2)
    for key in a1:
        if a1[key] == a2[key]:
            a[key] = a1[key]

    print("Result 1: " + str(max(a.values())))
示例#6
0
def main_2():
    claims_str = read_input(INPUT_FILE_PATH)
    claims = [parse_claim(claim_str) for claim_str in claims_str]

    for idx in range(0, len(claims)):
        intersect_any = False
        for idx_oth in range(0, len(claims)):
            if idx == idx_oth:
                continue

            if claims[idx][1].intersect(claims[idx_oth][1]):
                intersect_any = True
                break

        if not intersect_any:
            print(claims[idx][0])
示例#7
0
def main_1():
    claims_str = read_input(INPUT_FILE_PATH)
    claims = [parse_claim(claim_str) for claim_str in claims_str]

    overlap = {}
    for claim in claims:
        xs = range(claim[1].x_off, claim[1].x_off + claim[1].x_wid)
        ys = range(claim[1].y_off, claim[1].y_off + claim[1].y_wid)
        for x in xs:
            for y in ys:
                if not (x, y) in overlap:
                    overlap[(x, y)] = False
                else:
                    overlap[(x, y)] = True

    print(len([x for x in overlap.keys() if overlap[x]]))
示例#8
0
def main():
    lines = read_input(INPUT_FILE_PATH)

    guard_mins = {}
    guard_times = {}

    curr_id = -1
    start_time = None
    for line in lines:
        if has_id(line):
            curr_id = parse_id(line)

        if 'asleep' in parse_log(line):
            start_time = (parse_time(line)[3], parse_time(line)[4])

        if 'wakes' in parse_log(line):
            end_time = (parse_time(line)[3], parse_time(line)[4])
            if curr_id not in guard_mins:
                guard_mins[curr_id] = 0
                guard_times[curr_id] = {}

            if start_time[0] == 23 or start_time[0] == 1 or end_time[
                    0] == 23 or end_time[0] == 1:
                print('Unexpected')
            else:
                guard_mins[curr_id] += end_time[1] - start_time[1]
                for minute in range(start_time[1], end_time[1]):
                    if minute not in guard_times[curr_id]:
                        guard_times[curr_id][minute] = 0

                    guard_times[curr_id][minute] += 1

    max_id = max(guard_mins.items(), key=operator.itemgetter(1))[0]
    max_min = max(guard_times[max_id].items(), key=operator.itemgetter(1))[0]
    print("Part 1: " + str(max_id * max_min))

    max_id_min = [(curr_id, ) +
                  max(guard_times[curr_id].items(), key=operator.itemgetter(1))
                  for curr_id in guard_times.keys()]
    max_id = max(max_id_min, key=operator.itemgetter(2))[0]
    max_min = max(max_id_min, key=operator.itemgetter(2))[1]
    print("Part 2: " + str(max_id * max_min))
示例#9
0
def main_part2():
    freq = 0
    seen_freqs = {}

    deltas = read_input(INPUT_FILE_PATH)
    delta_idx = 0
    while True:
        delta = parse_delta(deltas[delta_idx])
        freq = apply_delta(freq, delta)

        if freq in seen_freqs:
            break
        else:
            # the value doesn't matter though, we're using the dict as a set
            seen_freqs[freq] = 1

        delta_idx += 1
        if delta_idx >= len(deltas):
            delta_idx = 0

    print("Part 2 result: " + str(freq))
示例#10
0
        if testing:
            print(f"pick up: {pickup}")

        d = destination_cup(cups, c)
        if testing:
            print(f"destination: {d}")

        cups = place_pickup(cups, pickup, d)

        c = current_cup(cups, c)
    if testing:
        print(f"\n-- final --")
        print(f"cups: {cups}\n\n")
    return get_order(cups)

# import text from file
if testing:
    text = shared.read_input("input_test")
    #text = shared.read_input("input")
    print(f"input: {text}")
else:
    text = shared.read_input("input")

# Part 1
cups = list(map(int, str(text)))
part1 = moves(cups)
print(f"[Part 1] {part1}")

# Display the time this took to run
shared.printTimeElapsed()
示例#11
0
testing = False

import shared

if testing:
    seats = shared.read_input("input_test")
    print(f"Instructions: {seats}")
else:
    seats = shared.read_input("input")


def evalSeat(seats, row, col):
    if row == 0:
        rowrange = range(0, 2)
    elif row == len(seats) - 1:
        rowrange = range(-1, 1)
    else:
        rowrange = range(-1, 2)
    if col == 0:
        colrange = range(0, 2)
    elif col == len(seats[0]) - 1:
        colrange = range(-1, 1)
    else:
        colrange = range(-1, 2)
    count = 0
    for i in rowrange:
        for j in colrange:
            if i == 0 and j == 0:
                continue
            elif seats[row + i][col + j] == '#':
                count += 1
示例#12
0
                self.nop(int(instructions[self.position][1]))
            elif instructions[self.position][0] == "acc":
                self.acc(int(instructions[self.position][1]))
            elif instructions[self.position][0] == "jmp":
                self.jmp(int(instructions[self.position][1]))
                if self.checkhistory():
                    print(
                        f"Exiting. Infinite loop encountered at Step {index}. Position: {self.position}, Accumulator: {self.accumulator}"
                    )
                    if testing:
                        print(f"History: {self.history}\n")
                    return False


if testing:
    text = shared.read_input(
        "input_test")  # expected test value in accumulator = 5
else:
    text = shared.read_input("input")

instructions = shared.parse_input(text)

if testing:
    print(f"Instructions: {instructions}")

for index in range(len(instructions)):
    changed = False
    if instructions[index][0] == "nop":
        worklist = instructions[:index] + [("jmp", instructions[index][1])
                                           ] + instructions[index + 1:]
        print(f"\nnop changed to jmp at Line {index}")
        changed = True
示例#13
0
def main_2():
    lines = shared.read_input('day6-input.txt')
    coords = [parse_coord(line) for line in lines]

    area = min_area(coords)
    print("Result 2: " + str(area))
示例#14
0
def main_2():
    lines = shared.read_input('day7-input.txt')

    (adj, rev_adj, nodes) = build_adjacency(
        [parse_steps(line) for line in lines])
    process_time(adj, rev_adj, nodes, num_workers=5)
示例#15
0
def main_1():
    lines = shared.read_input('day7-input.txt')

    steps = adjacency([parse_steps(line) for line in lines])
    print(''.join(steps))
示例#16
0
testing = False

import shared
from lark import Lark, LarkError

if testing:
    rules, messages = shared.read_input("input_test")
    print(f"Rules:\n{rules}")
    print(f"Messages:\n{messages}")
else:
    rules, messages = shared.read_input("input")


def solve(rules, messages):
    rules = rules.translate(str.maketrans('0123456789', 'abcdefghij'))
    parser = Lark(rules, start='a')
    match = 0
    for line in messages.splitlines():
        try:
            parser.parse(line)
            match += 1
        except LarkError:
            pass
    return match


# Part 2
rules = rules.replace('8: 42', '8: 42 | 42 8')
rules = rules.replace('11: 42 31', '11: 42 31 | 42 11 31')
part2 = solve(rules, messages)
print(f"[Part 2] {part2}")