Esempio n. 1
0
def is_beam(x, y):
    beam = intcode_machine(inp, [x, y])
    while True:
            halt, output = beam.run_till_output_or_halt()
            if halt:
                break
    return output
Esempio n. 2
0
def inspect(instr):
    instr = list(map(ord, instr))
    droid = intcode_machine(inp, instr)
    outp = []
    while True:
        halt, output = droid.run_till_output_or_halt()
        if halt:
            break
        outp.append(output)
    return outp
Esempio n. 3
0
def get_colors(start_color):
    intcode = intcode_machine(inp, [])
    face = 'U'
    pos = (0, 0)
    colors = {pos: start_color}
    while True:
        try:
            intcode.add_inputs([colors[pos]])
        except KeyError:
            intcode.add_inputs([0])
        halt1, paint = intcode.run_till_output_or_halt()
        if not halt1:
            halt2, turn = intcode.run_till_output_or_halt()
            colors[pos] = paint
            face, pos = new_pos(pos, face, turn)
        else:
            break

    return colors
Esempio n. 4
0
def run_program(a):
    intcode = intcode_machine(inp, [a])
    return intcode.run_till_halt()
Esempio n. 5
0
import sys
import os
sys.path.insert(1, os.path.dirname(os.getcwd()))
from aoc_functions import intcode_machine

with open('input') as f:
    inp = list(map(int, f.read().split(',')))

intcode = intcode_machine(inp, [1])
intcode.run_till_halt()
print(intcode.output)

intcode = intcode_machine(inp, [2])
intcode.run_till_halt()
print(intcode.output)
Esempio n. 6
0
import sys
import os
sys.path.insert(1, os.path.dirname(os.getcwd()))
from aoc_functions import intcode_machine

with open('input') as f:
    inp = list(map(int, f.read().split(',')))

# Part 1
ascii = intcode_machine(inp, [])
outp = []

while True:
    halt, output = ascii.run_till_output_or_halt()
    if halt:
        break
    outp.append(output)

print(''.join(chr(x) for x in outp))

scaffs = []
i, j = 0, 0
for o in outp:
    if o == 35:
        scaffs.append(complex(i, j))
    i += 1
    if o == 10:
        j += 1
        i = 0

Esempio n. 7
0
import sys
import os
import numpy as np
sys.path.insert(1, os.path.dirname(os.getcwd()))
from aoc_functions import intcode_machine


with open('input') as f:
    inp = list(map(int, f.read().split(',')))

# Part 1
intcode = intcode_machine(inp, [])
blocktiles = 0

while True:
    halt, x = intcode.run_till_output_or_halt()
    if halt:
        break
    else:
        _, y = intcode.run_till_output_or_halt()
        _, id = intcode.run_till_output_or_halt()
        blocktiles += id == 2

print(blocktiles)


# Part 2
intcode = intcode_machine(inp, [])
intcode[0] = 2
x_paddle = 0
x_ball = 0
Esempio n. 8
0
import sys
import os
from itertools import product
sys.path.insert(1, os.path.dirname(os.getcwd()))
from aoc_functions import intcode_machine

with open('input') as f:
    inp = list(map(int, f.read().split(',')))

n = 50
comps = [intcode_machine(inp, [i, -1]) for i in range(n)]


def packets_to_send():
    packets = []
    for i in range(n):
        while True:
            try:
                _, output = comps[i].run_till_output_or_halt()
                packets.append(output)
            except IndexError:
                break
    return [[packets[k + i] for i in range(3)]
            for k in range(0, len(packets), 3)]


def send_packets(ps):
    for p in ps:
        comps[p[0]].add_inputs([p[1], p[2]])

Esempio n. 9
0
import itertools
sys.path.insert(1, os.path.dirname(os.getcwd()))
from aoc_functions import intcode_machine


with open('input') as f:
    inp = list(map(int, f.read().split(',')))

# Part 1
phase_list = list(itertools.permutations([0, 1, 2, 3, 4]))
signals = []

for phase in phase_list:
    output = 0
    for amp in range(5):
        intcode = intcode_machine(inp, [phase[amp], output])
        output = intcode.run_till_halt()
    signals.append(output)

print(max(signals))

# Part 2
phase_list = list(itertools.permutations([5, 6, 7, 8, 9]))
signals = []
for phase in phase_list:
    intcodes = [intcode_machine(inp, [p]) for p in phase]
    outputs = [0]
    while True:
        for intcode in intcodes:
            intcode.add_inputs([outputs[-1]])
            halt, output = intcode.run_till_output_or_halt()
Esempio n. 10
0
def run_program(a, b):
    intcode = intcode_machine(inp)
    intcode[1] = a
    intcode[2] = b
    intcode.run_till_halt()
    return intcode[0]