It was pretty easy to slide back into Python. I used Python 3 because I hear that’s what one ought to use. I really like the Python documentation. A very good read. You can tell because I prefer reading the documentation to searching for examples on StackExchange. Well done!
So, Advent of Code, day 7, part 1: Count how many strings comply with the following specification:
alex[berta]otto
.otto
in the example above.Part 2: Count how many strings comply with the following specification:
ala[lala]otto
.ala
, and a matching inverted three character palindrome inside any of the square brackets, e.g. lal
in the example above.This was surprisingly more difficult!
import sys import re def check_line(line): inside = [] outside = [] for part in re.findall('([a-z]+)(?:\[([a-z]+)\])?', line): outside.append(part[0]) if part[1]: inside.append(part[1]) for word in outside: for i in range(len(word) - 2): if word[i] == word[i+2] and word[i] != word[i+1]: for other in inside: if re.search(word[i+1] + word[i] + word[i+1], other): return 1 def check_input(): sum = 0 for line in sys.stdin: if check_line(line): sum += 1 return sum if __name__ == '__main__': print (str(check_input()))
Call using python part1.py < data
and python part2.py < data
.
I also wondered about an integrated Python debugger for Emacs. Sadly, I used print statements all over the place as I was searching for bugs.
#Python #Advent of Code #Programming
(Please contact me if you want to remove your comment.)
⁂
You can use the fileinput module for reading the data: https://docs.python.org/2/library/fileinput.html
=> https://docs.python.org/2/library/fileinput.html
Instead of for i in range(len(word) - 2): you can do something like for a, b, c in zip(word, word[1:], word[2:]):
Instead of re.search you can simply use the “in” operator.
– Radomir Dopieralski 2016-12-09 08:23 UTC
Thanks!
– AlexSchroeder 2016-12-09 11:40 UTC
text/gemini
This content has been proxied by September (ba2dc).