mirror of
https://github.com/onyx-and-iris/aoc2025.git
synced 2026-04-20 08:53:38 +00:00
Compare commits
22 Commits
65934bcaa0
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 65499624e7 | |||
| 3dbbab6fbe | |||
| 3db1c4c372 | |||
| 22afdfb126 | |||
| 3811f7b26f | |||
| cce0b68275 | |||
| dda849abd3 | |||
| 296866f017 | |||
| 0a3b029aba | |||
| 432d14e248 | |||
| c8c523dd66 | |||
| f9e99d1d2c | |||
| 2add30a6c1 | |||
| b1957249e1 | |||
| 45c6ec52b0 | |||
| 0180ee243c | |||
| 18c73c1bb7 | |||
| 2a3c0d36a0 | |||
| 08da9593cc | |||
| df944f69f2 | |||
| 6fbf18f804 | |||
| c0152a39b2 |
@@ -13,7 +13,7 @@ class Main
|
|||||||
when "R"
|
when "R"
|
||||||
current = (current + magnitude) % 100
|
current = (current + magnitude) % 100
|
||||||
when "L"
|
when "L"
|
||||||
current = (current -= magnitude) % 100
|
current = (current - magnitude) % 100
|
||||||
end
|
end
|
||||||
|
|
||||||
if current == 0
|
if current == 0
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ class Main
|
|||||||
$stdin.each_line do |line|
|
$stdin.each_line do |line|
|
||||||
direction, magnitude = line[0], line[1..].to_i
|
direction, magnitude = line[0], line[1..].to_i
|
||||||
|
|
||||||
while magnitude > 0
|
magnitude.times do
|
||||||
case direction
|
case direction
|
||||||
when "R"
|
when "R"
|
||||||
current = (current + 1) % 100
|
current = (current + 1) % 100
|
||||||
@@ -20,8 +20,6 @@ class Main
|
|||||||
if current == 0
|
if current == 0
|
||||||
count += 1
|
count += 1
|
||||||
end
|
end
|
||||||
|
|
||||||
magnitude -= 1
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
24
day_02/1.rb
Executable file
24
day_02/1.rb
Executable file
@@ -0,0 +1,24 @@
|
|||||||
|
#!/usr/bin/env ruby
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class Main
|
||||||
|
def run
|
||||||
|
@sum = 0
|
||||||
|
|
||||||
|
line = $stdin.gets.chomp
|
||||||
|
line.split(",").each do |range|
|
||||||
|
lo, hi = range.split("-")
|
||||||
|
(lo..hi).each do |num|
|
||||||
|
if num.length.even?
|
||||||
|
if num[0..(num.length / 2) - 1] == num[(num.length / 2)..]
|
||||||
|
@sum += num.to_i
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
puts @sum
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
Main.new.run
|
||||||
34
day_02/2.rb
Executable file
34
day_02/2.rb
Executable file
@@ -0,0 +1,34 @@
|
|||||||
|
#!/usr/bin/env ruby
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class Main
|
||||||
|
def run
|
||||||
|
@sum = 0
|
||||||
|
|
||||||
|
line = $stdin.gets.chomp
|
||||||
|
line.split(",").each do |range|
|
||||||
|
lo, hi = range.split("-")
|
||||||
|
(lo..hi).each do |num|
|
||||||
|
(1..num.length / 2).each do |n|
|
||||||
|
if num.length % n != 0
|
||||||
|
next
|
||||||
|
end
|
||||||
|
|
||||||
|
parts = num.scan(/(\d{#{n}})/).map { _1[0] }
|
||||||
|
if all_equal? parts
|
||||||
|
@sum += num.to_i
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
puts @sum
|
||||||
|
end
|
||||||
|
|
||||||
|
def all_equal?(arr)
|
||||||
|
arr.uniq.size <= 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
Main.new.run
|
||||||
19
day_03/1.rb
Executable file
19
day_03/1.rb
Executable file
@@ -0,0 +1,19 @@
|
|||||||
|
#!/usr/bin/env ruby
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class Main
|
||||||
|
def run
|
||||||
|
@sum = 0
|
||||||
|
|
||||||
|
$stdin.each_line do |line|
|
||||||
|
nums = line.chomp.each_char.map(&:to_i)
|
||||||
|
first_digit = nums[0..-2].max
|
||||||
|
second_digit = nums[nums.find_index(first_digit) + 1..].max
|
||||||
|
@sum += "#{first_digit}#{second_digit}".to_i
|
||||||
|
end
|
||||||
|
|
||||||
|
puts @sum
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
Main.new.run
|
||||||
27
day_03/2.rb
Executable file
27
day_03/2.rb
Executable file
@@ -0,0 +1,27 @@
|
|||||||
|
#!/usr/bin/env ruby
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class Main
|
||||||
|
def run
|
||||||
|
@sum = 0
|
||||||
|
|
||||||
|
$stdin.each_line do |line|
|
||||||
|
nums = line.chomp.each_char.map(&:to_i)
|
||||||
|
memo = recurse(12, nums, [])
|
||||||
|
@sum += memo.join.to_i
|
||||||
|
end
|
||||||
|
|
||||||
|
puts @sum
|
||||||
|
end
|
||||||
|
|
||||||
|
def recurse(i, nums, memo)
|
||||||
|
return memo if i == 0
|
||||||
|
|
||||||
|
max = nums[..-i].max
|
||||||
|
memo << max
|
||||||
|
|
||||||
|
recurse(i - 1, nums[nums.find_index(max) + 1..], memo)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
Main.new.run
|
||||||
33
day_04/1.rb
Executable file
33
day_04/1.rb
Executable file
@@ -0,0 +1,33 @@
|
|||||||
|
#!/usr/bin/env ruby
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require_relative "grid"
|
||||||
|
|
||||||
|
class Main
|
||||||
|
def run
|
||||||
|
data = []
|
||||||
|
$stdin.each_line do |line|
|
||||||
|
data << line.chomp
|
||||||
|
end
|
||||||
|
|
||||||
|
@sum = 0
|
||||||
|
|
||||||
|
grid = Grid.new(data)
|
||||||
|
(0...grid.size).each do |row_index|
|
||||||
|
(0...grid[row_index].size).each do |column_index|
|
||||||
|
cell = grid.cell_at(row_index, column_index)
|
||||||
|
next unless cell.value == "@"
|
||||||
|
|
||||||
|
if cell.neighbours.count do |neighbour|
|
||||||
|
neighbour.value == "@"
|
||||||
|
end < 4
|
||||||
|
@sum += 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
puts @sum
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
Main.new.run
|
||||||
40
day_04/2.rb
Executable file
40
day_04/2.rb
Executable file
@@ -0,0 +1,40 @@
|
|||||||
|
#!/usr/bin/env ruby
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require_relative "grid"
|
||||||
|
|
||||||
|
class Main
|
||||||
|
def run
|
||||||
|
data = []
|
||||||
|
$stdin.each_line do |line|
|
||||||
|
data << line.chomp
|
||||||
|
end
|
||||||
|
|
||||||
|
@sum = 0
|
||||||
|
|
||||||
|
grid = Grid.new(data)
|
||||||
|
loop do
|
||||||
|
grid_updated = false
|
||||||
|
(0...grid.size).each do |row_index|
|
||||||
|
(0...grid[row_index].size).each do |column_index|
|
||||||
|
cell = grid.cell_at(row_index, column_index)
|
||||||
|
next unless cell.value == "@"
|
||||||
|
|
||||||
|
if cell.neighbours.count do |neighbour|
|
||||||
|
neighbour.value == "@"
|
||||||
|
end < 4
|
||||||
|
grid[row_index][column_index].value = "x"
|
||||||
|
@sum += 1
|
||||||
|
grid_updated = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
break unless grid_updated
|
||||||
|
end
|
||||||
|
|
||||||
|
puts @sum
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
Main.new.run
|
||||||
22
day_04/cell.rb
Normal file
22
day_04/cell.rb
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
class Cell
|
||||||
|
attr_reader :row, :column
|
||||||
|
attr_accessor :value
|
||||||
|
attr_accessor :n, :ne, :e, :se, :s, :sw, :w, :nw
|
||||||
|
|
||||||
|
def initialize(row, column)
|
||||||
|
@row, @column = row, column
|
||||||
|
end
|
||||||
|
|
||||||
|
def neighbours
|
||||||
|
list = []
|
||||||
|
list << n if n
|
||||||
|
list << ne if ne
|
||||||
|
list << e if e
|
||||||
|
list << se if se
|
||||||
|
list << s if s
|
||||||
|
list << sw if sw
|
||||||
|
list << w if w
|
||||||
|
list << nw if nw
|
||||||
|
list
|
||||||
|
end
|
||||||
|
end
|
||||||
41
day_04/grid.rb
Normal file
41
day_04/grid.rb
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
require "forwardable"
|
||||||
|
|
||||||
|
require_relative "cell"
|
||||||
|
|
||||||
|
class Grid
|
||||||
|
attr_reader :columns
|
||||||
|
extend Forwardable
|
||||||
|
def_delegators :@data, :size, :[]
|
||||||
|
|
||||||
|
def initialize(input)
|
||||||
|
@data = []
|
||||||
|
(0...input.size).each do |row_index|
|
||||||
|
row = []
|
||||||
|
(0...input[row_index].size).each do |column_index|
|
||||||
|
cell = Cell.new(row_index, column_index)
|
||||||
|
cell.value = input[row_index][column_index]
|
||||||
|
row << cell
|
||||||
|
end
|
||||||
|
@data << row
|
||||||
|
end
|
||||||
|
|
||||||
|
# Link neighbours
|
||||||
|
(0...@data.size).each do |row_index|
|
||||||
|
(0...@data[row_index].size).each do |column_index|
|
||||||
|
cell = @data[row_index][column_index]
|
||||||
|
cell.n = @data[row_index - 1][column_index] if row_index - 1 >= 0
|
||||||
|
cell.ne = @data[row_index - 1][column_index + 1] if row_index - 1 >= 0 && column_index + 1 < @data[row_index - 1].size
|
||||||
|
cell.e = @data[row_index][column_index + 1] if column_index + 1 < @data[row_index].size
|
||||||
|
cell.se = @data[row_index + 1][column_index + 1] if row_index + 1 < @data.size && column_index + 1 < @data[row_index + 1].size
|
||||||
|
cell.s = @data[row_index + 1][column_index] if row_index + 1 < @data.size
|
||||||
|
cell.sw = @data[row_index + 1][column_index - 1] if row_index + 1 < @data.size && column_index - 1 >= 0
|
||||||
|
cell.w = @data[row_index][column_index - 1] if column_index - 1 >= 0
|
||||||
|
cell.nw = @data[row_index - 1][column_index - 1] if row_index - 1 >= 0 && column_index - 1 >= 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def cell_at(row, column)
|
||||||
|
@data[row][column]
|
||||||
|
end
|
||||||
|
end
|
||||||
28
day_05/1.rb
Executable file
28
day_05/1.rb
Executable file
@@ -0,0 +1,28 @@
|
|||||||
|
#!/usr/bin/env ruby
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class Main
|
||||||
|
def run
|
||||||
|
ranges = []
|
||||||
|
ingredient_ids = Hash.new(0)
|
||||||
|
$stdin.each_line do |line|
|
||||||
|
arr = line.chomp.split("-").map(&:to_i)
|
||||||
|
if arr.size == 2
|
||||||
|
ranges << (arr[0]..arr[1])
|
||||||
|
elsif arr.size == 1
|
||||||
|
ingredient_ids[arr[0]] += 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@sum = 0
|
||||||
|
ingredient_ids.each do |id, count|
|
||||||
|
if ranges.any? { |r| r.include?(id) }
|
||||||
|
@sum += count
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
puts @sum
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
Main.new.run
|
||||||
47
day_05/2.rb
Executable file
47
day_05/2.rb
Executable file
@@ -0,0 +1,47 @@
|
|||||||
|
#!/usr/bin/env ruby
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class Main
|
||||||
|
def run
|
||||||
|
ranges = []
|
||||||
|
$stdin.each_line do |line|
|
||||||
|
arr = line.chomp.split("-").map(&:to_i)
|
||||||
|
if arr.size == 2
|
||||||
|
ranges << (arr[0]..arr[1])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Sort ranges by starting point
|
||||||
|
# This prevents having to check all previous ranges for every new range
|
||||||
|
ranges.sort_by!(&:begin)
|
||||||
|
|
||||||
|
valid_id_ranges = []
|
||||||
|
ranges.each do |r|
|
||||||
|
valid_id_ranges.each do |vr|
|
||||||
|
if (
|
||||||
|
# r starts inside vr
|
||||||
|
r.begin >= vr.begin && r.begin <= vr.end) ||
|
||||||
|
# r ends inside vr
|
||||||
|
(r.end >= vr.begin && r.end <= vr.end) ||
|
||||||
|
# vr starts inside r
|
||||||
|
(vr.begin >= r.begin && vr.begin <= r.end) ||
|
||||||
|
# vr ends inside r
|
||||||
|
(vr.end >= r.begin && vr.end <= r.end)
|
||||||
|
# Merge ranges
|
||||||
|
r = ([r.begin, vr.begin].min..[r.end, vr.end].max)
|
||||||
|
valid_id_ranges.delete(vr)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
valid_id_ranges << r
|
||||||
|
end
|
||||||
|
|
||||||
|
@total = 0
|
||||||
|
valid_id_ranges.each do |r|
|
||||||
|
@total += (r.end - r.begin + 1)
|
||||||
|
end
|
||||||
|
|
||||||
|
puts @total
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
Main.new.run
|
||||||
53
day_06/1.rb
Executable file
53
day_06/1.rb
Executable file
@@ -0,0 +1,53 @@
|
|||||||
|
#!/usr/bin/env ruby
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class Main
|
||||||
|
def run
|
||||||
|
problems = Hash.new(0)
|
||||||
|
$stdin.each_line do |line|
|
||||||
|
arr = line.chomp.split.map do |part|
|
||||||
|
if /^\d+$/.match?(part)
|
||||||
|
part.to_i
|
||||||
|
else
|
||||||
|
part.to_sym
|
||||||
|
end
|
||||||
|
end.compact
|
||||||
|
|
||||||
|
arr.each_with_index do |part, idx|
|
||||||
|
problems[idx] = [] unless problems.key?(idx)
|
||||||
|
problems[idx] << part
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@sum = 0
|
||||||
|
problems.each do |_, parts|
|
||||||
|
@sum += recurse(parts[..-2], parts.last)
|
||||||
|
end
|
||||||
|
|
||||||
|
puts @sum
|
||||||
|
end
|
||||||
|
|
||||||
|
def recurse(nums, op)
|
||||||
|
return nums.first if nums.size == 1
|
||||||
|
|
||||||
|
num1, num2 = nums.shift(2)
|
||||||
|
|
||||||
|
case op
|
||||||
|
when :+
|
||||||
|
nums.unshift(num1 + num2)
|
||||||
|
when :-
|
||||||
|
nums.unshift(num1 - num2)
|
||||||
|
when :*
|
||||||
|
nums.unshift(num1 * num2)
|
||||||
|
when :/
|
||||||
|
if num2 == 0
|
||||||
|
raise "Division by zero"
|
||||||
|
end
|
||||||
|
nums.unshift(num1 / num2)
|
||||||
|
end
|
||||||
|
|
||||||
|
recurse(nums, op)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
Main.new.run
|
||||||
68
day_06/2.rb
Executable file
68
day_06/2.rb
Executable file
@@ -0,0 +1,68 @@
|
|||||||
|
#!/usr/bin/env ruby
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require "stringio"
|
||||||
|
|
||||||
|
class Main
|
||||||
|
def run
|
||||||
|
num_map = []
|
||||||
|
$stdin.each_line do |line|
|
||||||
|
num_map << line.chomp.chars
|
||||||
|
end
|
||||||
|
|
||||||
|
problems = Hash.new(0)
|
||||||
|
count = 0
|
||||||
|
problems[count] = Hash.new(0)
|
||||||
|
|
||||||
|
num_map.transpose.each do |chars|
|
||||||
|
if chars.uniq == [" "]
|
||||||
|
count += 1
|
||||||
|
problems[count] = Hash.new(0)
|
||||||
|
next
|
||||||
|
end
|
||||||
|
|
||||||
|
num_str = StringIO.new
|
||||||
|
chars.each do |ch|
|
||||||
|
if ch.match?(/\d/)
|
||||||
|
num_str.write(ch)
|
||||||
|
else
|
||||||
|
problems[count][:op] = [] unless problems[count].key?(:op)
|
||||||
|
problems[count][:op] << ch.to_sym unless ch == " "
|
||||||
|
end
|
||||||
|
end
|
||||||
|
problems[count][:nums] = [] unless problems[count].key?(:nums)
|
||||||
|
problems[count][:nums] << num_str.string.to_i unless num_str.string.empty?
|
||||||
|
end
|
||||||
|
|
||||||
|
@sum = 0
|
||||||
|
problems.each do |_, data|
|
||||||
|
@sum += recurse(data[:nums], data[:op].first)
|
||||||
|
end
|
||||||
|
|
||||||
|
puts @sum
|
||||||
|
end
|
||||||
|
|
||||||
|
def recurse(nums, op)
|
||||||
|
return nums.first if nums.size == 1
|
||||||
|
|
||||||
|
num1, num2 = nums.shift(2)
|
||||||
|
|
||||||
|
case op
|
||||||
|
when :+
|
||||||
|
nums.unshift(num1 + num2)
|
||||||
|
when :-
|
||||||
|
nums.unshift(num1 - num2)
|
||||||
|
when :*
|
||||||
|
nums.unshift(num1 * num2)
|
||||||
|
when :/
|
||||||
|
if num2 == 0
|
||||||
|
raise "Division by zero"
|
||||||
|
end
|
||||||
|
nums.unshift(num1 / num2)
|
||||||
|
end
|
||||||
|
|
||||||
|
recurse(nums, op)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
Main.new.run
|
||||||
42
day_07/1.rb
Executable file
42
day_07/1.rb
Executable file
@@ -0,0 +1,42 @@
|
|||||||
|
#!/usr/bin/env ruby
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require_relative "graph"
|
||||||
|
|
||||||
|
class Main
|
||||||
|
def run
|
||||||
|
input = $stdin.readlines(chomp: true)
|
||||||
|
graph = Graph.new(input)
|
||||||
|
|
||||||
|
total = dfs(graph.start, graph, 0, Set.new)
|
||||||
|
puts total
|
||||||
|
end
|
||||||
|
|
||||||
|
def dfs(next_point, graph, total, visited)
|
||||||
|
if next_point.y > graph.height || visited.include?(next_point)
|
||||||
|
return total
|
||||||
|
end
|
||||||
|
|
||||||
|
if graph.at(next_point) == "^"
|
||||||
|
step_west = Point.new(next_point.x - 1, next_point.y)
|
||||||
|
step_east = Point.new(next_point.x + 1, next_point.y)
|
||||||
|
|
||||||
|
if graph.at(step_west) == "." ||
|
||||||
|
graph.at(step_east) == "."
|
||||||
|
total += 1
|
||||||
|
end
|
||||||
|
|
||||||
|
# step west then dfs south
|
||||||
|
total = dfs(step_west, graph, total, visited)
|
||||||
|
# step east then dfs south
|
||||||
|
total = dfs(step_east, graph, total, visited)
|
||||||
|
return total
|
||||||
|
elsif graph.at(next_point) == "."
|
||||||
|
visited.add(next_point)
|
||||||
|
end
|
||||||
|
|
||||||
|
dfs(Point.new(next_point.x, next_point.y + 1), graph, total, visited) # south
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
Main.new.run
|
||||||
39
day_07/2.rb
Executable file
39
day_07/2.rb
Executable file
@@ -0,0 +1,39 @@
|
|||||||
|
#!/usr/bin/env ruby
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require_relative "graph"
|
||||||
|
|
||||||
|
class Main
|
||||||
|
def run
|
||||||
|
input = $stdin.readlines(chomp: true)
|
||||||
|
graph = Graph.new(input)
|
||||||
|
counts = Hash.new(0)
|
||||||
|
counts[graph.start.x] = 1
|
||||||
|
|
||||||
|
process_graph(graph, counts)
|
||||||
|
puts counts.values.sum
|
||||||
|
end
|
||||||
|
|
||||||
|
def process_graph(graph, counts)
|
||||||
|
graph.each_with_index do |line, y|
|
||||||
|
line.each_with_index do |char, x|
|
||||||
|
next unless char == "^"
|
||||||
|
|
||||||
|
distribute_counts(x, y, counts)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# When we encounter a "^", we split the count to the west and east positions
|
||||||
|
# and reset the current position count to 0.
|
||||||
|
def distribute_counts(x, y, counts)
|
||||||
|
peek_west = Point.new(x - 1, y)
|
||||||
|
peek_east = Point.new(x + 1, y)
|
||||||
|
|
||||||
|
counts[peek_west.x] += counts[x]
|
||||||
|
counts[peek_east.x] += counts[x]
|
||||||
|
counts[x] = 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
Main.new.run
|
||||||
32
day_07/graph.rb
Normal file
32
day_07/graph.rb
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
require "forwardable"
|
||||||
|
|
||||||
|
require_relative "point"
|
||||||
|
|
||||||
|
class Graph
|
||||||
|
attr_reader :start, :height, :width
|
||||||
|
extend Forwardable
|
||||||
|
def_delegators :@data, :[], :each_with_index
|
||||||
|
|
||||||
|
def initialize(input)
|
||||||
|
@data = input.each_with_index.map do |line, y|
|
||||||
|
if y.zero?
|
||||||
|
@start = Point.new(line.index("S"), y)
|
||||||
|
end
|
||||||
|
line.chars
|
||||||
|
end
|
||||||
|
@width = @data[0].size - 1
|
||||||
|
@height = @data.size - 1
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
@data.map { |row| row.join("") }.join("\n")
|
||||||
|
end
|
||||||
|
|
||||||
|
def at(point)
|
||||||
|
@data[point.y][point.x]
|
||||||
|
end
|
||||||
|
|
||||||
|
def out_of_bounds?(point)
|
||||||
|
point.x < 0 || point.x > @width || point.y < 0 || point.y > @height
|
||||||
|
end
|
||||||
|
end
|
||||||
24
day_07/point.rb
Normal file
24
day_07/point.rb
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
class Point
|
||||||
|
attr_reader :x, :y
|
||||||
|
|
||||||
|
def initialize(x, y)
|
||||||
|
@x = x
|
||||||
|
@y = y
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
"(#{x}, #{y})"
|
||||||
|
end
|
||||||
|
|
||||||
|
def ==(other)
|
||||||
|
x == other.x && y == other.y
|
||||||
|
end
|
||||||
|
|
||||||
|
def hash
|
||||||
|
[x, y].hash
|
||||||
|
end
|
||||||
|
|
||||||
|
def eql?(other)
|
||||||
|
self == other
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user