Compare commits

...

2 Commits

3 changed files with 1128 additions and 4 deletions

File diff suppressed because it is too large Load Diff

View File

@ -13,4 +13,25 @@ aaa is nice because it has at least three vowels and a double letter, even thoug
jchzalrnumimnmhp is naughty because it has no double letter.
haegwjzuvuyypxyu is naughty because it contains the string xy.
dvszwmarrgswjxmb is naughty because it contains only one vowel.
How many strings are nice?
How many strings are nice?
Your puzzle answer was 238.
--- Part Two ---
Realizing the error of his ways, Santa has switched to a better model of determining whether a string is naughty or nice. None of the old rules apply, as they are all clearly ridiculous.
Now, a nice string is one with all of the following properties:
It contains a pair of any two letters that appears at least twice in the string without overlapping, like xyxy (xy) or aabcdefgaa (aa), but not like aaa (aa, but it overlaps).
It contains at least one letter which repeats with exactly one letter between them, like xyx, abcdefeghi (efe), or even aaa.
For example:
qjhvhtzxzqqjkmpb is nice because is has a pair that appears twice (qj) and a letter that repeats with exactly one letter between them (zxz).
xxyxx is nice because it has a pair that appears twice and a letter that repeats with one between, even though the letters used by each rule overlap.
uurcxstgmygtbstg is naughty because it has a pair (tg) but no repeat with a single letter between them.
ieodomkazucvgmuy is naughty because it has a repeating letter with one between (odo), but no pair that appears twice.
How many strings are nice under these new rules?
Your puzzle answer was 69.
Both parts of this puzzle are complete! They provide two gold stars: **

View File

@ -1,10 +1,113 @@
# region Part 1
def check_disallowed_strs(text: str) -> bool:
disallowed_strs = ["ab", "cd", "pq", "xy"]
str_is_disallowed = False
for disallowed_str in disallowed_strs:
if disallowed_str in text:
str_is_disallowed = True
return str_is_disallowed
def check_for_unique_vowels(text: str) -> bool:
vowels = ["a", "e", "i", "o", "u"]
num_vowels = 0
for vowel in vowels:
if vowel in text:
num_vowels += 1
return num_vowels > 2
def check_for_vowels(text: str) -> bool:
vowels = ["a", "e", "i", "o", "u"]
num_vowels = 0
for char in text:
if char in vowels:
num_vowels += 1
return num_vowels > 2
def check_for_repeating_letters(text: str) -> bool:
num_repeats, last_char = 0, ''
for char in text:
if char == last_char and last_char != '':
num_repeats += 1
last_char = char
return num_repeats > 0
def check_if_nice(text: str) -> bool:
nice = check_for_vowels(text) and check_for_repeating_letters(text) and not check_disallowed_strs(text)
return nice
def check_all(pi: list) -> int:
nice_words = 0
for word in pi:
nice_words += check_if_nice(word)
return nice_words
# endregion Part 1
# region Part 2
def check_pair_part2(text: str) -> bool:
contains_pair, last_char = False, ''
for i, char in enumerate(text):
if last_char != '' and (last_char + char) in text[i+1:]:
contains_pair = True
last_char = char
return contains_pair
def check_double_part2(text: str) -> bool:
spaced_double, last_char, second_last_char = False, '', ''
for char in text:
if last_char != '' and second_last_char != '':
if char == second_last_char:
spaced_double = True
second_last_char = last_char
last_char = char
return spaced_double
def check_if_nice_part2(text: str) -> bool:
return check_pair_part2(text) and check_double_part2(text)
def check_all_part2(pi: list) -> int:
nice_words = 0
for word in pi:
nice_words += check_if_nice_part2(word)
return nice_words
# endregion Part 2
if __name__ == '__main__':
assert (check_for_vowels("aei")
and check_for_vowels("xazegov")
and check_for_vowels("aeiouaeiouaeiou")), \
"Error: Vowlchecker couldn't solve"
assert (check_for_repeating_letters("xx")
and check_for_repeating_letters("abcdde")), \
"Error: repeating letters couldn't solve"
assert check_if_nice("ugknbfddgicrmopn"), "Error: Example 1 couldn't be solved"
assert check_if_nice("aaa"), "Error: Example 2 couldn't be solved"
assert not check_if_nice("jchzalrnumimnmhp"), "Error: Example 3 couldn't be solved"
assert not check_if_nice("haegwjzuvuyypxyu"), "Error: Example 4 couldn't be solved"
assert not check_if_nice("dvszwmarrgswjxmb"), "Error: Example 5 couldn't be solved"
assert check_pair_part2("xyxy") and check_pair_part2("aabcdefgaa") and not check_pair_part2("aaa"), \
"Error: Part 2 Pairchecker couldn't solve"
assert check_double_part2("xxx") and check_double_part2("abcdefeghi"), "Error: Part 2 Doublechecker couldn't solve"
assert check_if_nice_part2("qjhvhtzxzqqjkmpb"), "Error: Part 2 Example 1 couldn't be solved"
assert check_if_nice_part2("xxyxx"), "Error: Part 2 Example 2 couldn't be solved"
assert not check_if_nice_part2("uurcxstgmygtbstg"), "Error: Part 2 Example 3 couldn't be solved"
assert not check_if_nice_part2("ieodomkazucvgmuy"), "Error: Part 2 Example 4 couldn't be solved"
print("All test passed")
puzzle_input = open("input.txt", "r").readline()
print("solution: ", )
puzzle_input = open("input.txt", "r").read().splitlines()
print("solution: ", check_all(puzzle_input))
print("Part2: ")
print("solution: ", )
print("solution: ", check_all_part2(puzzle_input))