aboutsummaryrefslogtreecommitdiff
path: root/day19/part1
blob: 536bb42f9e8d7117ede2d294bb02b9eb4c60bc54 (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
#!/usr/bin/env ruby

def turn_left(vector)
  return case vector
  when [1, 0]
    [0, 1]
  when [0, 1]
    [-1, 0]
  when [-1, 0]
    [0, -1]
  when [0, -1]
    [1, 0]
  end
end

def turn_right(vector)
  return case vector
  when [1, 0]
    [0, -1]
  when [0, -1]
    [-1, 0]
  when [-1, 0]
    [0, 1]
  when [0, 1]
    [1, 0]
  end
end

def turn(map, vector, row, col)
  lvec = turn_left(vector)
  rvec = turn_right(vector)

  lrow = row + lvec[0]
  lcol = col + lvec[1]

  rrow = row + rvec[0]
  rcol = col + rvec[1]

  if map[lrow][lcol] == ' ' then
    return rvec
  else
    return lvec
  end
end

input = $stdin.readlines.map(&:chomp).map(&:chars).map(&:to_a)

row = 0
col = input[0].index('|')
vec = [1, 0]
saw = []

loop do
  row += vec[0]
  col += vec[1]
  this = input[row][col]
  if this == ' ' then
    break
  elsif this >= 'A' && this <= 'Z' then
    saw << this
  elsif this == '+' then
    vec = turn(input, vec, row, col)
  end
end

puts saw.join