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