aboutsummaryrefslogtreecommitdiff
path: root/day22/part2
blob: affcea0910fdc63582bfcd39111a5e81f9caaa29 (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
#!/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

input = $stdin.readlines.map(&:chomp)
size = input.length
infinity = 1000

map = []

infinity.times do
  map << ([:clean]*((2*infinity)+size))
end

input.each do |r|
  map << ([:clean]*infinity) + r.chars.map{|x| x == '#' ? :infected : :clean} + ([:clean]*infinity)
end

infinity.times do
  map << ([:clean]*((2*infinity)+size))
end

row = infinity + (size + 1)/2 - 1
col = infinity + (size + 1)/2 - 1
dir = [-1, 0]
infections = 0

10_000_000.times do
  case map[row][col]
  when :clean
    dir = turn_left(dir)
    map[row][col] = :weakened
  when :weakened
    map[row][col] = :infected
    infections += 1
  when :infected
    dir = turn_right(dir)
    map[row][col] = :flagged
  when :flagged
    dir = turn_right(turn_right(dir))
    map[row][col] = :clean
  end
  row += dir[0]
  col += dir[1]
end

puts infections