From 116ff98539480676c644c06a5529968adeaebbf7 Mon Sep 17 00:00:00 2001 From: Michael Knudsen Date: Sun, 3 Dec 2023 15:01:19 +0100 Subject: [PATCH] Comments and less extreme code formatting --- 2015/05/code.py | 92 +++++++++++++------------------------------------ 1 file changed, 24 insertions(+), 68 deletions(-) diff --git a/2015/05/code.py b/2015/05/code.py index 00195dc..1a9389b 100644 --- a/2015/05/code.py +++ b/2015/05/code.py @@ -1,7 +1,12 @@ import unittest -def is_nice(message: str) -> bool: +def is_nice_first_attempt( + message: str, +) -> bool: + """Santa has come up with a very sophisticated algorithm for determining + whether a string is naughty or nice.""" + # Message must contain at least three vowels. if sum(1 for char in message if char in "aeiou") < 3: return False @@ -26,9 +31,12 @@ def is_nice(message: str) -> bool: return True -def is_very_nice( +def is_nice_second_attempt( message: str, ) -> bool: + """Realizing that the first algorithm was no good, Santa devises + a completely new algorithm.""" + # Message must contain a pair of any two letters that appears at least # twice in the string without overlapping. if not any(message[i : i + 2] in message[i + 2 :] for i in range(len(message) - 2)): @@ -43,54 +51,18 @@ def is_very_nice( class TestCode(unittest.TestCase): - def test_is_nice(self) -> None: - self.assertTrue( - is_nice( - message="ugknbfddgicrmopn", - ) - ) - self.assertTrue( - is_nice( - message="aaa", - ) - ) - self.assertFalse( - is_nice( - message="jchzalrnumimnmhp", - ) - ) - self.assertFalse( - is_nice( - message="haegwjzuvuyypxyu", - ) - ) - self.assertFalse( - is_nice( - message="dvszwmarrgswjxmb", - ) - ) - - def test_is_very_nice(self) -> None: - self.assertTrue( - is_very_nice( - message="qjhvhtzxzqqjkmpb", - ) - ) - self.assertTrue( - is_very_nice( - message="xxyxx", - ) - ) - self.assertFalse( - is_very_nice( - message="uurcxstgmygtbstg", - ) - ) - self.assertFalse( - is_very_nice( - message="ieodomkazucvgmuy", - ) - ) + def test_is_nice_first_attempt(self) -> None: + self.assertTrue(is_nice_first_attempt(message="ugknbfddgicrmopn")) + self.assertTrue(is_nice_first_attempt(message="aaa")) + self.assertFalse(is_nice_first_attempt(message="jchzalrnumimnmhp")) + self.assertFalse(is_nice_first_attempt(message="haegwjzuvuyypxyu")) + self.assertFalse(is_nice_first_attempt(message="dvszwmarrgswjxmb")) + + def test_is_nice_second_attempt(self) -> None: + self.assertTrue(is_nice_second_attempt(message="qjhvhtzxzqqjkmpb")) + self.assertTrue(is_nice_second_attempt(message="xxyxx")) + self.assertFalse(is_nice_second_attempt(message="uurcxstgmygtbstg")) + self.assertFalse(is_nice_second_attempt(message="ieodomkazucvgmuy")) class TestPuzzle(unittest.TestCase): @@ -99,23 +71,7 @@ def setUp(self) -> None: self.messages = f.read().splitlines() def test_part_one(self) -> None: - self.assertEqual( - sum( - map( - is_nice, - self.messages, - ) - ), - 255, - ) + self.assertEqual(sum(map(is_nice_first_attempt, self.messages)), 255) def test_part_two(self) -> None: - self.assertEqual( - sum( - map( - is_very_nice, - self.messages, - ) - ), - 55, - ) + self.assertEqual(sum(map(is_nice_second_attempt, self.messages)), 55)