Skip to content

Instantly share code, notes, and snippets.

Created December 29, 2017 10:37
Show Gist options
  • Save anonymous/8d179d004d1219e2483a0a9f62984f7c to your computer and use it in GitHub Desktop.
Save anonymous/8d179d004d1219e2483a0a9f62984f7c to your computer and use it in GitHub Desktop.
import strutils, sequtils
const
instructions = readFile("./inputs/16.txt").split(',')
programs = "abcdefghijklmnop"
proc dance(dancers: string): string =
result = dancers
for instr in instructions:
let rem = instr[1 .. instr.high]
case instr[0]
of 's':
let rot = rem.parseInt
result = result[^rot .. result.high] & result[0 ..< ^rot]
of 'x':
let
x = rem.split('/')
a = x[0].parseInt
b = x[1].parseInt
swap(result[a], result[b])
of 'p':
let
a = result.find(rem[0])
b = result.find(rem[^1])
result[a] = rem[^1]
result[b] = rem[0]
else: discard
proc longDance(dancers: string, iterations = 1_000_000_000): string =
var
dancers = dancers
seen = @[dancers]
for i in 1 .. iterations:
dancers = dancers.dance()
if dancers in seen:
return seen[iterations mod i]
seen.add(dancers)
echo dance(programs)
echo longDance(programs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment