generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay17.kt
139 lines (109 loc) · 3.29 KB
/
Day17.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
private data class TargetPoint(
val x: Int,
val y: Int
) {
fun applySpeed(speed: TargetPoint): TargetPoint {
return TargetPoint(
x + speed.x,
y + speed.y,
)
}
fun correctSpeed(): TargetPoint {
return TargetPoint(
x = (x - 1).coerceAtLeast(0),
y = y - 1,
)
}
fun isInside(area: TargetArea): Boolean {
return x in area.xRange && y in area.yRange
}
}
private data class TargetArea(
val xRange: IntRange,
val yRange: IntRange,
) {
fun leftArea(
currentPosition: TargetPoint,
): Boolean {
return currentPosition.x > xRange.last ||
currentPosition.y < yRange.first
}
}
private val initialPoint = TargetPoint(0, 0)
private val inputTest = TargetArea(
xRange = 20..30,
yRange = -10..-5,
)
private val inputReal = TargetArea(
xRange = 79..137,
yRange = -176..-117,
)
private fun calculateAllVariantsForInput(
targetArea: TargetArea,
calculateAnswer: (Map<TargetPoint, Set<TargetPoint>>) -> Int,
): Int {
// Speed To Positions for correct items
val mutableSet = mutableMapOf<TargetPoint, Set<TargetPoint>>()
val xRange = targetArea.xRange
val yRange = targetArea.yRange
/**
* THIS IS HARDCODED VALUES - Instead of calculating it just put large values for x and y that works for both inputs.
*/
val hardcodedXRange = 0..xRange.last
val hardcodedYRange = yRange.first..200
for (x in hardcodedXRange) {
for (y in hardcodedYRange) {
val initialSpeed = TargetPoint(
x = x,
y = y,
)
val result = checkIsInTargetAreaWithInitialPosition(
targetArea = targetArea,
initialPoint = initialPoint,
initialSpeed = initialSpeed,
)
if (result.any { it.isInside(targetArea) }) {
mutableSet[initialSpeed] = result
}
}
}
return calculateAnswer(mutableSet)
}
private fun checkIsInTargetAreaWithInitialPosition(
targetArea: TargetArea,
initialPoint: TargetPoint,
initialSpeed: TargetPoint,
): Set<TargetPoint> {
val allPositions = mutableSetOf(initialPoint)
var currentSpeed = initialSpeed
var currentPosition = initialPoint
while (targetArea.leftArea(currentPosition).not()) {
currentPosition = currentPosition.applySpeed(currentSpeed)
currentSpeed = currentSpeed.correctSpeed()
allPositions.add(currentPosition)
}
return allPositions
}
fun main() {
fun part1(input: TargetArea): Int {
return calculateAllVariantsForInput(input) { mutableSet ->
mutableSet.mapValues { it.value.maxOf { it.y } }
.filter { it.value != 0 }
.maxOf { it.value }
}
}
fun part2(input: TargetArea): Int {
return calculateAllVariantsForInput(input) { mutableSet ->
mutableSet.keys.size
}
}
// test if implementation meets criteria from the description, like:
val part1Test = part1(inputTest)
check(part1Test == 45)
val part1 = part1(inputReal)
println("Part 1: $part1")
check(part1 == 15400)
val part2 = part2(inputReal)
println("Part 2: $part2")
check(part2 == 5844)
}