diff options
author | romangraef <romangraef@loves.dicksinhisan.us> | 2018-12-06 17:04:54 +0100 |
---|---|---|
committer | romangraef <romangraef@loves.dicksinhisan.us> | 2018-12-06 17:04:54 +0100 |
commit | fa183aa17721e86cd44b4cf9d43917987bc074ca (patch) | |
tree | 0cdbc958e48c00f0d2d35e78b93454a8a054b889 | |
parent | 28697e7a3bb6da16e9be331bf9576572468910d6 (diff) | |
download | aoc2018-fa183aa17721e86cd44b4cf9d43917987bc074ca.tar.gz aoc2018-fa183aa17721e86cd44b4cf9d43917987bc074ca.tar.bz2 aoc2018-fa183aa17721e86cd44b4cf9d43917987bc074ca.zip |
day5
-rw-r--r-- | commons.py | 2 | ||||
-rw-r--r-- | day5/solve.py | 34 |
2 files changed, 35 insertions, 1 deletions
@@ -22,7 +22,7 @@ def get_input(day: int) -> str: if cache_file.exists(): return cache_file.read_text() text = requests.get('http://adventofcode.com/2018/day/{}/input'.format(day), - cookies=cookies).text + cookies=cookies).text.strip() cache_file.write_text(text) return text diff --git a/day5/solve.py b/day5/solve.py new file mode 100644 index 0000000..bf62eba --- /dev/null +++ b/day5/solve.py @@ -0,0 +1,34 @@ +from string import ascii_lowercase, ascii_uppercase + +from commons import get_input + +removals = [''.join(x) for x in + list(zip(ascii_uppercase, ascii_lowercase)) + list(zip(ascii_lowercase, ascii_uppercase))] + +inp = get_input(5) + + +def find_reduced_length(txt): + last = "" + + while last != txt: + last = txt + for removal in removals: + txt = txt.replace(removal, '') + if txt != last: + break + return len(txt) + + +def find_best_removal(txt): + min_length = 1000000000 + for ch in ascii_lowercase: + length = find_reduced_length(txt.replace(ch, '').replace(ch.upper(), '')) + if length < min_length: + min_length = length + return min_length + + +if __name__ == '__main__': + print("first:", find_reduced_length(inp)) + print("second:", find_best_removal(inp)) |