Skip to content

Commit eb40b87

Browse files
authored
Added Solution to Puzzle 13 in Kotlin
1 parent 7a45540 commit eb40b87

File tree

1 file changed

+177
-0
lines changed

1 file changed

+177
-0
lines changed

13/main.kt

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
import java.io.File
2+
3+
fun parseFile(filePath: String): List<List<String>> {
4+
val content = File(filePath).readText()
5+
return content.split("\n\n").map { it.split("\n") }
6+
}
7+
8+
fun colsEqual(col1: Int, col2: Int, pattern: Array<Array<Char>>): Boolean {
9+
for (rowIndex in 0 until pattern.size) {
10+
if (pattern[rowIndex][col1] != pattern[rowIndex][col2]) {
11+
return false;
12+
}
13+
}
14+
return true;
15+
}
16+
17+
fun isVerticalMirror(colIndex: Int, pattern: Array<Array<Char>>): Boolean {
18+
var max = Math.min(colIndex, pattern[0].size - colIndex)
19+
for (i in 0 until max) {
20+
var col1 = colIndex - i - 1;
21+
var col2 = colIndex + i;
22+
if (! colsEqual(col1, col2, pattern)) {
23+
return false;
24+
}
25+
}
26+
return true;
27+
}
28+
29+
// returns the number of cols left to the mirror
30+
fun findVerticalMirror(pattern: Array<Array<Char>>): Int {
31+
for (colIndex in 1 until pattern[0].size) {
32+
if (isVerticalMirror(colIndex, pattern)) {
33+
return colIndex;
34+
}
35+
}
36+
return 0;
37+
}
38+
39+
fun isHorizontalMirror(rowIndex: Int, pattern: Array<Array<Char>>): Boolean {
40+
var max = Math.min(rowIndex, pattern.size - rowIndex)
41+
for (i in 0 until max) {
42+
var row1 = pattern[rowIndex - i - 1]
43+
var row2 = pattern[rowIndex + i]
44+
if (! row1.contentEquals(row2)) {
45+
return false;
46+
}
47+
}
48+
return true;
49+
}
50+
51+
// returns the number of rows above the mirror
52+
fun findHorizontalMirror(pattern: Array<Array<Char>>): Int {
53+
for (rowIndex in 1 until pattern.size) {
54+
if (isHorizontalMirror(rowIndex, pattern)) {
55+
return rowIndex;
56+
}
57+
}
58+
return 0;
59+
}
60+
61+
fun solvePattern(patternInput: List<String>): Int {
62+
var pattern: Array<Array<Char>> = patternInput.map {
63+
it.toCharArray().toTypedArray() }.toTypedArray()
64+
65+
var horizontalMirror = findHorizontalMirror(pattern);
66+
var verticalMirror = findVerticalMirror(pattern);
67+
68+
return verticalMirror + (horizontalMirror * 100);
69+
}
70+
71+
fun solve(patterns: List<List<String>>) {
72+
println(patterns.map({ solvePattern(it) }).sum());
73+
}
74+
75+
76+
// compares the rows and accepts one difference
77+
// return 0 if equal, 1 if equal with one nudge, 2 if more than two nudges are required
78+
fun colsEqualWithNudge(col1: Int, col2: Int, pattern: Array<Array<Char>>): Int {
79+
var nudges = 0;
80+
for (rowIndex in 0 until pattern.size) {
81+
if (pattern[rowIndex][col1] != pattern[rowIndex][col2]) {
82+
nudges++;
83+
}
84+
85+
if (nudges > 1) {
86+
return 2;
87+
}
88+
}
89+
return nudges;
90+
}
91+
92+
// compares the rows and accepts one difference
93+
// return 0 if equal, 1 if equal with one nudge, 2 if more than two nudges are required
94+
fun rowsEqualWithNudge(row1: Int, row2: Int, pattern: Array<Array<Char>>): Int {
95+
var nudges = 0;
96+
for (colIndex in 0 until pattern[0].size) {
97+
if (pattern[row1][colIndex] != pattern[row2][colIndex]) {
98+
nudges++;
99+
}
100+
101+
if (nudges > 1) {
102+
return 2;
103+
}
104+
}
105+
return nudges;
106+
}
107+
108+
fun isVerticalMirrorWithNudge(colIndex: Int, pattern: Array<Array<Char>>): Boolean {
109+
var max = Math.min(colIndex, pattern[0].size - colIndex);
110+
var nudges = 0;
111+
for (i in 0 until max) {
112+
var col1 = colIndex - i - 1;
113+
var col2 = colIndex + i;
114+
nudges += colsEqualWithNudge(col1, col2, pattern);
115+
116+
if (nudges > 1) {
117+
return false;
118+
}
119+
}
120+
return nudges == 1;
121+
}
122+
123+
// returns the number of cols left to the mirror
124+
fun findVerticalMirrorWithNudge(pattern: Array<Array<Char>>): Int {
125+
for (colIndex in 1 until pattern[0].size) {
126+
if (isVerticalMirrorWithNudge(colIndex, pattern)) {
127+
return colIndex;
128+
}
129+
}
130+
return 0;
131+
}
132+
133+
fun isHorizontalMirrorWithNudge(rowIndex: Int, pattern: Array<Array<Char>>): Boolean {
134+
var max = Math.min(rowIndex, pattern.size - rowIndex)
135+
var nudges = 0;
136+
137+
for (i in 0 until max) {
138+
var row1 = rowIndex - i - 1;
139+
var row2 = rowIndex + i;
140+
nudges += rowsEqualWithNudge(row1, row2, pattern);
141+
142+
if (nudges > 1) {
143+
return false;
144+
}
145+
}
146+
return nudges == 1;
147+
}
148+
149+
// returns the number of rows above the mirror
150+
fun findHorizontalMirrorWithNudge(pattern: Array<Array<Char>>): Int {
151+
for (rowIndex in 1 until pattern.size) {
152+
if (isHorizontalMirrorWithNudge(rowIndex, pattern)) {
153+
return rowIndex;
154+
}
155+
}
156+
return 0;
157+
}
158+
159+
fun solvePattern2(patternInput: List<String>): Int {
160+
var pattern: Array<Array<Char>> = patternInput.map {
161+
it.toCharArray().toTypedArray() }.toTypedArray()
162+
163+
var horizontalMirror = findHorizontalMirrorWithNudge(pattern);
164+
var verticalMirror = findVerticalMirrorWithNudge(pattern);
165+
166+
return verticalMirror + (horizontalMirror * 100);
167+
}
168+
169+
fun solve2(patterns: List<List<String>>) {
170+
println(patterns.map({ solvePattern2(it) }).sum());
171+
}
172+
173+
fun main(args: Array<String>) {
174+
var patterns = parseFile("input")
175+
solve(patterns);
176+
solve2(patterns);
177+
}

0 commit comments

Comments
 (0)