-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.kt
156 lines (131 loc) · 5.11 KB
/
App.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
package day08.solution1
// Original solution used to solve the AoC day 8 puzzle. Warts and all.
import common.Solution
enum class Direction { LEFT, UP, RIGHT, DOWN }
data class Point(val row: Int, val col: Int)
typealias IntGrid = List<List<Int>>
val IntGrid.width: Int get() = this.first().size
val IntGrid.height: Int get() = this.size
fun IntGrid.pointInBounds(p: Point): Boolean =
p.row >= 0 && p.row < this.height && p.col >= 0 && p.col < this.width
fun IntGrid.pointOnEdge(p: Point): Boolean =
p.row == 0 || p.col == 0 || p.row == this.height - 1 || p.col == this.width - 1
fun IntGrid.valueAt(p: Point): Int = this[p.row][p.col]
fun IntGrid.valueAt(row: Int, col: Int): Int = this[row][col]
object Day08 : Solution.LinedInput<IntGrid>(day = 8) {
override fun parseInput(input: List<String>): IntGrid {
return input.mapIndexed() { rowIdx, line ->
line.toList().mapIndexed { colIdx, value -> value.digitToInt() }
}
}
override fun part1(grid: IntGrid): Any {
fun pointVisibleInDirection(p: Point, direction: Direction): Boolean {
if (!grid.pointInBounds(p)) {
return false
}
if (grid.pointOnEdge(p)) {
return true
}
return when (direction) {
Direction.LEFT -> (0 until p.col).map { c -> grid.valueAt(p) > grid.valueAt(p.row, c)
}.all { it }
Direction.UP -> (0 until p.row).map { r ->
grid.valueAt(p) > grid.valueAt(r, p.col)
}.all { it }
Direction.RIGHT -> (p.col + 1 until grid.width).map { c ->
grid.valueAt(p) > grid.valueAt(p.row, c)
}.all { it }
Direction.DOWN -> (p.row + 1 until grid.height).map { r ->
grid.valueAt(p) > grid.valueAt(r, p.col)
}.all { it }
}
}
fun pointHasVisibility(p: Point) = Direction.values().map { dir -> pointVisibleInDirection(p, dir) }.any { it }
// grid.forEachIndexed { rowIdx, row ->
// val s = row.mapIndexed { colIdx, value ->
// val visible = pointHasVisibility(Point(rowIdx, colIdx))
// "[$value:${visible.toString()[0]}]"
// }.joinToString(" ")
// println(s)
// }
return grid.mapIndexed { rowIdx, row ->
row.indices.map { colIdx -> pointHasVisibility(Point(rowIdx, colIdx)) }.count { it }
}.sum()
}
override fun part2(grid: IntGrid): Any {
fun pointVisibilityScoreInDirection(p: Point, direction: Direction): Int {
if (!grid.pointInBounds(p)) {
return -1
}
if (grid.pointOnEdge(p)) {
return 0
}
return when (direction) {
Direction.LEFT -> {
var score = 0
var c = p.col - 1
while (c >= 0) {
score += 1
if (grid.valueAt(p.row, c) >= grid.valueAt(p)) {
break
}
c -= 1
}
score
}
Direction.UP -> {
var score = 0
var r = p.row - 1
while (r >= 0) {
score += 1
if (grid.valueAt(r, p.col) >= grid.valueAt(p)) {
break
}
r -= 1
}
score
}
Direction.RIGHT -> {
var score = 0
var c = p.col + 1
while (c < grid.width) {
score += 1
if (grid.valueAt(p.row, c) >= grid.valueAt(p)) {
break
}
c += 1
}
score
}
Direction.DOWN -> {
var score = 0
var r = p.row + 1
while (r < grid.height) {
score += 1
if (grid.valueAt(r, p.col) >= grid.valueAt(p)) {
break
}
r += 1
}
score
}
}
}
fun pointVisibilityScore(p: Point): Int = Direction.values().map { dir ->
pointVisibilityScoreInDirection(p, dir) }.reduce { acc, value -> acc * value }
// grid.forEachIndexed { rowIdx, row ->
// val s = row.mapIndexed { colIdx, value ->
// val score = pointVisibilityScore(Point(rowIdx, colIdx))
// "[$value:${score}]"
// }.joinToString(" ")
// println(s)
// }
return grid.mapIndexed { rowIdx, row ->
row.indices.map { colIdx -> pointVisibilityScore(Point(rowIdx, colIdx)) }.max()
}.max()
return Unit
}
}
fun main() {
Day08.solve(test = false)
}