-
-
Save pawlos/d355e133b372b90e429a97e204f7b067 to your computer and use it in GitHub Desktop.
Solution to Day 16: Permutation Promenade - Part 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#aoc_d16.py | |
import re | |
inp = 'abcde' | |
inp = ''.join([chr(c+ord('a')) for c in range(0,16)]) | |
moves = open('input_d16.txt','r').read().split(',') | |
def rotate(l, n): | |
return l[-n:] + l[:-n] | |
def dance(inp, moves): | |
for i in moves: | |
if i.startswith("s"): | |
much = int(i.replace('s','')) | |
inp = rotate(inp, much) | |
if i.startswith("x"): | |
m = re.match("x(\d+)/(\d+)",i) | |
c1 = int(m.group(1)) | |
c2 = int(m.group(2)) | |
d = [c for c in inp] | |
#print i,c1 | |
t = d[c1] | |
d[c1] = d[c2] | |
d[c2] = t | |
inp = ''.join(d) | |
if i.startswith("p"): | |
m = re.match("p([a-p]+)/([a-p]+)",i) | |
e1 = m.group(1) | |
c1 = inp.index(e1) | |
e2 = m.group(2) | |
c2 = inp.index(e2) | |
d = [c for c in inp] | |
t = d[c1] | |
d[c1] = d[c2] | |
d[c2] = t | |
inp = ''.join(d) | |
return inp | |
print dance(inp, moves) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment