from trianglesolver import solve, degree

from advents_of_code.year2019.day18.intcode import OutputInterrupt, IntCode

program = """109,424,203,1,21102,11,1,0,1105,1,282,21101,18,0,0,1106,0,259,1202,1,1,221,203,1,21101,0,31,0,1106,0,282,21102,38,1,0,1105,1,259,20101,0,23,2,22102,1,1,3,21102,1,1,1,21102,57,1,0,1105,1,303,2101,0,1,222,21002,221,1,3,20101,0,221,2,21102,1,259,1,21101,0,80,0,1105,1,225,21102,40,1,2,21101,0,91,0,1105,1,303,1201,1,0,223,20101,0,222,4,21101,0,259,3,21101,0,225,2,21101,0,225,1,21102,118,1,0,1105,1,225,21001,222,0,3,21102,1,144,2,21101,0,133,0,1105,1,303,21202,1,-1,1,22001,223,1,1,21102,148,1,0,1105,1,259,1202,1,1,223,20101,0,221,4,21001,222,0,3,21102,1,14,2,1001,132,-2,224,1002,224,2,224,1001,224,3,224,1002,132,-1,132,1,224,132,224,21001,224,1,1,21102,195,1,0,106,0,109,20207,1,223,2,20101,0,23,1,21101,0,-1,3,21102,214,1,0,1105,1,303,22101,1,1,1,204,1,99,0,0,0,0,109,5,2101,0,-4,249,22101,0,-3,1,21201,-2,0,2,22101,0,-1,3,21101,0,250,0,1105,1,225,21202,1,1,-4,109,-5,2105,1,0,109,3,22107,0,-2,-1,21202,-1,2,-1,21201,-1,-1,-1,22202,-1,-2,-2,109,-3,2106,0,0,109,3,21207,-2,0,-1,1206,-1,294,104,0,99,21202,-2,1,-2,109,-3,2106,0,0,109,5,22207,-3,-4,-1,1206,-1,346,22201,-4,-3,-4,21202,-3,-1,-1,22201,-4,-1,2,21202,2,-1,-1,22201,-4,-1,1,21202,-2,1,3,21101,343,0,0,1106,0,303,1105,1,415,22207,-2,-3,-1,1206,-1,387,22201,-3,-2,-3,21202,-2,-1,-1,22201,-3,-1,3,21202,3,-1,-1,22201,-3,-1,2,21202,-4,1,1,21102,384,1,0,1106,0,303,1105,1,415,21202,-4,-1,-4,22201,-4,-3,-4,22202,-3,-2,-2,22202,-2,-4,-4,22202,-3,-2,-3,21202,-4,-1,-2,22201,-3,-2,1,22101,0,1,-4,109,-5,2106,0,0"""

a, b, c, A, B, C = solve(C=90 * degree, b=80, A=32.784 * degree)


def get_wall(x, y):
    comp = IntCode(program=program)
    comp.input_queue.append(y)
    comp.input_queue.append(x)

    while not comp.done:
        try:
            comp.run()
        except OutputInterrupt:
            pass
            # print("Sensor BOOST code: ", comp.output_queue[-1])

    if comp.output_queue[-1]:
        return '#'
    else:
        return '.'


x, y = 772, 1507

for x in range(772, 750, -1):
    for y in range(1507, 1380, -1):

        c = 0
        for x1 in range(x, x + 101):
            if get_wall(x1, y) == '#':
                c += 1

        for y1 in range(y, y + 101):
            if get_wall(x, y1) == '#':
                c += 1

        if c == 199:
            print(x, y)


# print(get_wall(771, 1505))


# for b in range(800, 10000):
#     a, b, c, A, B, C = solve(C=90 * degree, b=b, A=59.689 * degree)
#
#     # print(b, a)
#
#     counter = 0
#     for z in range(b, b - 4000, -1):
#
#         if get_wall(z, int(a)) == '#':
#             counter += 1
#
#         if get_wall(z, int(a)) != '#' and counter:
#             break
#         if counter == 100:
#             break
#
#     print(f'Found line {b}, counter {counter}')
#
#     if counter >= 100:
#         counter = 0
#         for q in range(int(a), int(a) + 501):
#             if get_wall(z + 1, q) == '#':
#                 counter += 1
#             else:
#                 print(counter)
#                 break
#
#     if counter == 100:
#         print(z + 1, int(a))
#

3f279c01318e0ede45caeb71264f1720c04c67eb
<mask token>

<mask token>


def get_wall(x, y):
    comp = IntCode(program=program)
    comp.input_queue.append(y)
    comp.input_queue.append(x)
    while not comp.done:
        try:
            comp.run()
        except OutputInterrupt:
            pass
    if comp.output_queue[-1]:
        return '#'
    else:
        return '.'


<mask token>
for x in range(772, 750, -1):
    for y in range(1507, 1380, -1):
        c = 0
        for x1 in range(x, x + 101):
            if get_wall(x1, y) == '#':
                c += 1
        for y1 in range(y, y + 101):
            if get_wall(x, y1) == '#':
                c += 1
        if c == 199:
            print(x, y)

<mask token>
program = (
    '109,424,203,1,21102,11,1,0,1105,1,282,21101,18,0,0,1106,0,259,1202,1,1,221,203,1,21101,0,31,0,1106,0,282,21102,38,1,0,1105,1,259,20101,0,23,2,22102,1,1,3,21102,1,1,1,21102,57,1,0,1105,1,303,2101,0,1,222,21002,221,1,3,20101,0,221,2,21102,1,259,1,21101,0,80,0,1105,1,225,21102,40,1,2,21101,0,91,0,1105,1,303,1201,1,0,223,20101,0,222,4,21101,0,259,3,21101,0,225,2,21101,0,225,1,21102,118,1,0,1105,1,225,21001,222,0,3,21102,1,144,2,21101,0,133,0,1105,1,303,21202,1,-1,1,22001,223,1,1,21102,148,1,0,1105,1,259,1202,1,1,223,20101,0,221,4,21001,222,0,3,21102,1,14,2,1001,132,-2,224,1002,224,2,224,1001,224,3,224,1002,132,-1,132,1,224,132,224,21001,224,1,1,21102,195,1,0,106,0,109,20207,1,223,2,20101,0,23,1,21101,0,-1,3,21102,214,1,0,1105,1,303,22101,1,1,1,204,1,99,0,0,0,0,109,5,2101,0,-4,249,22101,0,-3,1,21201,-2,0,2,22101,0,-1,3,21101,0,250,0,1105,1,225,21202,1,1,-4,109,-5,2105,1,0,109,3,22107,0,-2,-1,21202,-1,2,-1,21201,-1,-1,-1,22202,-1,-2,-2,109,-3,2106,0,0,109,3,21207,-2,0,-1,1206,-1,294,104,0,99,21202,-2,1,-2,109,-3,2106,0,0,109,5,22207,-3,-4,-1,1206,-1,346,22201,-4,-3,-4,21202,-3,-1,-1,22201,-4,-1,2,21202,2,-1,-1,22201,-4,-1,1,21202,-2,1,3,21101,343,0,0,1106,0,303,1105,1,415,22207,-2,-3,-1,1206,-1,387,22201,-3,-2,-3,21202,-2,-1,-1,22201,-3,-1,3,21202,3,-1,-1,22201,-3,-1,2,21202,-4,1,1,21102,384,1,0,1106,0,303,1105,1,415,21202,-4,-1,-4,22201,-4,-3,-4,22202,-3,-2,-2,22202,-2,-4,-4,22202,-3,-2,-3,21202,-4,-1,-2,22201,-3,-2,1,22101,0,1,-4,109,-5,2106,0,0'
    )
a, b, c, A, B, C = solve(C=90 * degree, b=80, A=32.784 * degree)


def get_wall(x, y):
    comp = IntCode(program=program)
    comp.input_queue.append(y)
    comp.input_queue.append(x)
    while not comp.done:
        try:
            comp.run()
        except OutputInterrupt:
            pass
    if comp.output_queue[-1]:
        return '#'
    else:
        return '.'


x, y = 772, 1507
for x in range(772, 750, -1):
    for y in range(1507, 1380, -1):
        c = 0
        for x1 in range(x, x + 101):
            if get_wall(x1, y) == '#':
                c += 1
        for y1 in range(y, y + 101):
            if get_wall(x, y1) == '#':
                c += 1
        if c == 199:
            print(x, y)

from trianglesolver import solve, degree
from advents_of_code.year2019.day18.intcode import OutputInterrupt, IntCode
program = (
    '109,424,203,1,21102,11,1,0,1105,1,282,21101,18,0,0,1106,0,259,1202,1,1,221,203,1,21101,0,31,0,1106,0,282,21102,38,1,0,1105,1,259,20101,0,23,2,22102,1,1,3,21102,1,1,1,21102,57,1,0,1105,1,303,2101,0,1,222,21002,221,1,3,20101,0,221,2,21102,1,259,1,21101,0,80,0,1105,1,225,21102,40,1,2,21101,0,91,0,1105,1,303,1201,1,0,223,20101,0,222,4,21101,0,259,3,21101,0,225,2,21101,0,225,1,21102,118,1,0,1105,1,225,21001,222,0,3,21102,1,144,2,21101,0,133,0,1105,1,303,21202,1,-1,1,22001,223,1,1,21102,148,1,0,1105,1,259,1202,1,1,223,20101,0,221,4,21001,222,0,3,21102,1,14,2,1001,132,-2,224,1002,224,2,224,1001,224,3,224,1002,132,-1,132,1,224,132,224,21001,224,1,1,21102,195,1,0,106,0,109,20207,1,223,2,20101,0,23,1,21101,0,-1,3,21102,214,1,0,1105,1,303,22101,1,1,1,204,1,99,0,0,0,0,109,5,2101,0,-4,249,22101,0,-3,1,21201,-2,0,2,22101,0,-1,3,21101,0,250,0,1105,1,225,21202,1,1,-4,109,-5,2105,1,0,109,3,22107,0,-2,-1,21202,-1,2,-1,21201,-1,-1,-1,22202,-1,-2,-2,109,-3,2106,0,0,109,3,21207,-2,0,-1,1206,-1,294,104,0,99,21202,-2,1,-2,109,-3,2106,0,0,109,5,22207,-3,-4,-1,1206,-1,346,22201,-4,-3,-4,21202,-3,-1,-1,22201,-4,-1,2,21202,2,-1,-1,22201,-4,-1,1,21202,-2,1,3,21101,343,0,0,1106,0,303,1105,1,415,22207,-2,-3,-1,1206,-1,387,22201,-3,-2,-3,21202,-2,-1,-1,22201,-3,-1,3,21202,3,-1,-1,22201,-3,-1,2,21202,-4,1,1,21102,384,1,0,1106,0,303,1105,1,415,21202,-4,-1,-4,22201,-4,-3,-4,22202,-3,-2,-2,22202,-2,-4,-4,22202,-3,-2,-3,21202,-4,-1,-2,22201,-3,-2,1,22101,0,1,-4,109,-5,2106,0,0'
    )
a, b, c, A, B, C = solve(C=90 * degree, b=80, A=32.784 * degree)


def get_wall(x, y):
    comp = IntCode(program=program)
    comp.input_queue.append(y)
    comp.input_queue.append(x)
    while not comp.done:
        try:
            comp.run()
        except OutputInterrupt:
            pass
    if comp.output_queue[-1]:
        return '#'
    else:
        return '.'


x, y = 772, 1507
for x in range(772, 750, -1):
    for y in range(1507, 1380, -1):
        c = 0
        for x1 in range(x, x + 101):
            if get_wall(x1, y) == '#':
                c += 1
        for y1 in range(y, y + 101):
            if get_wall(x, y1) == '#':
                c += 1
        if c == 199:
            print(x, y)

from trianglesolver import solve, degree

from advents_of_code.year2019.day18.intcode import OutputInterrupt, IntCode

program = """109,424,203,1,21102,11,1,0,1105,1,282,21101,18,0,0,1106,0,259,1202,1,1,221,203,1,21101,0,31,0,1106,0,282,21102,38,1,0,1105,1,259,20101,0,23,2,22102,1,1,3,21102,1,1,1,21102,57,1,0,1105,1,303,2101,0,1,222,21002,221,1,3,20101,0,221,2,21102,1,259,1,21101,0,80,0,1105,1,225,21102,40,1,2,21101,0,91,0,1105,1,303,1201,1,0,223,20101,0,222,4,21101,0,259,3,21101,0,225,2,21101,0,225,1,21102,118,1,0,1105,1,225,21001,222,0,3,21102,1,144,2,21101,0,133,0,1105,1,303,21202,1,-1,1,22001,223,1,1,21102,148,1,0,1105,1,259,1202,1,1,223,20101,0,221,4,21001,222,0,3,21102,1,14,2,1001,132,-2,224,1002,224,2,224,1001,224,3,224,1002,132,-1,132,1,224,132,224,21001,224,1,1,21102,195,1,0,106,0,109,20207,1,223,2,20101,0,23,1,21101,0,-1,3,21102,214,1,0,1105,1,303,22101,1,1,1,204,1,99,0,0,0,0,109,5,2101,0,-4,249,22101,0,-3,1,21201,-2,0,2,22101,0,-1,3,21101,0,250,0,1105,1,225,21202,1,1,-4,109,-5,2105,1,0,109,3,22107,0,-2,-1,21202,-1,2,-1,21201,-1,-1,-1,22202,-1,-2,-2,109,-3,2106,0,0,109,3,21207,-2,0,-1,1206,-1,294,104,0,99,21202,-2,1,-2,109,-3,2106,0,0,109,5,22207,-3,-4,-1,1206,-1,346,22201,-4,-3,-4,21202,-3,-1,-1,22201,-4,-1,2,21202,2,-1,-1,22201,-4,-1,1,21202,-2,1,3,21101,343,0,0,1106,0,303,1105,1,415,22207,-2,-3,-1,1206,-1,387,22201,-3,-2,-3,21202,-2,-1,-1,22201,-3,-1,3,21202,3,-1,-1,22201,-3,-1,2,21202,-4,1,1,21102,384,1,0,1106,0,303,1105,1,415,21202,-4,-1,-4,22201,-4,-3,-4,22202,-3,-2,-2,22202,-2,-4,-4,22202,-3,-2,-3,21202,-4,-1,-2,22201,-3,-2,1,22101,0,1,-4,109,-5,2106,0,0"""

a, b, c, A, B, C = solve(C=90 * degree, b=80, A=32.784 * degree)


def get_wall(x, y):
    comp = IntCode(program=program)
    comp.input_queue.append(y)
    comp.input_queue.append(x)

    while not comp.done:
        try:
            comp.run()
        except OutputInterrupt:
            pass
            # print("Sensor BOOST code: ", comp.output_queue[-1])

    if comp.output_queue[-1]:
        return '#'
    else:
        return '.'


x, y = 772, 1507

for x in range(772, 750, -1):
    for y in range(1507, 1380, -1):

        c = 0
        for x1 in range(x, x + 101):
            if get_wall(x1, y) == '#':
                c += 1

        for y1 in range(y, y + 101):
            if get_wall(x, y1) == '#':
                c += 1

        if c == 199:
            print(x, y)


# print(get_wall(771, 1505))


# for b in range(800, 10000):
#     a, b, c, A, B, C = solve(C=90 * degree, b=b, A=59.689 * degree)
#
#     # print(b, a)
#
#     counter = 0
#     for z in range(b, b - 4000, -1):
#
#         if get_wall(z, int(a)) == '#':
#             counter += 1
#
#         if get_wall(z, int(a)) != '#' and counter:
#             break
#         if counter == 100:
#             break
#
#     print(f'Found line {b}, counter {counter}')
#
#     if counter >= 100:
#         counter = 0
#         for q in range(int(a), int(a) + 501):
#             if get_wall(z + 1, q) == '#':
#                 counter += 1
#             else:
#                 print(counter)
#                 break
#
#     if counter == 100:
#         print(z + 1, int(a))
#
