Пример #1
0
def solve_part1_and_2(input_file):
    fuel_needed1 = 0
    fuel_needed2 = 0
    for mass in process(input_file):
        fuel_needed1 += fuel_requirement_part1(int(mass))
        fuel_needed2 += fuel_requirement_part2(int(mass))
    return (fuel_needed1, fuel_needed2)
Пример #2
0
def run(input_file):
    with timing("Day 16: Flawed Frequency Transmission"):
        for line in process(input_file):
            part1 = solve_part1(line[:], 100)
            part2 = solve_part2(line[:], 100)
    print(part1)
    print(part2)
Пример #3
0
def run(input_file):
    line = next(process(input_file))
    with timing("Day 4: Secure Container"):
        low, high = [int(n) for n in line.split("-")]
        part1, part2 = solve_part1_and_part2(low, high)
    print(part1)
    print(part2)
Пример #4
0
async def run(input_file):
    with timing("Day 7: Amplification Circuit - async"):
        line = next(process(input_file))
        part1 = await solve_part1(line)
        part2 = await solve_part2(line)
    print(part1)
    print(part2)
Пример #5
0
def run(input_file):
    line = next(process(input_file))
    with timing("Day 13: Care Package"):
        part1 = solve_part1(line)
        part2 = solve_part2(line)
    print(part1)
    print(part2)
Пример #6
0
def test_part2():
    line = next(process("input/8"))
    response = solve_part2(line, 25, 6)
    assert (
        response ==
        "100000110010001100101110010000100101000110010100101000010000010101111011100100001011000100100101001010000100100010010010100101111001110001001001011100"
    )
Пример #7
0
def precompute(config: str, scheduler: sched.scheduler = None) -> None:
    """Precompute a configuration file result to serve it faster when it is requested.  This function
    should be used with a scheduler to be repeated over time.

    :param config: name of the configuration file to precompute the result for
    :type config: str

    scheduler used to relaunch the precomputing task in the future.  If not scheduler is specified,
    the task will not be relaunched
    :type scheduler: sched.scheduler
    """
    try:
        cal = process(os.path.basename(config), False)
        path = "app/cache/" + os.path.basename(config).rstrip('.json') + ".ics"
        open(path, 'w').writelines(cal)
        print(arrow.now().format("YYYY-MM-DD HH:mm:ss"), "Precomputed", os.path.basename(config).rstrip('.json'))

    except Exception as e:
        with open("error " + arrow.now().format("YYYY-MM-DD HH:mm:ss")+".txt", 'w') as file:
            file.write(arrow.now().format("YYYY-MM-DD HH:mm:ss") + "\nCould not precompute : " + str(config))
            file.write(str(e))
            file.write(str(traceback.format_exc()))
    finally:
        if scheduler is not None:
            delay = get_min_cache(config)
            delay *= 60
            scheduler.enter(delay=delay, priority=1, action=precompute, argument=(config, scheduler))
Пример #8
0
def run(input_file):
    width, height = 25, 6
    with timing("Day 8: Space Image Format"):
        line = next(process(input_file))
        part1 = solve_part1(line, width, height)
        part2 = solve_part2(line, width, height)
    print(part1)
    _print_image(part2, width, height)
Пример #9
0
def run(input_file):
    line = next(process(input_file))
    with timing("Day 9: Sensor Boost"):
        part1 = ICComputer(get_program(line), 1, [])
        part2 = ICComputer(get_program(line), 2, [])
        part1.run()
        part2.run()
    print(part1.output[0])
    print(part2.output[0])
Пример #10
0
def get_asteroids(input_file):
    asteroids = set()
    y = 0
    for line in process(input_file):
        for x, c in enumerate(line):
            if c == "#":
                asteroids.add(Point(x, y))
        y += 1
    return asteroids
Пример #11
0
def test_part2():
    program = get_program(next(process("input/11")))
    paint_computer = ICComputer(program, position=Point(0, 0), start=1)
    response = solve_part2(program)

    response = response.split("\n")

    expected = [
        ".*....***..****.****..**...**..****.****...",
        ".*....*..*....*.*....*..*.*..*.*....*......",
        ".*....*..*...*..***..*....*....***..***....",
        ".*....***...*...*....*....*.**.*....*......",
        ".*....*.*..*....*....*..*.*..*.*....*......",
        ".****.*..*.****.****..**...***.*....****...",
    ]
    for i, line in enumerate(expected):
        assert line == response[i]
Пример #12
0
def test_part2():
    line = next(process("input/9"))
    part2 = ICComputer(get_program(line), 2, [])
    part2.run()
    assert part2.output[0] == 49115
Пример #13
0
def test_part1():
    line = next(process("input/9"))
    part1 = ICComputer(get_program(line), 1, [])
    part1.run()
    assert part1.output[0] == 2775723069
Пример #14
0
def run(input_file):
    with timing("template"):
        for line in process(input_file):
            solve_part1(line)
            solve_part2(line)
Пример #15
0
async def test_part2():
    line = next(process("input/7"))
    part2 = await solve_part2(line)
    assert part2 == 89603079
Пример #16
0
async def test_part1():
    line = next(process("input/7"))
    part1 = await solve_part1(line)
    assert part1 == 116680
Пример #17
0
def get_intcodes(input_file):
    line = next(process(input_file))
    return [int(n) for n in line.split(",")]
Пример #18
0
def test_part1():
    program = get_program(next(process("input/11")))
    paint_computer = ICComputer(program, position=Point(0, 0))
    panels = paint_computer.run()

    assert len(panels.keys()) == 2160
Пример #19
0
def run(input_file):
    with timing("Day 11: Space Police"):
        part1 = solve_part1(get_program(next(process(input_file))))
        part2 = solve_part2(get_program(next(process(input_file))))
    print(part1)
    print(part2)
Пример #20
0
def test_part1():
    line = next(process("input/8"))
    response = solve_part1(line, 25, 6)

    assert response == 1088
Пример #21
0
def get_wires(input_file):
    wires = []
    for line in process(input_file):
        wires.append([m for m in line.split(",")])
    return wires
Пример #22
0
def test_part2():
    line = next(process("input/13"))
    assert solve_part2(line) == 10292
Пример #23
0
def test_part1():
    line = next(process("input/13"))
    assert solve_part1(line) == 205
Пример #24
0
def get_reactions(input_file):
    reactions = {}
    for line in process(input_file):
        set_reaction(line, reactions)
    return reactions
Пример #25
0
def test_part1():
    response = solve_part1(next(process("input/16")), 100)
    assert response == "73127523"
Пример #26
0
def create_graph(input_file):
    graph = {"COM": None}
    for line in process(input_file):
        parent, orbiter = line.split(")")
        graph[orbiter] = parent
    return graph
Пример #27
0
            output.append(data)
            pc += 2
        elif opcode == 5 or opcode == 6:  # not equal or equal to 0
            parameter1 = get_value(inst, 1)
            parameter2 = get_value(inst, 2)
            if ops[opcode](parameter1, 0):
                pc = parameter2
            else:
                pc += 3
        elif opcode == 7 or opcode == 8:  # less than or equal
            if ops[opcode](get_value(inst, 1), get_value(inst, 2)):
                p[get_index(inst, 3)] = 1
            else:
                p[get_index(inst, 3)] = 0
            pc += 4
        elif opcode == 9:
            base += get_value(inst, 1)
            pc += 2
        else:
            print("unknown opcode", p[pc])
            break
    return output


line = next(process("input/9"))
with timing("Day 9: Sensor Boost"):
    part1 = icc(line, 1)
    part2 = icc(line, 2)
print(part1[0])
print(part2[0])
Пример #28
0
def test_part2():
    line = next(process("input/7"))
    part1 = solve_part2(line)
    assert part1 == 89603079