Skip to content

Instantly share code, notes, and snippets.

@pawlos
Created December 16, 2017 13:33
Show Gist options
  • Save pawlos/d355e133b372b90e429a97e204f7b067 to your computer and use it in GitHub Desktop.
Save pawlos/d355e133b372b90e429a97e204f7b067 to your computer and use it in GitHub Desktop.
Solution to Day 16: Permutation Promenade - Part 1
#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