{ "cells": [ { "cell_type": "code", "execution_count": 16, "id": "bridal-lightning", "metadata": {}, "outputs": [], "source": [ "def count(f):\n", " increased = 0 \n", " prev_m = None \n", "\n", " for l in f: \n", " i = int(l.rstrip()) \n", " if prev_m is None: \n", " prev_m = i \n", " continue \n", " if i > prev_m: \n", " increased += 1 \n", "\n", " prev_m = i\n", " \n", " return increased\n", "\n", "\n", "def count_window(f):\n", " increased = 0 \n", " window = [] \n", "\n", " for l in f:\n", " i = int(l.rstrip()) \n", " if len(window) < 3: \n", " window.append(i) \n", " continue \n", "\n", " if sum(window[1:]) + i > sum(window): \n", " increased += 1 \n", "\n", " window.pop(0) \n", " window.append(i)\n", " \n", " return increased" ] }, { "cell_type": "code", "execution_count": 17, "id": "unsigned-sterling", "metadata": {}, "outputs": [], "source": [ "import import_ipynb\n", "import helper" ] }, { "cell_type": "code", "execution_count": 18, "id": "angry-richards", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1759" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "count(helper.open_file('2021_1.txt'))" ] }, { "cell_type": "code", "execution_count": 19, "id": "authentic-berkeley", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1805" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "count_window(helper.open_file('2021_1.txt'))" ] }, { "cell_type": "code", "execution_count": null, "id": "average-disclosure", "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": "Sonar Sweep" }, "nbformat": 4, "nbformat_minor": 5 }