aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNat Lasseter <Nat Lasseter nathan@bytemark.co.uk>2017-12-10 21:14:34 +0000
committerNat Lasseter <Nat Lasseter nathan@bytemark.co.uk>2017-12-10 21:14:34 +0000
commit53c1f2ca21484b732f646787898e68cbbf731972 (patch)
tree71f75b8f91b754ba80b1fbd15589a43985cdcaa0
parent89b9402b79bb0d496e37ec2a02164cbbb211a637 (diff)
Day 10
-rw-r--r--day10/input1
-rwxr-xr-xday10/part165
-rwxr-xr-xday10/part285
3 files changed, 151 insertions, 0 deletions
diff --git a/day10/input b/day10/input
new file mode 100644
index 0000000..7a416f0
--- /dev/null
+++ b/day10/input
@@ -0,0 +1 @@
+76,1,88,148,166,217,130,0,128,254,16,2,130,71,255,229
diff --git a/day10/part1 b/day10/part1
new file mode 100755
index 0000000..1c24c83
--- /dev/null
+++ b/day10/part1
@@ -0,0 +1,65 @@
+#!/usr/bin/env ruby
+
+class CircList
+ def initialize(size)
+ @list = (0...size).to_a
+ @skip = 0
+ @ptr = 0
+ end
+
+ def reverse_sublist(len)
+ if @ptr + len > @list.length then
+ reverse_wrapping_sublist(len)
+ else
+ reverse_contained_sublist(len)
+ end
+
+ inc_ptr(len)
+ end
+
+ def to_a
+ return @list
+ end
+
+ def to_s
+ l = @list.dup
+ l[@ptr] = "[#{l[@ptr]}]"
+ return "#{l.join(' ')} skip = #{@skip}"
+ end
+
+ private
+
+ def reverse_wrapping_sublist(len)
+ dbl = @list.dup + @list.dup
+ sublist = dbl[@ptr...@ptr+len].reverse
+
+ (@ptr...@ptr+len).each do |i|
+ @list[i % @list.length] = sublist[i-@ptr]
+ end
+ end
+
+ def reverse_contained_sublist(len)
+ sublist = @list[@ptr...@ptr+len].reverse
+
+ (@ptr...@ptr+len).each do |i|
+ @list[i] = sublist[i-@ptr]
+ end
+ end
+
+ def inc_ptr(len)
+ @ptr += len
+ @ptr += @skip
+ @ptr = @ptr % @list.length
+ @skip += 1
+ end
+end
+
+input = gets.chomp.split(',').map(&:to_i)
+
+list = CircList.new(256)
+
+input.each do |len|
+ list.reverse_sublist(len)
+end
+
+puts list.to_a[0] * list.to_a[1]
diff --git a/day10/part2 b/day10/part2
new file mode 100755
index 0000000..5a55d7d
--- /dev/null
+++ b/day10/part2
@@ -0,0 +1,85 @@
+#!/usr/bin/env ruby
+
+class KnotHash
+ def initialize
+ @list = (0...256).to_a
+ @skip = 0
+ @ptr = 0
+ end
+
+ def reverse_sublist(len)
+ if @ptr + len > @list.length then
+ reverse_wrapping_sublist(len)
+ else
+ reverse_contained_sublist(len)
+ end
+
+ inc_ptr(len)
+ end
+
+ def compress
+ p = 0
+ out = []
+
+ 16.times do
+ out << @list[p...p+16].inject(:^)
+ p += 16
+ end
+
+ return out
+ end
+
+ def hexdigest
+ dense = self.compress
+ hex = dense.map { |i| "%02x" % i }
+ return hex.join
+ end
+
+ def to_a
+ return @list
+ end
+
+ def to_s
+ l = @list.dup
+ l[@ptr] = "[#{l[@ptr]}]"
+ return "#{l.join(' ')} skip = #{@skip}"
+ end
+
+ private
+
+ def reverse_wrapping_sublist(len)
+ dbl = @list.dup + @list.dup
+ sublist = dbl[@ptr...@ptr+len].reverse
+
+ (@ptr...@ptr+len).each do |i|
+ @list[i % @list.length] = sublist[i-@ptr]
+ end
+ end
+
+ def reverse_contained_sublist(len)
+ sublist = @list[@ptr...@ptr+len].reverse
+
+ (@ptr...@ptr+len).each do |i|
+ @list[i] = sublist[i-@ptr]
+ end
+ end
+
+ def inc_ptr(len)
+ @ptr += len
+ @ptr += @skip
+ @ptr = @ptr % @list.length
+ @skip += 1
+ end
+end
+
+input = gets.chomp.chars.map(&:ord) + [17,31,73,47,23]
+
+hash = KnotHash.new
+
+64.times do
+ input.each do |len|
+ hash.reverse_sublist(len)
+ end
+end
+
+puts hash.hexdigest