Advent of Code 2015 day 5 part2: solved

This commit is contained in:
Fabian Tessmer 2025-03-22 18:20:13 +01:00
parent ff126eef68
commit f08395a6ff
2 changed files with 65 additions and 2 deletions

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

@ -49,6 +49,40 @@ def check_all(pi: list) -> int:
# 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")
@ -62,10 +96,18 @@ if __name__ == '__main__':
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").read().splitlines()
print("solution: ", check_all(puzzle_input))
print("Part2: ")
print("solution: ", )
print("solution: ", check_all_part2(puzzle_input))