aboutsummaryrefslogtreecommitdiff
path: root/day10/part1
blob: 1c24c83524a783a1592bc46b488f81babd2a5e51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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]