generated from kotlin-hands-on/advent-of-code-kotlin-template
-
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.
But why oh why does part 2 not work? Checks pass, termination not a problem but solution is rejected
- Loading branch information
1 parent
41219a9
commit 40f077a
Showing
3 changed files
with
1,480 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,78 @@ | ||
package solutions23 | ||
|
||
import solutions22.transpose | ||
|
||
fun main() { | ||
|
||
// returns number rows above the mirror | ||
fun<A> findReflectionLine(grid: Array<Array<A>>): Int { | ||
for(i in 1 until grid.size) { | ||
// take i lines above and check if the next i lines below are mirrored | ||
if((0 until i).all{ 2*i-(it+1) >= grid.size || grid[it].contentEquals(grid[2*i-(it+1)]) }) | ||
return i | ||
} | ||
return -1 | ||
} | ||
|
||
fun part1(input: List<List<String>>): Int { | ||
val grids = input.map { grid -> grid.map { row -> Array(row.length) {i -> row[i]} }.toTypedArray() } | ||
val horizontalRefls = grids.map { findReflectionLine(it) } | ||
var result = 0 | ||
for(i in grids.indices) { | ||
if(horizontalRefls[i] != -1) { | ||
result += horizontalRefls[i] * 100 | ||
} else { | ||
val verticalRefl = findReflectionLine(transpose(grids[i])) | ||
if(verticalRefl == -1) println("No reflection found") | ||
else result += verticalRefl | ||
} | ||
} | ||
return result | ||
} | ||
|
||
fun part2(input: List<List<String>>): Int { | ||
// for each grid and each element in the grid try flipping it and find a reflection line different from the original | ||
val grids = input.map { grid -> grid.map { row -> Array(row.length) {i -> row[i]} }.toTypedArray() } | ||
// save the original reflections | ||
val horizontalRefls = grids.map { findReflectionLine(it) } | ||
val verticalRefls = grids.map { findReflectionLine(transpose(it)) } | ||
|
||
var result = 0 | ||
for(i in grids.indices) { | ||
val grid = grids[i] | ||
rows@for(flipRow in grid.indices) { | ||
for(flipCol in grid[0].indices) { | ||
when(grid[flipRow][flipCol]) { | ||
'#' -> grid[flipRow][flipCol] = '.' | ||
'.' -> grid[flipRow][flipCol] = '#' | ||
} | ||
val horizontalRefl = findReflectionLine(grid) | ||
if(horizontalRefl != -1 && horizontalRefl != horizontalRefls[i]) { | ||
result += horizontalRefl * 100 | ||
break@rows | ||
} | ||
val verticalRefl = findReflectionLine(transpose(grid)) | ||
if(verticalRefl != -1 && verticalRefl != verticalRefls[i]) { | ||
result += verticalRefl | ||
break@rows | ||
} | ||
// flip back | ||
when(grid[flipRow][flipCol]) { | ||
'#' -> grid[flipRow][flipCol] = '.' | ||
'.' -> grid[flipRow][flipCol] = '#' | ||
} | ||
} | ||
} | ||
} | ||
return result | ||
} | ||
|
||
val testInput = parseChunks("Day13_test") | ||
check(part1(testInput) == 405) | ||
check(part2(testInput) == 400) | ||
|
||
val input = parseChunks("Day13") | ||
println(part1(input)) | ||
println(part2(input)) | ||
} | ||
|
Oops, something went wrong.