{ "cells": [ { "cell_type": "code", "execution_count": 3, "id": "continued-river", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1694130\n", "1698850445\n" ] } ], "source": [ "class Submarine():\n", " horizontal = 0\n", " depth = 0\n", " aim = 0\n", "\n", " use_aim = False\n", "\n", " def __init__(self, use_aim=False):\n", " self.use_aim = use_aim\n", "\n", " def down(self, n):\n", " if self.use_aim:\n", " self.aim += n\n", " else:\n", " self.depth += n\n", " \n", " def up(self, n):\n", " if self.use_aim:\n", " self.aim -= n\n", " else:\n", " self.depth -= n\n", "\n", " def forward(self, n):\n", " self.horizontal += n\n", " \n", " if self.use_aim:\n", " self.depth += n * self.aim\n", "\n", "import import_ipynb\n", "import helper\n", "f = helper.open_file('2021_2.txt')\n", "\n", "s = Submarine()\n", "sa = Submarine(use_aim=True)\n", "\n", "for l in f:\n", " d, n = l.split(' ')\n", " f = None\n", " if d == 'forward':\n", " f = Submarine.forward\n", " elif d == 'up':\n", " f = Submarine.up\n", " elif d == 'down':\n", " f = Submarine.down\n", "\n", " f(s, int(n))\n", " f(sa, int(n))\n", " \n", "\n", "print(s.horizontal * s.depth)\n", "print(sa.horizontal * sa.depth)" ] }, { "cell_type": "code", "execution_count": null, "id": "important-pillow", "metadata": {}, "outputs": [], "source": [] } ], "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.9.2" }, "title": "Dive!" }, "nbformat": 4, "nbformat_minor": 5 }