aboutsummaryrefslogtreecommitdiff
path: root/day16
diff options
context:
space:
mode:
authorNat Lasseter <Nat Lasseter nathan@bytemark.co.uk>2018-11-19 22:56:50 +0000
committerNat Lasseter <Nat Lasseter nathan@bytemark.co.uk>2018-11-19 22:56:50 +0000
commita1de6e22366061049c0c876348f4e2cfdc2f5c07 (patch)
tree01b59265f70ea6333b187c57ec38f04658928028 /day16
parent73b650f44d8a2ba166936a13d275d606bae35395 (diff)
[day16] Had an inspiration about part 2
Diffstat (limited to 'day16')
-rwxr-xr-xday16/part241
1 files changed, 41 insertions, 0 deletions
diff --git a/day16/part2 b/day16/part2
new file mode 100755
index 0000000..e494f16
--- /dev/null
+++ b/day16/part2
@@ -0,0 +1,41 @@
+#!/usr/bin/env ruby
+
+input = gets.chomp.split(',')
+
+startline = %w( a b c d e f g h i j k l m n o p )
+line = startline.dup
+i = 0
+
+def dodance(line, dance)
+ dance.each do |istr|
+ case istr[0]
+ when 's'
+ n = istr[1..-1].to_i
+ line = line[-n..-1] + line[0..-n-1]
+ when 'x'
+ ns = istr[1..-1].split('/').map(&:to_i)
+ t = line[ns[0]]
+ line[ns[0]] = line[ns[1]]
+ line[ns[1]] = t
+ when 'p'
+ ps = istr[1..-1].split('/')
+ i0 = line.index(ps[0])
+ i1 = line.index(ps[1])
+ line[i0] = ps[1]
+ line[i1] = ps[0]
+ end
+ end
+ line
+end
+
+loop do
+ line = dodance(line, input)
+ i += 1
+ break if line == startline
+end
+
+(1_000_000_000 % i).times do
+ line = dodance(line, input)
+end
+
+puts line.join