-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a6e78d9
commit 5f215c3
Showing
3 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#.##..##. | ||
..#.##.#. | ||
##......# | ||
##......# | ||
..#.##.#. | ||
..##..##. | ||
#.#.##.#. | ||
|
||
#...##..# | ||
#....#..# | ||
..##..### | ||
#####.##. | ||
#####.##. | ||
..##..### | ||
#....#..# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
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 | ||
end | ||
|
||
(rows * 100) + cols | ||
end | ||
|
||
def find_mirror_column(chunk) | ||
rows = chunk.split("\n") | ||
|
||
mirror_column = nil | ||
|
||
(0..(rows.first.length - 2)).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] | ||
|
||
if left.length > right.length | ||
left = left[(left.length - right.length)..] | ||
end | ||
|
||
if left != right.reverse | ||
is_mirror = false | ||
break | ||
end | ||
end | ||
|
||
if is_mirror | ||
mirror_column = n | ||
break | ||
end | ||
end | ||
|
||
mirror_column | ||
end | ||
|
||
def find_mirror_row(chunk) | ||
rows = chunk.split("\n") | ||
|
||
mirror_row = nil | ||
|
||
(0..(rows.length - 2)).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] | ||
|
||
if top.length > bottom.length | ||
top = top[(top.length - bottom.length)..] | ||
end | ||
|
||
if top != bottom.reverse | ||
is_mirror = false | ||
break | ||
end | ||
end | ||
|
||
if is_mirror | ||
mirror_row = n | ||
break | ||
end | ||
end | ||
|
||
mirror_row | ||
end | ||
|
||
def part2(input) | ||
'TODO' | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require 'minitest/autorun' | ||
require 'minitest/pride' | ||
require_relative '../app' | ||
|
||
class TestDay13 < Minitest::Test | ||
def setup | ||
@data = File.read(File.join(APP_ROOT, 'examples', '13.txt')).rstrip | ||
@day = Day13.new | ||
end | ||
|
||
def test_part1 | ||
assert_equal @day.part1(@data), 405 | ||
end | ||
|
||
def test_part2 | ||
assert_equal @day.part2(@data), '' | ||
end | ||
end |