|
| 1 | +require_relative 'day' |
| 2 | +require_relative 'grid' |
| 3 | +require 'set' |
| 4 | + |
| 5 | +class Day06 < Day # > |
| 6 | + attr_accessor :guard |
| 7 | + |
| 8 | + # @example |
| 9 | + # day.part1 #=> 41 |
| 10 | + def part1 |
| 11 | + @start = nil |
| 12 | + @lab_map = |
| 13 | + Grid |
| 14 | + .new(input) |
| 15 | + .parse do |coord, char| |
| 16 | + @start = coord if char == '^' |
| 17 | + end |
| 18 | + |
| 19 | + predict_path(@lab_map, @start) |
| 20 | + .count |
| 21 | + end |
| 22 | + |
| 23 | + # @example |
| 24 | + # day.part1 |
| 25 | + # day.part2 #=> 6 |
| 26 | + def part2 |
| 27 | + predict_path(@lab_map, @start) |
| 28 | + .map { |location| |
| 29 | + predict_path(@lab_map, @start, new_obstruction: location) |
| 30 | + } |
| 31 | + .map(&:first) |
| 32 | + .count |
| 33 | + end |
| 34 | + |
| 35 | + def predict_path(map, start, new_obstruction: nil) |
| 36 | + guard = Guard.new(start, '^') |
| 37 | + |
| 38 | + map.set(new_obstruction, 'O') if new_obstruction |
| 39 | + loop do |
| 40 | + case map.at(guard.next_step) |
| 41 | + when '.','^' ; guard.step_forward! |
| 42 | + when '#','O' ; guard.turn_right! |
| 43 | + when :out_of_bounds |
| 44 | + break |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + guard.visited |
| 49 | + # rescue LoopDetected |
| 50 | + # return :loop_detected |
| 51 | + ensure |
| 52 | + map.set(new_obstruction, '.') if new_obstruction |
| 53 | + end |
| 54 | + |
| 55 | + EXAMPLE_INPUT = File.read("../inputs/day06-example-input.txt") |
| 56 | + |
| 57 | + class Guard |
| 58 | + attr_accessor :position, :facing |
| 59 | + attr_reader :visited |
| 60 | + |
| 61 | + DIRECTIONS = { |
| 62 | + '^' => 0, |
| 63 | + '>' => 90, |
| 64 | + 'v' => 180, |
| 65 | + '<' => 270, |
| 66 | + } |
| 67 | + |
| 68 | + def initialize(position, facing) |
| 69 | + @position = position |
| 70 | + @facing = DIRECTIONS.fetch(facing) |
| 71 | + @visited = Set.new.add([@position, @facing]) |
| 72 | + end |
| 73 | + |
| 74 | + def next_step |
| 75 | + case facing |
| 76 | + when 0 ; [ position[0] -1, position[1] ] |
| 77 | + when 90 ; [ position[0] , position[1] +1 ] |
| 78 | + when 180 ; [ position[0] +1, position[1] ] |
| 79 | + when 270 ; [ position[0] , position[1] -1 ] |
| 80 | + else |
| 81 | + raise "lolwut: weird facing direction #{facing}" |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + class LoopDetected < RuntimeError; end |
| 86 | + |
| 87 | + def step_forward! |
| 88 | + @position = next_step |
| 89 | + @visited.add?([@position, @facing]) or raise LoopDetected |
| 90 | + end |
| 91 | + |
| 92 | + def turn_right! |
| 93 | + @facing = (facing + 90).modulo(360) |
| 94 | + end |
| 95 | + end |
| 96 | +end |
| 97 | + |
| 98 | +require 'rspec' |
| 99 | + |
| 100 | +describe 'Day06' do |
| 101 | + let(:day) { Day06.new(Day06::EXAMPLE_INPUT) } |
| 102 | + it 'answers part 1' do |
| 103 | + expect(day.part1).to eq(41) |
| 104 | + end |
| 105 | + |
| 106 | + it 'answers part 2' do |
| 107 | + day.part1 # part 2 depends on state determined from part 1 |
| 108 | + # expect(day.part2).to eq(6) |
| 109 | + expect(day.part2).to eq([[6,3], [7,6], [7,8], [8,1], [8,3], [9,7]]) |
| 110 | + end |
| 111 | + |
| 112 | + describe 'Guard' do |
| 113 | + subject { Day06::Guard.new([4,5], '^') } |
| 114 | + |
| 115 | + it "knows their next step" do |
| 116 | + expect(subject.next_step).to eq([3,5]) |
| 117 | + end |
| 118 | + |
| 119 | + it "can step forward" do |
| 120 | + subject.step_forward! |
| 121 | + expect(subject.position).to eq([3,5]) |
| 122 | + end |
| 123 | + |
| 124 | + it "can turn right" do |
| 125 | + subject.turn_right! |
| 126 | + expect(subject.facing).to eq(90) |
| 127 | + |
| 128 | + subject.turn_right! |
| 129 | + subject.turn_right! |
| 130 | + expect(subject.facing).to eq(270) |
| 131 | + end |
| 132 | + end |
| 133 | +end |
0 commit comments