Skip to content

Commit

Permalink
Day 13 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
gregawoods committed Dec 14, 2023
1 parent 0854b4f commit 494e969
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
AllCops:
TargetRubyVersion: 3.2.2
TargetRubyVersion: 3.3.0
SuggestExtensions: false
NewCops: enable
Exclude:
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ GEM

PLATFORMS
arm64-darwin-21
arm64-darwin-23
x64-mingw-ucrt

DEPENDENCIES
Expand Down
1 change: 1 addition & 0 deletions app.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
RubyVM::YJIT.enable
APP_ROOT = __dir__

require 'debug'
Expand Down
2 changes: 1 addition & 1 deletion lib/days/12.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def convert_data(data)
data.join.split(/\.+/).reject(&:empty?).map(&:length)
end

def part2(input)
def part2(_input)
'TODO'
end

Expand Down
65 changes: 34 additions & 31 deletions lib/days/13.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,24 @@
class Day13

def part1(input)
cols = 0
rows = 0

input.split("\n\n").each do |chunk|
mirror_column = find_mirror_column(chunk)

if mirror_column
cols += mirror_column + 1
else
mirror_row = find_mirror_row(chunk)
rows += mirror_row + 1
end
input.split("\n\n").sum do |chunk|
find_reflections(chunk).first
end

(rows * 100) + cols
end

def find_mirror_column(chunk)
def find_mirror_columns(chunk)
rows = chunk.split("\n")

mirror_column = nil
mirror_columns = []

(0..(rows.first.length - 2)).each do |n|
(1..(rows.first.length - 1)).each do |n|
is_mirror = true

(0..(rows.length - 1)).each do |i|
row = rows[i]

left = row[0..n]
right = row[(n + 1), left.length]
left = row[0, n]
right = row[n, left.length]

if left.length > right.length
left = left[(left.length - right.length)..]
Expand All @@ -43,27 +31,26 @@ def find_mirror_column(chunk)
end

if is_mirror
mirror_column = n
break
mirror_columns << n
end
end

mirror_column
mirror_columns
end

def find_mirror_row(chunk)
def find_mirror_rows(chunk)
rows = chunk.split("\n")

mirror_row = nil
mirror_rows = []

(0..(rows.length - 2)).each do |n|
(1..(rows.length - 1)).each do |n|
is_mirror = true

(0..(rows[n].length - 1)).each do |i|
column = rows.map { |r| r[i] }.join

top = column[0..n]
bottom = column[(n + 1), top.length]
top = column[0, n]
bottom = column[n, top.length]

if top.length > bottom.length
top = top[(top.length - bottom.length)..]
Expand All @@ -76,16 +63,32 @@ def find_mirror_row(chunk)
end

if is_mirror
mirror_row = n
break
mirror_rows << (n * 100)
end
end

mirror_row
mirror_rows
end

def find_reflections(chunk)
find_mirror_columns(chunk) + find_mirror_rows(chunk)
end

def part2(input)
'TODO'
input.split("\n\n").sum do |chunk|
original_reflection = find_reflections(chunk).first

chunk.length.times do |i|
next if chunk[i] == "\n"

new_chunk = chunk.dup
new_chunk[i] = new_chunk[i] == '.' ? '#' : '.'

reflection = find_reflections(new_chunk).find { |r| r != original_reflection }

break reflection if reflection
end
end
end

end
2 changes: 1 addition & 1 deletion test/12_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ def test_part1
end

def test_part2
assert_equal @day.part2(@data), ''
assert_equal @day.part2(@data), 'TODO'
end
end
2 changes: 1 addition & 1 deletion test/13_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ def test_part1
end

def test_part2
assert_equal @day.part2(@data), ''
assert_equal @day.part2(@data), 400
end
end

0 comments on commit 494e969

Please sign in to comment.