-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.rb
77 lines (68 loc) · 2.21 KB
/
part2.rb
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
# frozen_string_literal: true
require_relative "part1"
require_relative "gear"
module AdventOfCode
module Puzzles2023
module Day03
##
# Class for solving Day 3 - Part 2 puzzle
class Part2 < Part1
##
# Array of Gear objects
# @return [Array<Gear>] array of gears
attr_reader :gears
##
# @param file [String] path to input file
def initialize(file: nil)
super(file:)
@gears = find_gears
end
##
# Get the sum of the ratios of the gears.
#
# @return [Integer] sum of the ratios of the gears
def answer
gears.sum(&:ratio)
end
protected
##
# Find the gears in the puzzle.
#
# @return [Array<Gear>] array of gears
def find_gears
gears = []
symbols.each_with_index do |row_symbols, row_index|
row_symbols.each do |symbol_index, symbol|
adjacent_numbers = find_adjacent_numbers(row_index, symbol_index)
if adjacent_numbers.length == 2
gears << Gear.new(symbol:, index: "#{row_index}:#{symbol_index}", numbers: adjacent_numbers)
end
end
end
gears
end
##
# Find the numbers next to the given symbol.
#
# @param row_index [Integer] index of the row to check
# @param symbol_index [Integer] index of the symbol to check
#
# @return [Array<Integer>] array of numbers next to the symbol
def find_adjacent_numbers(row_index, symbol_index)
# Adjacent rows
adjacent_number_rows = adjacent_rows_range(row_index, numbers.length - 1)
# Check numbers next to the current symbol
adjacent_numbers = []
adjacent_number_rows.each do |number_row_idx|
numbers[number_row_idx].each do |number_idx, number|
# Indexes occupied by the current number
number_limits = number_idx - 1..number_idx + number.length
adjacent_numbers << number.to_i if number_limits.include?(symbol_index)
end
end
adjacent_numbers
end
end
end
end
end