Skip to content

Commit

Permalink
day 13 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
gregawoods committed Dec 13, 2023
1 parent a6e78d9 commit 5f215c3
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
15 changes: 15 additions & 0 deletions examples/13.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#.##..##.
..#.##.#.
##......#
##......#
..#.##.#.
..##..##.
#.#.##.#.

#...##..#
#....#..#
..##..###
#####.##.
#####.##.
..##..###
#....#..#
91 changes: 91 additions & 0 deletions lib/days/13.rb
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
18 changes: 18 additions & 0 deletions test/13_test.rb
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

0 comments on commit 5f215c3

Please sign in to comment.