Ejemplo n.º 1
0
# Advent of Code 2020 - Day 9

# Author:   Rachael Judy
# Date:     12/9/2020
# Purpose:  Find number in which previous 25 does not possess two numbers to sum to it
#           Find contiguous set in whole list that does sum to the number and add its min and max

import os
import sys

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import parseMod

# input numbers
numbers = parseMod.readCSV('data/9numbers.csv', '\n')

# initial preamble
preamble = numbers[0:25]
# part 1 - find number that previous 25 can't sum to
for to_match in numbers[
        25:]:  # look for number that cannot be summed from the preamble
    match = False
    for x in range(len(preamble)):  # check every number in preamble
        for y in range(len(preamble)):
            if x != y and preamble[x] + preamble[y] == to_match:
                match = True
                break
        if match:
            break
    # if no match was found in that preamble, display the secret number
    if not match:
Ejemplo n.º 2
0
# Author:   Rachael Judy
# Date:     12/12/2020
# Purpose:  Run intcode program on fifty network computers communicating
#

import copy
import os
import sys
import queue

from intcodecomputer import Computer
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import parseMod

# get program instructions
instructions = parseMod.readCSV('data/23networks.csv')

# create fifty computers and give them their own addresses
computers = []
input_queues = [queue.SimpleQueue() for i in range(50)]
for i in range(50):  # create fifty computers and label them
    computers.append(Computer(copy.deepcopy(instructions)))
    input_queues[i].put(i)  # put computer number in the computer
    computers[i].processInput(input_queues[i])  # put appropriate input queue on each

# outputs from computers, content, requested vals
outputs = [[] for i in range(50)]
nat_content = [0, 0]  # x, y
first_y, prev_y = 1, 1  # for tracking the requested values

# continue until computers give the info we want
Ejemplo n.º 3
0
        return output, array, halt, base


# display the array as grid
def paintShip(display):
    for row in display:
        for el in row:
            if el == 1:
                print('}{', end='')
            else:
                print('  ', end='')
        print()


# get instructions
instructions = parseMod.readCSV('data/11robot.csv', ',')
for i in range(1000):
    instructions.append(0)

# prep for run
inputQ = queue.SimpleQueue()
inputQ.put(stage)  # prep input with either 0 or 1
ind, stop, direction, base = 0, 0, 100, 0  # direction mod 4 will indicate up, right, down, left

# track panels and number changed
countOfPaints = 0
squaresPainted = []  # will hold tuples of spots visited
panels = [[0 for j in range(75)] for i in range(75)]  # side of ship
x, y = 30, 20  # start in center

# run program until end
Ejemplo n.º 4
0
            else:
                array[array[index + 3]] = 0
            index += 3

        elif array[index] % 100 == 99:  # end program
            halt = -1
            break

        index += 1  # separate in case of garbage numbers
    if not feedback:
        return output
    else:
        return output, array, halt


instructions = parseMod.readCSV('data/7num.csv', ',')

# loop through here
if not part:
    permutations = list(itertools.permutations([0, 1, 2, 3, 4]))
    inputQ = queue.SimpleQueue()
    maxT = 0
    bestSeq = permutations[0]
    # check all possible permutations
    for p in permutations:
        imp = 0  # input to each phase, set be output from previous
        for phase in p:
            inputQ.put(phase)
            inputQ.put(imp)

            # compute next
Ejemplo n.º 5
0
# Date:     12/9/2020
# Purpose:  Use map provided by intcode program to find alignment parameters at intersections and
#           map route through paths to find dust collected value

import os
import sys

import copy
import queue

from intcodecomputer import Computer
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import parseMod

# get input
instructions = parseMod.readCSV('data/17robot.csv')
instructions_copy = copy.deepcopy(instructions)  # untouched version for part 2

# part 1
# initialize camera and process
cameras = Computer(instructions)
display = cameras.processInput(queue.SimpleQueue())

array = [[' ' for i in range(53)] for j in range(53)]
# create array from the input string
x, y = 0, 0
for response in display:
    print(chr(response), end='')
    if response == 10:
        x = 0
        y += 1
Ejemplo n.º 6
0
    left = XMARGIN + (tileX * TILESIZE) + (tileX - 1)
    top = YMARGIN + (tileY * TILESIZE) + (tileY - 1)
    return (left, top)


def drawTile(tilex, tiley, number, adjx=0, adjy=0):
    # draw a tile at board coordinates tilex and tiley, optionally a few
    # pixels over (determined by adjx and adjy)
    TILECOLOR = colorDict[number]
    left, top = getLeftTopOfTile(tilex, tiley)
    pygame.draw.rect(DISPLAYSURF, TILECOLOR,
                     (left + adjx, top + adjy, TILESIZE, TILESIZE))


# input
instructions = parseMod.readCSV(
    'data/13game.csv')  # MODIFY TO 13gameMOD.csv for MOD VERSION

pygame.init()
FPSCLOCK = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption('Arcade Game')

# phase 1
if phase == 1:
    # create computer
    arcade = intcodecomputer.Computer(instructions)

    # run program and show gameboard
    count = 0
    inputQ = queue.SimpleQueue()
    gameboard = np.zeros((42, 24))
Ejemplo n.º 7
0
                array[z] = 0
            index += 3

        elif array[index] % 100 == 8:  # equals
            if array[x] == array[y]:
                array[z] = 1
            else:
                array[z] = 0
            index += 3

        elif array[index] % 100 == 9:  # base + parameter
            base += array[x]
            index += 1

        elif array[index] % 100 == 99:  # end program
            halt = -1
            break

        index += 1  # separate in case of garbage numbers
    if not feedback:
        return output
    else:
        return output, array, halt


instructions = parseMod.readCSV('data/9instr.csv', ',')

inputQ = queue.SimpleQueue()
inputQ.put(stage)
processInput(instructions, inputQ)
Ejemplo n.º 8
0
# Advent of Code 2018 - Day 1

# Author:   Rachael Judy
# Date:
# Purpose:

import os
import sys
import math

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import parseMod

changes = parseMod.readCSV('data/1offset.csv')

# part 1
print("Part 1: ", sum(changes))

# part 2 - slow, maybe fix it later
x = 0
used = [x]
end, round = False, 0
while True:
    for i in changes:
        x += i
        if x in used:  # found repetition
            end = True
            break
        elif not round:  # will eventually start pattern over, so need only first set
            used.append(x)
    round += 1
Ejemplo n.º 9
0
            if not x:
                index = y
                index -= 1
            else:
                index += 2

        elif array[index] % 100 == 7:  # less than
            x, y = setParams(array, index)
            if x < y:
                array[array[index + 3]] = 1
            else:
                array[array[index + 3]] = 0
            index += 3

        elif array[index] % 100 == 8:  # equals
            x, y = setParams(array, index)
            if x == y:
                array[array[index + 3]] = 1
            else:
                array[array[index + 3]] = 0
            index += 3

        elif array[index] % 100 == 99:  # end program
            break
        index += 1  # separate in case of garbage numbers
    return array


content = parseMod.readCSV('data/5num.csv', ',')
processInput(content)
Ejemplo n.º 10
0
# Advent of Code 2020 - Day 10

# Author:   Rachael Judy
# Date:     12/10/2020
# Purpose:  Find product of occupied_count voltage diff between each element in adapters - no duplicates
#           Find total number of adapter arrangements that meet conditions
#           Part 2 works if can remove max of three adapters in a row

import os
import sys

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import parseMod

# get adapters
adapters = parseMod.readCSV('data/10adapters.csv', '\n')
adapters.sort()  # must go up, no duplicates
print("Adapter Jolts: ", adapters)

adapters.insert(0, 0)  # insert source
adapters.append(adapters[-1] + 3)  # insert device

# part 1
# compute differences between each adapter in the list
differences = [0, 0, 0, 0]  # create difference array
for i in range(1, len(adapters)):
    differences[adapters[i] - adapters[i - 1]] += 1

print("Diff array: ", differences)
print("Part 1: ", differences[1] * differences[3])
Ejemplo n.º 11
0
# Author:   Rachael Judy
# Date:     12/10/2020
# Purpose:  Find space affected by beam in 50x50 grid and find first coordinates (distance need)
#           to fit in 100x100 grid (upper left)

import os
import sys

import queue

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import parseMod
from intcodecomputer import Computer

# read drone instructions - each run takes one coordinate pair
instructions = parseMod.readCSV('data/19drone.csv')

# part 1 - find number of spaces affected in 50x50 range
beam_map = [[0 for i in range(50)] for j in range(50)]
count = 0
inputQ = queue.SimpleQueue()
for y in range(50):
    for x in range(50):
        drone = Computer(instructions)  # must recreate instance each time

        # put coordinates into input and get info
        inputQ.put(x)
        inputQ.put(y)
        beam_map[y][x] = drone.processInput(inputQ)[0]

        count += beam_map[y][x]  # if beam has affect, will increment
Ejemplo n.º 12
0
# Advent of Code 2018 - Day 8

# Author:   Rachael Judy
# Date:     12/23/2020
# Purpose:  Read tree in (space separated numbers) with child and metadata attributes and find metadata sum and node val

import os
import sys

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import parseMod
inp = parseMod.readCSV('data/8tree.csv', ' ')

global ptr
ptr = 0  # pointer for collecting the data
nodes = []  # list of Node objects


class Node:  # has children
    def __init__(self, id_p):
        self.id = id_p  # pointer found at
        self.children = []  # children Node objects
        self.metadata = []  # numbers

    def sum_metadata(self):  # sum all it and its children's metadata (recursive)
        return sum(m for m in self.metadata) + sum(c.sum_metadata() for c in self.children)

    def calculate_node_val(self):  # return sum of own data if no children, otherwise return sum of children's values
        return sum(m for m in self.metadata) if len(self.children) == 0 \
            else sum(self.children[x-1].calculate_node_val() for x in self.metadata if 0 < x <= len(self.children))
Ejemplo n.º 13
0
                if dire == 1 or dire == 2:
                    inputQ.put(3 - dire)
                elif dire == 3 or dire == 4:
                    inputQ.put(7 - dire)
                robot.processInput(inputQ)

    # color completed vertices
    location_dict[location].color = 'B'

    # record max depth
    global maxDepth
    maxDepth = max(maxDepth, depth)  # depth at bottom


# input
instructions = parseMod.readCSV('data/15droid.csv')

# create computer
robot = intcodecomputer.Computer(instructions)

# run program
grid = np.zeros((100, 100))
x, y = 50, 50  # start at center

### COMMENT TO THE END COMMENT NOTE FOR VISUALIZATION
# set location dictionary up - not needed to be global, but saves some parameter passing
global location_dict
location_dict = dict()
global oxygenLocation, maxOx, maxDepth, robotCopy
maxOx, maxDepth = 0, 0
for x in range(len(grid)):
Ejemplo n.º 14
0
# Advent of Code 2020 Day 1
# Author:   Rachael Judy
# Date:     12/1/20
# Purpose:  Subset sum, 3 numbers problem

import parseMod
stage = 2

array = parseMod.readCSV("data/1num.csv", '\n')
found = False
for i in array:
    for j in array:
        for k in array:
            if stage == 1:
                k = 0  # uncomment this for stage 1
            if i + j + k == 2020:
                print(i, j, k, sep=',')
                if k:
                    print(i * j * k)
                else:
                    print(i * j)
                found = True
                break
        if found:
            break
    if found:
        break
Ejemplo n.º 15
0
# Purpose:  Create springcode program that tells robot when to jump based on sight in front of it

import os
import sys

import queue

from intcodecomputer import Computer

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import parseMod

# set part
part = 2

instructions = parseMod.readCSV('data/21jumper.csv')
jumper = Computer(instructions)

# part 1 pattern - (!A+!B+!C)D - prevents always jump if can for some reason, otherwise preemptive jump
if part == 1:
    springcode = "NOT B T\nNOT C J\nOR T J\nAND D J\nNOT A T\nOR T J\nWALK\n"  # part 1

# part 2 pattern, similar logic but if gap H ahead, don't know if E will be problem so get closer to see
# - possible missing case
else:
    springcode = "NOT B T\nNOT C J\nOR T J\nAND D J\nAND H J\nNOT A T\nOR T J\nRUN\n"  # part 2

# run through jumped robot
inputQ = queue.SimpleQueue()
for letter in springcode:
    inputQ.put(ord(letter))