Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4c26843

Browse files
author
juntingMa
committed
chore: finish day 15 part 1, left day 13,14,15-2
1 parent 146ddc4 commit 4c26843

File tree

6 files changed

+191
-43
lines changed

6 files changed

+191
-43
lines changed

‎src/Application.kt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ interface Application {
33
}
44

55
fun main(args: Array<String>) {
6-
val app: Application = Day12()
7-
val day = 12
6+
val app: Application = Day15()
7+
val day = 15
88
app.run("day${day.toString().padStart(2, '0')}test").printRes(day, true)
99
app.run("day${day.toString().padStart(2, '0')}").printRes(day, false)
1010
}

‎src/Day13.kt‎

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
class Day13 : Application {
2-
private var inputMap = mutableListOf<MutableList<Char>>()
32
override fun run(fileName: String): Pair<Long, Long> {
4-
inputMap = readInput(fileName).map { it.toMutableList() }.toMutableList()
5-
inputMap.println()
3+
val input = readInput(fileName)
64

75
val res1 = this.part1()
86
val res2 = this.part2()
@@ -11,47 +9,9 @@ class Day13 : Application {
119

1210
private fun part1(): Long {
1311
var res = 0L
14-
for (i in inputMap.indices) {
15-
for (j in inputMap.indices) {
16-
if (inputMap[i][j] != '.') {
17-
val output = compute(inputMap[i][j], Pair(i, j))
18-
res += output.first * output.second
19-
cleanUpMap()
20-
}
21-
}
22-
}
2312
return res
2413
}
2514

26-
private fun compute(target: Char, coord: Pair<Int, Int>): Pair<Int, Int> {
27-
if (coord.first > inputMap.lastIndex || coord.first < 0 || coord.second > inputMap[0].lastIndex || coord.second < 0) return 0 to 1
28-
when (inputMap[coord.first][coord.second]) {
29-
'-' -> return 0 to 0
30-
target -> {
31-
inputMap[coord.first][coord.second] = '-'
32-
return listOf(
33-
compute(target, Pair(coord.first - 1, coord.second)),
34-
compute(target, Pair(coord.first, coord.second + 1)),
35-
compute(target, Pair(coord.first + 1, coord.second)),
36-
compute(target, Pair(coord.first, coord.second - 1))
37-
).fold(Pair(1, 0)) { acc, pair -> Pair(acc.first + pair.first, acc.second + pair.second) }
38-
}
39-
else -> return 0 to 1
40-
}
41-
}
42-
43-
private fun cleanUpMap() {
44-
inputMap = inputMap.map {
45-
it.map { c ->
46-
when (c) {
47-
'-' -> '.'
48-
else -> c
49-
}
50-
}.toMutableList()
51-
}.toMutableList()
52-
}
53-
54-
5515
private fun part2(): Long {
5616
return 0
5717
}

‎src/Day15.kt‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
class Day15 : Application {
2+
var input: MutableList<MutableList<Char>> = mutableListOf()
3+
val directions = listOf(Pair(-1, 0), Pair(0, 1), Pair(1, 0), Pair(0, -1))
4+
5+
override fun run(fileName: String): Pair<Long, Long> {
6+
val input = readInput(fileName)
7+
val emptyListIdx = input.indexOf("")
8+
val inputMap = input.subList(0, emptyListIdx)
9+
inputMap.println()
10+
val steps = input.subList(emptyListIdx + 1, input.size).fold(StringBuilder()) { acc, s -> acc.append(s) }
11+
.toString().toList().map {
12+
when (it) {
13+
'^' -> 0
14+
'>' -> 1
15+
'v' -> 2
16+
else -> 3
17+
}
18+
}
19+
val initialCoord = getInitialCoord(inputMap)
20+
val res1 = this.part1(inputMap, steps, initialCoord)
21+
val res2 = this.part2()
22+
return res1 to res2
23+
}
24+
25+
private fun part1(inputMap: List<String>, steps: List<Int>, initialCoord: Pair<Int, Int>): Long {
26+
var res = 0L
27+
var coord = initialCoord
28+
input = inputMap.map { it.toMutableList() }.toMutableList()
29+
steps.forEach { n ->
30+
coord = computeStep(directions[n], coord)
31+
}
32+
input.forEachIndexed {i, list ->
33+
list.forEachIndexed { idx, c ->
34+
if (c == 'O') {
35+
res += 100 * i + idx
36+
}
37+
}
38+
}
39+
return res
40+
}
41+
42+
private fun part2(): Long {
43+
return 0
44+
}
45+
46+
private fun getInitialCoord(inputMap: List<String>): Pair<Int, Int> {
47+
inputMap.forEachIndexed { i, s ->
48+
s.forEachIndexed { j, c ->
49+
if (c == '@') return Pair(i, j)
50+
}
51+
}
52+
return Pair(0, 0)
53+
}
54+
55+
private fun computeStep(dir: Pair<Int, Int>, coord: Pair<Int, Int>): Pair<Int, Int> {
56+
val isNextCoordEmpty = try {
57+
input[coord.first + dir.first][coord.second + dir.second] == '.'
58+
} catch (e: IndexOutOfBoundsException) {
59+
return coord
60+
}
61+
if (isNextCoordEmpty) {
62+
input[coord.first][coord.second] = '.'
63+
input[coord.first + dir.first][coord.second + dir.second] = '@'
64+
return Pair(coord.first + dir.first, coord.second + dir.second)
65+
} else {
66+
if (input[coord.first + dir.first][coord.second + dir.second] == '#') return coord
67+
val distToEmptyCoord = distToEmptyCoord(dir, coord)
68+
if (distToEmptyCoord == -1) return coord
69+
else {
70+
input[coord.first + dir.first * distToEmptyCoord][coord.second + dir.second * distToEmptyCoord] = 'O'
71+
input[coord.plus(dir).first][coord.plus(dir).second] = '@'
72+
input[coord.first][coord.second] = '.'
73+
return coord.plus(dir)
74+
}
75+
}
76+
}
77+
78+
private fun distToEmptyCoord(dir: Pair<Int, Int>, coord: Pair<Int, Int>): Int {
79+
var res = 0
80+
var continueSearch = true
81+
var coords = coord
82+
while(continueSearch) {
83+
coords = coords.plus(dir)
84+
try {
85+
if (input[coords.first][coords.second] == '.') return ++res
86+
if (input[coords.first][coords.second] == '#') return -1
87+
if (input[coords.first][coords.second] == 'O') res++
88+
} catch (e: IndexOutOfBoundsException) {
89+
return -1
90+
}
91+
}
92+
return -1
93+
}
94+
}

‎src/Utils.kt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ fun String.md5() = BigInteger(1, MessageDigest.getInstance("MD5").digest(toByteA
1616
* The cleaner shorthand for printing output.
1717
*/
1818
fun Any?.println() = println(this)
19+
20+
fun Pair<Int, Int>.plus(b: Pair<Int, Int>) = Pair(this.first + b.first, this.second + b.second)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /