diff --git a/2019/05/input.txt b/2019/05/input.txt new file mode 100644 index 0000000..2cc8e7d --- /dev/null +++ b/2019/05/input.txt @@ -0,0 +1 @@ +3,225,1,225,6,6,1100,1,238,225,104,0,1102,68,5,225,1101,71,12,225,1,117,166,224,1001,224,-100,224,4,224,102,8,223,223,101,2,224,224,1,223,224,223,1001,66,36,224,101,-87,224,224,4,224,102,8,223,223,101,2,224,224,1,223,224,223,1101,26,51,225,1102,11,61,224,1001,224,-671,224,4,224,1002,223,8,223,1001,224,5,224,1,223,224,223,1101,59,77,224,101,-136,224,224,4,224,1002,223,8,223,1001,224,1,224,1,223,224,223,1101,11,36,225,1102,31,16,225,102,24,217,224,1001,224,-1656,224,4,224,102,8,223,223,1001,224,1,224,1,224,223,223,101,60,169,224,1001,224,-147,224,4,224,102,8,223,223,101,2,224,224,1,223,224,223,1102,38,69,225,1101,87,42,225,2,17,14,224,101,-355,224,224,4,224,102,8,223,223,1001,224,2,224,1,224,223,223,1002,113,89,224,101,-979,224,224,4,224,1002,223,8,223,1001,224,7,224,1,224,223,223,1102,69,59,225,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,7,677,677,224,1002,223,2,223,1006,224,329,1001,223,1,223,1007,226,226,224,1002,223,2,223,1006,224,344,1001,223,1,223,1108,226,677,224,102,2,223,223,1005,224,359,1001,223,1,223,1107,226,677,224,1002,223,2,223,1006,224,374,101,1,223,223,1107,677,226,224,1002,223,2,223,1006,224,389,101,1,223,223,7,226,677,224,1002,223,2,223,1005,224,404,101,1,223,223,1008,677,226,224,102,2,223,223,1005,224,419,101,1,223,223,1008,226,226,224,102,2,223,223,1006,224,434,101,1,223,223,107,226,226,224,1002,223,2,223,1005,224,449,1001,223,1,223,108,226,677,224,102,2,223,223,1005,224,464,101,1,223,223,1108,677,226,224,102,2,223,223,1005,224,479,101,1,223,223,1007,226,677,224,102,2,223,223,1006,224,494,101,1,223,223,107,677,677,224,102,2,223,223,1005,224,509,101,1,223,223,108,677,677,224,102,2,223,223,1006,224,524,1001,223,1,223,8,226,677,224,102,2,223,223,1005,224,539,101,1,223,223,107,677,226,224,102,2,223,223,1005,224,554,1001,223,1,223,8,226,226,224,102,2,223,223,1006,224,569,1001,223,1,223,7,677,226,224,1002,223,2,223,1005,224,584,1001,223,1,223,1108,226,226,224,102,2,223,223,1005,224,599,1001,223,1,223,1107,677,677,224,1002,223,2,223,1006,224,614,1001,223,1,223,1007,677,677,224,1002,223,2,223,1006,224,629,1001,223,1,223,108,226,226,224,102,2,223,223,1005,224,644,1001,223,1,223,8,677,226,224,1002,223,2,223,1005,224,659,1001,223,1,223,1008,677,677,224,1002,223,2,223,1006,224,674,1001,223,1,223,4,223,99,226 diff --git a/2019/05/solution.ipynb b/2019/05/solution.ipynb new file mode 100644 index 0000000..29ac059 --- /dev/null +++ b/2019/05/solution.ipynb @@ -0,0 +1,210 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "source": [ + "# Advent of Code 2019 - Day 5\n", + "\n", + "## Part 1" + ], + "metadata": {} + }, + { + "cell_type": "code", + "source": [ + "from enum import IntEnum\n", + "\n", + "class Opcode(IntEnum):\n", + " ADD = 1\n", + " MULTIPLY = 2\n", + " STORE = 3\n", + " OUTPUT = 4\n", + " EXIT = 99\n", + "\n", + "class Mode(IntEnum):\n", + " POSITION = 0\n", + " IMMEDIATE = 1" + ], + "outputs": [], + "execution_count": 19, + "metadata": { + "collapsed": false, + "outputHidden": false, + "inputHidden": false + } + }, + { + "cell_type": "code", + "source": [ + "from copy import deepcopy\n", + "\n", + "inputs = [1]\n", + "\n", + "def process_program(program, inputs=inputs):\n", + " user_inputs = deepcopy(inputs)\n", + " \n", + " for idx in range(0, len(program), 4):\n", + " if Opcode(program[idx]) is Opcode.ADD:\n", + " (idx_1, idx_2, idx_3) = program[idx + 1:idx + 4]\n", + " program[idx_3] = program[idx_1] + program[idx_2]\n", + "\n", + " elif Opcode(program[idx]) is Opcode.MULTIPLY:\n", + " (idx_1, idx_2, idx_3) = program[idx + 1:idx + 4]\n", + " program[idx_3] = program[idx_1] * program[idx_2]\n", + "\n", + " elif Opcode(program[idx]) is Opcode.STORE:\n", + " idx_1 = program[idx + 1:idx + 2]\n", + " # get input\n", + " user_inputs.pop(0)\n", + " # program[idx_3] = program[idx_1] * program[idx_2]\n", + " \n", + " elif Opcode(program[idx]) is Opcode.OUTPUT:\n", + " idx_1 = program[idx + 1:idx + 2]\n", + " # get input\n", + " # program[idx_3] = program[idx_1] * program[idx_2]\n", + " \n", + " elif Opcode(program[idx]) == Opcode.EXIT:\n", + " # end program\n", + " return program\n", + "\n", + "\n", + "def str_to_list(string):\n", + " return [int(i) for i in string.split(',')]\n", + "\n", + "\n", + "def parse_input_line(line):\n", + " return [process_program(segment) for segment in str_to_list(line)]\n", + "\n", + "\n", + "def parse_input_file(filename):\n", + " with open(filename, 'r') as f:\n", + " return [parse_input_line(line) for line in f]\n", + "\n", + "\n", + "def parse_instruction(instruction):\n", + " pass\n", + " \n", + "# puzzle_input = parse_input_file('input.txt')\n", + "\n", + "# print(process_program(str_to_list('1002,4,3,4,33')))\n", + "print(process_program(str_to_list('3,0,4,0,99')))" + ], + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[3, 0, 4, 0, 99]\n" + ] + } + ], + "execution_count": 22, + "metadata": { + "collapsed": false, + "outputHidden": false, + "inputHidden": false + } + }, + { + "cell_type": "code", + "source": [ + "points = []\n", + "points.append(get_points(puzzle_input[0]))\n", + "points.append(get_points(puzzle_input[1]))\n", + "\n", + "intersections = set(points[0]).intersection(set(points[1]))" + ], + "outputs": [], + "execution_count": 2, + "metadata": { + "collapsed": false, + "outputHidden": false, + "inputHidden": false + } + }, + { + "cell_type": "code", + "source": [ + "distances = [(abs(i[0]) + abs(i[1])) for i in intersections]\n", + "distances.sort()\n", + "print(f'solution: {distances[1]}')" + ], + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "solution: 1084\n" + ] + } + ], + "execution_count": 3, + "metadata": { + "collapsed": false, + "outputHidden": false, + "inputHidden": false + } + }, + { + "cell_type": "markdown", + "source": [ + "## Part 2" + ], + "metadata": {} + }, + { + "cell_type": "code", + "source": [ + "distances = {}\n", + "for intersection in intersections:\n", + " dist = points[0].index(intersection) + points[1].index(intersection)\n", + " distances[dist] = intersection\n", + "\n", + "nearest = sorted(distances.keys())[1]\n", + "print(f'solution: {nearest}')" + ], + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "solution: 9240\n" + ] + } + ], + "execution_count": 4, + "metadata": { + "collapsed": false, + "outputHidden": false, + "inputHidden": false + } + } + ], + "metadata": { + "kernel_info": { + "name": "advent-of-code" + }, + "language_info": { + "name": "python", + "version": "3.8.0", + "mimetype": "text/x-python", + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "pygments_lexer": "ipython3", + "nbconvert_exporter": "python", + "file_extension": ".py" + }, + "kernelspec": { + "name": "advent-of-code", + "language": "python", + "display_name": "advent-of-code" + }, + "nteract": { + "version": "0.15.0" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} \ No newline at end of file