Skip to content

Commit

Permalink
Comments and less extreme code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
micknudsen committed Dec 3, 2023
1 parent 0a03229 commit 116ff98
Showing 1 changed file with 24 additions and 68 deletions.
92 changes: 24 additions & 68 deletions 2015/05/code.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)):
Expand All @@ -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):
Expand All @@ -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)

0 comments on commit 116ff98

Please sign in to comment.