From 0e83d8f58978111a3104f2d636637338d2db3a2e Mon Sep 17 00:00:00 2001 From: FrederikBaerentsen Date: Sat, 30 Nov 2024 19:20:09 +0100 Subject: [PATCH] Solved 2015/05 part 2 --- 2015/05/5.md | 12 +++++++---- 2015/05/solution.py | 51 ++++++++------------------------------------- 2015/05/test3 | 4 ++++ 3 files changed, 21 insertions(+), 46 deletions(-) diff --git a/2015/05/5.md b/2015/05/5.md index cd29a1b..8a57ed7 100644 --- a/2015/05/5.md +++ b/2015/05/5.md @@ -28,8 +28,6 @@ How many strings are nice? Your puzzle answer was `236`. -The first half of this puzzle is complete! It provides one gold star: \* - ## \-\-- Part Two \-\-- {#part2} Realizing the error of his ways, Santa has switched to a better model of @@ -60,8 +58,14 @@ For example: How many strings are nice under these new rules? -Answer: +Your puzzle answer was `51`. -Although it hasn\'t changed, you can still [get your puzzle +Both parts of this puzzle are complete! They provide two gold stars: +\*\* + +At this point, you should [return to your Advent calendar](/2015) and +try another puzzle. + +If you still want to see it, you can [get your puzzle input](5/input). diff --git a/2015/05/solution.py b/2015/05/solution.py index b3f3e51..0c39fe0 100644 --- a/2015/05/solution.py +++ b/2015/05/solution.py @@ -46,47 +46,16 @@ if part == 1: # # ######################################### +def find_non_overlapping_pairs(string): + pattern = r'(..).*?\1' + matches = re.findall(pattern, string) + return list(set(matches)) + def isNaughty2(inst): - # bad_words = ['ab','cd','pq','xy'] - - # if any(w in inst for w in bad_words): - # return True - # if sum(inst.lower().count(v) for v in "aeiou") < 3: - # return True - # for i in range(0,len(inst)-1): - # if inst[i] == inst[i+1]: - # return False - # return True - - pairs = [] - for i in range(0,len(inst)-1): - tmp = inst[i]+inst[i+1] - pairs.append(tmp) - #print(inst) - #print(pairs) - n_pairs = [] - sdx = 0 - while sdx < len(pairs)-2: - #for sdx in range(0,len(pairs)-1): - if pairs[sdx] != pairs[sdx+1]: - n_pairs.append(pairs[sdx]) - if pairs[sdx] == pairs[sdx+1] and pairs[sdx] == pairs[sdx+2]: - n_pairs.append(pairs[sdx]) - n_pairs.append(pairs[sdx+2]) - sdx+=1 - #print(n_pairs) - - opt = [item for item in set(pairs) if n_pairs.count(item) > 1] - - - #print(opt) - if len(opt) > 0: - for i in range(0,len(inst)-2): - if inst[i] == inst[i+2]: - print(inst[i], inst[i+1],inst[i+2]) - return False - return True - return True + for i in range(0,len(inst)-2): + if inst[i] == inst[i+2] and len(find_non_overlapping_pairs(inst)) > 0: + return False + return True if part == 2: instructions = [] @@ -98,7 +67,5 @@ if part == 2: for inst in instructions: if not isNaughty2(inst): - #print('Nice') count += 1 - #input() print(count) diff --git a/2015/05/test3 b/2015/05/test3 index 7232e99..1aee0c4 100644 --- a/2015/05/test3 +++ b/2015/05/test3 @@ -1,2 +1,6 @@ aaaa +aaa aaabcb +xxyxyxx +xxxyyx +hejejk