{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import aocd\n", "\n", "data = aocd.get_data(day=5, year=2017)\n", "instructions = list(map(int, data.splitlines()))" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def execute(instructions, decrement_at_3=False):\n", " pos = 0\n", " steps = 0\n", " while 0 <= pos < len(instructions):\n", " delta = instructions[pos]\n", " if (not decrement_at_3) or delta < 3:\n", " instructions[pos] += 1\n", " else:\n", " instructions[pos] -= 1\n", " pos += delta\n", " steps += 1\n", " return steps" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Part 1: 372671\n" ] } ], "source": [ "assert execute([0, 3, 0, 1, -3]) == 5\n", "print(\"Part 1:\", execute(instructions[:]))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Part 2: 25608480\n" ] } ], "source": [ "assert execute([0, 3, 0, 1, -3], True) == 10\n", "print(\"Part 2:\", execute(instructions[:], True))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.0" } }, "nbformat": 4, "nbformat_minor": 2 }