コード例 #1
0
ファイル: day08.py プロジェクト: feph/AdventOfCode2019
from get_input import get_aoc_input
from matplotlib import pyplot as plt
import matplotlib
import numpy as np
inp = "".join(get_aoc_input(8))
ROWS = 6
COLS = 25

def chunk(l, n):
    i = 0
    while (i*n) < len(l):
        yield l[i*n:(i+1)*n]
        i += 1

layers = list(chunk(inp, ROWS*COLS))[::-1]
digits_zero = [layer.count("0") for layer in layers]
mz = min(digits_zero)
num_layer = digits_zero.index(mz)
res = layers[num_layer].count("1") * layers[num_layer].count("2")
print(f"checksum: {res}")

fig = plt.figure()
ax = plt.gca()
ax.set_facecolor('xkcd:salmon')
for layer in layers:
    layer = np.array([int(f) for f in layer])
    layer = layer.reshape(ROWS, COLS)
    bw = np.ma.masked_where(layer == 2, layer)
    plt.imshow(bw, cmap="gray", vmin=0, vmax=1, interpolation="none")
plt.show()
コード例 #2
0
ファイル: day03.py プロジェクト: feph/AdventOfCode2019
from get_input import get_aoc_input
# inp = ["R8,U5,L5,D3",
# "U7,R6,D4,L4"]
# inp = ["R75,D30,R83,U83,L12,D49,R71,U7,L72",
# "U62,R66,U55,R34,D71,R55,D58,R83"]
# inp = ["R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51",
# "U98,R91,D20,R16,D67,R40,U7,R15,U6,R7"]
# inp = ["R2,U6", "U2, R6, U1,L1,D3"]
inp = get_aoc_input(3)

grid = {}
timings = {}
for w, wire in enumerate(inp):
    w = 1 << w
    t = 0
    x = 0
    y = 0
    steps = wire.split(",")
    for step in steps:
        step = step.strip()
        direction = step[0]
        num = int(step[1:])
        for i in range(1, num + 1):
            t += 1
            if direction == "U":
                y += 1
            if direction == "D":
                y -= 1
            if direction == "R":
                x += 1
            if direction == "L":
コード例 #3
0
ファイル: day09.py プロジェクト: feph/AdventOfCode2019
from get_input import get_aoc_input

from intcomputer import *
import logging
log = logging.getLogger()
# log.setLevel(logging.DEBUG)

inp = "109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99"
# inp = "1102,34915192,34915192,7,4,7,99,0"
# inp = "104,1125899906842624,99"
# inp = "1,9,10,3,2,3,11,0,99,30,40,50"
# inp = "1,1,1,4,99,5,6,0,99 "
# inp = "3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31, 1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104, 999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99"

inp = "".join(get_aoc_input(9))
#part1
# c = intcomputer(inp, [1], memsize=4096)
#part2
c = intcomputer(inp, [2], memsize=4096)

c.run()

print(c.output_queue)
# print(c.memory)
コード例 #4
0
ファイル: day01.py プロジェクト: feph/AdventOfCode2019
import logging
from get_input import get_aoc_input
from math import floor
modules = get_aoc_input(day=1)

### Part 1
fuel = sum([floor(int(mass) / 3) - 2 for mass in modules])
print(f'Needed fuel: {fuel}')

### Part 2


def fuel4mass(x):
    return floor(int(x) / 3) - 2


def fuel4module(m):
    fuel = fuel4mass(m)
    fuels = []
    while fuel > 0:
        fuels.append(fuel)
        fuel = fuel4mass(fuel)
    return sum(fuels)


#print(fuel4module(14))
#print(fuel4module(1969))

total_fuel = sum([fuel4module(m) for m in modules])
print(f'Total fuel for part 2: {total_fuel}')
コード例 #5
0
ファイル: day12.2.py プロジェクト: feph/AdventOfCode2019
reg = re.compile(r'<x=[ ]*([+-]?\d+)\s*,\s*y=\s*([+-]?\d+)\s*,\s*z\s*=\s*([+-]?\d+)>')
def str2vec(s):
    m = reg.search(s)
    return np.array([float(m.group(i)) for i in range(1,4)])

def vec2str(v):
    return f"<x={v[0]:>4}, y={v[1]:>4}, z={v[2]:>4}>"

if __name__ == "__main__":

    inp = """<x=-1, y=0, z=2>
    <x=2, y=-10, z=-7>
    <x=4, y=-8, z=8>
    <x=3, y=5, z=-1>""".split("\n")

    inp = get_aoc_input(12)

    moon_idx = range(0,4)
    perm = tuple(itertools.permutations(moon_idx, 2))
    axis_idx = range(0,3)
    moons = np.array([str2vec(v) for v in inp])
    start_moons = moons.copy()
    vels = np.array([np.zeros(3) for v in moons])
    start_vels = vels.copy()
    steps = np.zeros_like(axis_idx)
    # print(moons)
    # print(moons[:,0])
    # sys.exit(0)
    for ax in axis_idx:
        step = 0
        while True:
コード例 #6
0
    inp = "3,0,4,0,99"  # output the input
    arg = 123142
    log.info(f'running program {inp} with input {arg}')
    out = run_program(inp, arg)
    log.info(f'output = {out}')
    assert (out[0] == arg)

    inp = "1002,6,3,6,4,6,33"
    code = (''.join(inp)).split(',')
    codes = list(map(int, code))
    log.info(f'running program {codes} with input {arg}')
    out = run_program(codes, arg)
    log.info(f'output = {out}')
    assert (out[0] == 99)

    aoc_input = "".join(get_aoc_input(5)).strip()
    out = run_program(aoc_input, 1)
    log.info(f'output for part 1 = {out}')

    arg = 0
    inpstr = "3,9,8,9,10,9,4,9,99,-1,8"
    out = run_program(inpstr, arg)
    log.info(f'output = {out}')
    assert (out[0] == int(arg == 8))

    inpstr = "3,9,7,9,10,9,4,9,99,-1,8"
    out = run_program(inpstr, 7)

    inpstr = "3,3,1108,-1,8,3,4,3,99"
    out = run_program(inpstr, 7)
コード例 #7
0
ファイル: day07.py プロジェクト: feph/AdventOfCode2019
from get_input import get_aoc_input
from intcomputer import *
import itertools
import logging
import sys
log = logging.getLogger()
logging.basicConfig(level=logging.DEBUG)
# log.setLevel(logging.DEBUG)

aoc_inp = "".join(get_aoc_input(7))


def part1():
    # inp = "3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0"
    # inp = "3,23,3,24,1002,24,10,24,1002,23,-1,23, 101,5,23,23,1,24,23,23,4,23,99,0,0"
    # inp = "3,31,3,32,1002,32,10,32,1001,31,-2,31,1007,31,0,33, 1002,33,7,33,1,33,31,31,1,32,31,31,4,31,99,0,0,0"
    inp = aoc_inp
    # print(inp)

    phase_setting = range(5)
    all_phase_settings = itertools.permutations(phase_setting)
    # all_phase_settings = [(4,3,2,1,0)]
    outputs = {}
    c = intcomputer(inp)
    for ps in all_phase_settings:
        input_signal = 0
        for p in ps:
            # print(f"p = {p}")
            # print(f"input_signal = {input_signal}")
            log.debug(f"running program with input = {input_signal}")
            c.push_input(input_signal)  # 2nd
コード例 #8
0
ファイル: day06.py プロジェクト: feph/AdventOfCode2019
G)H
D)I
E)J
J)K
K)L""".split("\n")

elist = [tuple(line.split(")")) for line in testcase]
print(elist)
G = nx.Graph()
G.add_edges_from(elist)

nx.draw_networkx(G)

distances = [len(nx.shortest_path(G, source='COM',target=a))-1 for a in G.nodes()]

print(sum(distances))

aoc_input = get_aoc_input(6)
elist = [tuple(line.split(")")) for line in aoc_input]
# print(elist)
G = nx.Graph()
G.add_edges_from(elist)

# nx.draw_networkx(G)
# plt.waitforbuttonpress()
distances = [len(nx.shortest_path(G, source='COM',target=a))-1 for a in G.nodes()]
print(sum(distances))

path = len(nx.shortest_path(G, source='YOU',target='SAN')) -3
print(path)
コード例 #9
0
from get_input import get_aoc_input
import itertools

inp = get_aoc_input(2)
#inp = "1,9,10,3,2,3,11,0,99,30,40,50"
code = (''.join(inp)).split(',')

codes = list(map(int, code))

## part 1
codes1 = list(codes)
noun = 12
verb = 2
codes1[1] = noun
codes1[2] = verb


def run_program(codes_inp):
    codes = list(codes_inp)  # new memory
    ip = 0
    while True:
        c = codes[ip]
        dest = codes[ip + 3]
        if c == 1:
            codes[dest] = codes[codes[ip + 1]] + codes[codes[ip + 2]]
        if c == 2:
            codes[dest] = codes[codes[ip + 1]] * codes[codes[ip + 2]]
        if c == 99:
            break
        ip += 4
    return codes