{ "cells": [ { "cell_type": "code", "execution_count": 10, "id": "italian-borough", "metadata": {}, "outputs": [], "source": [ "def nice(s):\n", " prev_ch = None\n", " vowels = 0\n", " doubles = 0\n", " disallowed_strings = 0\n", " for ch in s:\n", " if ch in ('a', 'e', 'i', 'o', 'u'):\n", " vowels += 1\n", " \n", " if ch == prev_ch:\n", " doubles += 1\n", " \n", " if prev_ch and prev_ch + ch in ('ab', 'cd', 'pq', 'xy'):\n", " disallowed_strings += 1\n", " \n", " prev_ch = ch\n", " \n", " return vowels >= 3 and \\\n", " doubles >= 1 and \\\n", " disallowed_strings == 0" ] }, { "cell_type": "code", "execution_count": 11, "id": "completed-shakespeare", "metadata": {}, "outputs": [], "source": [ "assert nice('ugknbfddgicrmopn') == True\n", "assert nice('aaa') == True\n", "assert nice('jchzalrnumimnmhp') == False\n", "assert nice('haegwjzuvuyypxyu') == False\n", "assert nice('dvszwmarrgswjxmb') == False" ] }, { "cell_type": "code", "execution_count": 12, "id": "accompanied-overhead", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "importing Jupyter notebook from helper.ipynb\n" ] } ], "source": [ "import import_ipynb\n", "import helper\n", "s = helper.read_file('2015_5.txt')" ] }, { "cell_type": "code", "execution_count": 13, "id": "imported-luther", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "255" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(map(nice, s.splitlines()))" ] }, { "cell_type": "code", "execution_count": 21, "id": "boolean-wyoming", "metadata": {}, "outputs": [], "source": [ "def nice2(s):\n", " doubles = 0\n", " repeating = 0\n", " \n", " for i in range(len(s)):\n", " if i > len(s)-3:\n", " break\n", " \n", " if s[i] == s[i+2]:\n", " repeating += 1\n", " \n", " for j in range(i+2, len(s)-1):\n", " if s[i] == s[j] and s[i+1] == s[j+1]:\n", " doubles += 1\n", " \n", " return doubles >= 1 and repeating >= 1" ] }, { "cell_type": "code", "execution_count": 22, "id": "scientific-amendment", "metadata": {}, "outputs": [], "source": [ "assert nice2('qjhvhtzxzqqjkmpb') == True\n", "assert nice2('xxyxx') == True\n", "assert nice2('uurcxstgmygtbstg') == False\n", "assert nice2('ieodomkazucvgmuy') == False" ] }, { "cell_type": "code", "execution_count": 23, "id": "parliamentary-omaha", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "55" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(map(nice2, s.splitlines()))" ] }, { "cell_type": "code", "execution_count": null, "id": "ranking-spell", "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": "Doesn't He Have Intern-Elves For This?" }, "nbformat": 4, "nbformat_minor": 5 }