From d2d97b442baafdc2fc90041884d2467691d47f96 Mon Sep 17 00:00:00 2001 From: FrederikBaerentsen Date: Sat, 30 Nov 2024 19:07:44 +0100 Subject: [PATCH] Working on 2015/05 part 2 --- 2015/05/5.md | 67 ++++++++++++++++++++++++++++ 2015/05/solution.py | 104 ++++++++++++++++++++++++++++++++++++++++++++ 2015/05/test2 | 4 ++ 2015/05/test3 | 2 + 4 files changed, 177 insertions(+) create mode 100644 2015/05/5.md create mode 100644 2015/05/solution.py create mode 100644 2015/05/test2 create mode 100644 2015/05/test3 diff --git a/2015/05/5.md b/2015/05/5.md new file mode 100644 index 0000000..cd29a1b --- /dev/null +++ b/2015/05/5.md @@ -0,0 +1,67 @@ +## \-\-- Day 5: Doesn\'t He Have Intern-Elves For This? \-\-- + +Santa needs help figuring out which strings in his text file are naughty +or nice. + +A *nice string* is one with all of the following properties: + +- It contains at least three vowels (`aeiou` only), like `aei`, + `xazegov`, or + `aeiouaeiouaeiou`{title="John Madden John Madden John Madden"}. +- It contains at least one letter that appears twice in a row, like + `xx`, `abcdde` (`dd`), or `aabbccdd` (`aa`, `bb`, `cc`, or `dd`). +- It does *not* contain the strings `ab`, `cd`, `pq`, or `xy`, even if + they are part of one of the other requirements. + +For example: + +- `ugknbfddgicrmopn` is nice because it has at least three vowels + (`u...i...o...`), a double letter (`...dd...`), and none of the + disallowed substrings. +- `aaa` is nice because it has at least three vowels and a double + letter, even though the letters used by different rules overlap. +- `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? + +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 +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? + +Answer: + +Although it hasn\'t changed, you can still [get your puzzle +input](5/input). + diff --git a/2015/05/solution.py b/2015/05/solution.py new file mode 100644 index 0000000..b3f3e51 --- /dev/null +++ b/2015/05/solution.py @@ -0,0 +1,104 @@ +#!/bin/python3 +import sys,re +from pprint import pprint +sys.path.insert(0, '../../') +from fred import list2int + +input_f = 'input' + +part = 2 +######################################### +# # +# Part 1 # +# # +######################################### + +def isNaughty(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 + +instructions = [] + +if part == 1: + count = 0 + with open(input_f) as file: + for line in file: + instructions.append(line.rstrip()) + + for inst in instructions: + if not isNaughty(inst): + count += 1 + print(count) + + + +######################################### +# # +# Part 2 # +# # +######################################### + +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 + +if part == 2: + instructions = [] + + count = 0 + with open(input_f) as file: + for line in file: + instructions.append(line.rstrip()) + + for inst in instructions: + if not isNaughty2(inst): + #print('Nice') + count += 1 + #input() + print(count) diff --git a/2015/05/test2 b/2015/05/test2 new file mode 100644 index 0000000..2de64a3 --- /dev/null +++ b/2015/05/test2 @@ -0,0 +1,4 @@ +qjhvhtzxzqqjkmpb +xxyxx +uurcxstgmygtbstg +ieodomkazucvgmuy diff --git a/2015/05/test3 b/2015/05/test3 new file mode 100644 index 0000000..7232e99 --- /dev/null +++ b/2015/05/test3 @@ -0,0 +1,2 @@ +aaaa +aaabcb