aboutsummaryrefslogtreecommitdiff
path: root/day13/part1
blob: a858ffe6b68425ad435b4574c1c3ee2fc975eac1 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env ruby

class Cart
  def initialize(dir = nil)
    @dir = dir
    @next = :l
    @moved = false
  end

  def moved?
    @moved
  end

  def moved
    @moved = true
  end

  def reset
    @moved = false
  end

  attr_reader :dir

  def turn(dir = nil)
    case dir
    when :l
      case @dir
      when :u
        @dir = :l
      when :d
        @dir = :r
      when :l
        @dir = :d
      when :r
        @dir = :u
      end
    when :r
      case @dir
      when :u
        @dir = :r
      when :d
        @dir = :l
      when :l
        @dir = :u
      when :r
        @dir = :d
      end
    else
      case @next
      when :l
        @next = :s
        self.turn(:l)
      when :s
        @next = :r
      when :r
        @next = :l
        self.turn(:r)
      end
    end
  end
end

class Cell
  def initialize(track = nil, cart = nil)
    @track = track
    @cart = cart
  end

  attr_accessor :cart
  attr_reader :track

  def maybe_turn_cart
    case @track
    when :b
      case @cart.dir
      when :u
        @cart.turn(:l)
      when :d
        @cart.turn(:l)
      when :l
        @cart.turn(:r)
      when :r
        @cart.turn(:r)
      end
    when :f
      case @cart.dir
      when :u
        @cart.turn(:r)
      when :d
        @cart.turn(:r)
      when :l
        @cart.turn(:l)
      when :r
        @cart.turn(:l)
      end
    when :i
      @cart.turn
    end
  end

  def show
    case @cart&.dir
    when :u
      "^"
    when :d
      "v"
    when :l
      "<"
    when :r
      ">"
    else
      case @track
      when :v
        "|"
      when :h
        "-"
      when :f
        "/"
      when :b
        "\\"
      when :i
        "+"
      else
        " "
      end
    end
  end
end

input = $stdin.readlines

grid = []

input.each do |irow|
  grow = []
  irow.chars.each do |cell|
    case cell
    when " "
      grow << Cell.new
    when "-"
      grow << Cell.new(:h)
    when "|"
      grow << Cell.new(:v)
    when "/"
      grow << Cell.new(:f)
    when "\\"
      grow << Cell.new(:b)
    when "+"
      grow << Cell.new(:i)
    when "^"
      grow << Cell.new(:v, Cart.new(:u))
    when "v"
      grow << Cell.new(:v, Cart.new(:d))
    when "<"
      grow << Cell.new(:h, Cart.new(:l))
    when ">"
      grow << Cell.new(:h, Cart.new(:r))
    end
  end
  grid << grow
end

loop do
  grid.each do |row|
    row.each do |cell|
      cell.cart&.reset
    end
  end

  grid.length.times do |row|
    grid[row].length.times do |col|
      cart = grid[row][col].cart
      unless cart&.moved? then
        case cart&.dir
        when :u
          newcell = grid[row-1][col]
          if newcell.cart then
            puts "#{col},#{row-1}"
            exit 0
          end
          cart.moved
          grid[row][col].cart = nil
          newcell.cart = cart
          newcell.maybe_turn_cart
        when :d
          newcell = grid[row+1][col]
          if newcell.cart then
            puts "#{col},#{row+1}"
            exit 0
          end
          cart.moved
          grid[row][col].cart = nil
          newcell.cart = cart
          newcell.maybe_turn_cart
        when :l
          newcell = grid[row][col-1]
          if newcell.cart then
            puts "#{col-1},#{row}"
            exit 0
          end
          cart.moved
          grid[row][col].cart = nil
          newcell.cart = cart
          newcell.maybe_turn_cart
        when :r
          newcell = grid[row][col+1]
          if newcell.cart then
            puts "#{col+1},#{row}"
            exit 0
          end
          cart.moved
          grid[row][col].cart = nil
          newcell.cart = cart
          newcell.maybe_turn_cart
        end
      end
    end
  end
end