-
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
0a0904a
commit d249338
Showing
2 changed files
with
69 additions
and
20 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 |
---|---|---|
@@ -1,63 +1,112 @@ | ||
class Day14 | ||
|
||
def part1(input) | ||
def parse(input) | ||
lines = input.split("\n") | ||
|
||
columns = [] | ||
grid = [] | ||
|
||
lines.length.times do |n| | ||
lines[n].chars.each_with_index do |char, i| | ||
columns[i] ||= [] | ||
columns[i] << char | ||
grid[i] ||= [] | ||
grid[i] << char | ||
end | ||
end | ||
|
||
columns = columns.map do |c| | ||
shift_column(c) | ||
end | ||
grid | ||
end | ||
|
||
def calculate(grid) | ||
value = 0 | ||
|
||
columns.each do |col| | ||
grid.each do |col| | ||
col.each_with_index do |obj, index| | ||
if obj == 'O' | ||
value += (col.length - index) | ||
end | ||
value += (col.length - index) if obj == 'O' | ||
end | ||
end | ||
|
||
value | ||
end | ||
|
||
def shift_column(column) | ||
new_column = column | ||
|
||
column.length.times do |n| | ||
next if n.zero? | ||
|
||
n.times do |i| | ||
position = n - i | ||
|
||
char = column[position] | ||
prev_char = column[position - 1] | ||
char = new_column[position] | ||
prev_char = new_column[position - 1] | ||
|
||
if char == 'O' && prev_char == '.' | ||
column[position] = '.' | ||
column[position - 1] = 'O' | ||
new_column[position] = '.' | ||
new_column[position - 1] = 'O' | ||
else | ||
break | ||
end | ||
end | ||
end | ||
|
||
column | ||
new_column | ||
end | ||
|
||
def shift_grid(grid) | ||
grid.map do |column| | ||
shift_column(column) | ||
end | ||
end | ||
|
||
def rotate_clockwise(grid) | ||
new_grid = [] | ||
|
||
grid.each do |column| | ||
column.reverse.each_with_index do |item, index| | ||
new_grid[index] ||= [] | ||
new_grid[index] << item | ||
end | ||
end | ||
|
||
new_grid | ||
end | ||
|
||
def rotate_clockwise(columns) | ||
new_columns = [] | ||
def print_grid(grid) | ||
grid.first.length.times do |y| | ||
grid.length.times do |x| | ||
print grid[x][y] | ||
end | ||
print "\n" | ||
end | ||
end | ||
|
||
def part1(input) | ||
calculate(shift_grid(parse(input))) | ||
end | ||
|
||
def part2(input) | ||
'TODO' | ||
grid = parse(input) | ||
|
||
history = [grid.dup] | ||
|
||
iterations = 1_000_000_000 | ||
|
||
iterations.times do |i| | ||
4.times do | ||
grid = rotate_clockwise(shift_grid(grid)) | ||
end | ||
|
||
if history.include?(grid) | ||
loop_start_idx = history.index(grid) | ||
the_loop = history[loop_start_idx..] | ||
idx = ((iterations - i) % the_loop.length) - 1 | ||
|
||
return calculate(the_loop[idx]) | ||
end | ||
|
||
history << grid.map(&:dup) | ||
end | ||
|
||
raise 'Should not get this far!' | ||
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 |
---|---|---|
|
@@ -13,6 +13,6 @@ def test_part1 | |
end | ||
|
||
def test_part2 | ||
assert_equal @day.part2(@data), '' | ||
assert_equal @day.part2(@data), 64 | ||
end | ||
end |