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 532dae4

Browse files
author
juntingMa
committed
chore: finish day 14
1 parent 4c26843 commit 532dae4

File tree

18 files changed

+4355
-5
lines changed

18 files changed

+4355
-5
lines changed

‎.DS_Store‎

6 KB
Binary file not shown.

‎.gitignore‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
.kotlin/
44
build
55
local.properties
6+
7+
src/resources/day14-output.txt
8+
src/resources/day14test-output.txt

‎src/.DS_Store‎

8 KB
Binary file not shown.

‎src/Application.kt‎

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

55
fun main(args: Array<String>) {
6-
val app: Application = Day15()
7-
val day = 15
6+
val app: Application = Day14()
7+
val day = 14
8+
89
app.run("day${day.toString().padStart(2, '0')}test").printRes(day, true)
910
app.run("day${day.toString().padStart(2, '0')}").printRes(day, false)
1011
}

‎src/Day14.kt‎

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import java.io.BufferedWriter
2+
import java.io.File
3+
4+
class Day14 : Application {
5+
var bufferedWriter: BufferedWriter? = null
6+
var xlim = 0
7+
var ylim = 0
8+
var initialPositions = mutableListOf<Pair<Int, Int>>()
9+
var moves = mutableListOf<Pair<Int, Int>>()
10+
override fun run(fileName: String): Pair<Long, Long> {
11+
if (fileName == "day14test") {
12+
xlim = 7
13+
ylim = 11
14+
} else {
15+
xlim = 103
16+
ylim = 101
17+
}
18+
bufferedWriter = File("src/resources/$fileName-output.txt").bufferedWriter()
19+
val input = readInput(fileName)
20+
val inputPos = input.map {
21+
Pair(
22+
it.split(" ")[0].split("=")[1].split(",")[1].toInt(),
23+
it.split(" ")[0].split("=")[1].split(",")[0].toInt()
24+
)
25+
}
26+
initialPositions = inputPos.toMutableList()
27+
val inputMoves = input.map {
28+
Pair(
29+
it.split(" ")[1].split("=")[1].split(",")[1].toInt(),
30+
it.split(" ")[1].split("=")[1].split(",")[0].toInt()
31+
)
32+
}
33+
moves = inputMoves.toMutableList()
34+
inputPos.println()
35+
inputMoves.println()
36+
val res1 = this.part1()
37+
val res2 = this.part2()
38+
bufferedWriter?.close()
39+
return res1 to res2
40+
}
41+
42+
private fun part1(): Long {
43+
val res = mutableListOf(0, 0, 0, 0)
44+
initialPositions.zip(moves).forEach {
45+
val finalPos = move100Times(it.first, it.second)
46+
when {
47+
finalPos.first < xlim / 2 && finalPos.second < ylim / 2 -> res[0]++
48+
finalPos.first > xlim / 2 && finalPos.second < ylim / 2 -> res[1]++
49+
finalPos.first < xlim / 2 && finalPos.second > ylim / 2 -> res[2]++
50+
finalPos.first > xlim / 2 && finalPos.second > ylim / 2 -> res[3]++
51+
}
52+
}
53+
res.println()
54+
return res.fold(1) { acc, i -> acc * (if (i == 0) 1 else i) }
55+
}
56+
57+
private fun move100Times(pos: Pair<Int, Int>, move: Pair<Int, Int>): Pair<Int, Int> {
58+
var newPos = pos
59+
for (i in 0 until 100) {
60+
newPos = moveRobot(newPos, move)
61+
}
62+
return newPos
63+
}
64+
65+
private fun moveRobot(pos: Pair<Int, Int>, move: Pair<Int, Int>): Pair<Int, Int> {
66+
var x = pos.first + move.first
67+
if (x >= xlim) x -= xlim
68+
else if (x < 0) x += xlim
69+
var y = pos.second + move.second
70+
if (y >= ylim) y -= ylim
71+
else if (y < 0) y += ylim
72+
return Pair(x, y)
73+
}
74+
75+
private fun part2(): Long {
76+
var res = 0L
77+
var positions = initialPositions
78+
for (i in 0 until 10000) {
79+
var notConnected = false
80+
positions = positions.zip(moves).map {
81+
moveRobot(it.first, it.second)
82+
}.toMutableList()
83+
bufferedWriter?.write("------------------------ Day: ${i + 1} ------------------------")
84+
bufferedWriter?.newLine()
85+
86+
positions.draw()
87+
positions.forEach {
88+
if (
89+
!positions.contains(Pair(it.first + 1, it.second + 1)) &&
90+
!positions.contains(Pair(it.first - 1, it.second + 1)) &&
91+
!positions.contains(Pair(it.first + 1, it.second - 1)) &&
92+
!positions.contains(Pair(it.first - 1, it.second - 1))
93+
) notConnected = true
94+
}
95+
if (!notConnected) {
96+
res = i.toLong()
97+
break
98+
}
99+
}
100+
return res
101+
}
102+
103+
private fun MutableList<Pair<Int, Int>>.draw() {
104+
val map = MutableList(xlim) { MutableList(ylim) { '.' } }
105+
forEach { map[it.first][it.second] = 'o' }
106+
val strs = map.map {
107+
val strBuilder = StringBuilder()
108+
it.forEach { c ->
109+
when {
110+
c == '.' -> strBuilder.append(' ')
111+
else -> strBuilder.append('o')
112+
}
113+
}
114+
strBuilder.toString()
115+
}
116+
strs.forEach {
117+
bufferedWriter?.write(it)
118+
bufferedWriter?.newLine()
119+
// .use { bw ->
120+
// bw?.write(it)
121+
// bw?.newLine()
122+
// }
123+
}
124+
}
125+
}

‎src/Day15.kt‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ class Day15 : Application {
1818
}
1919
val initialCoord = getInitialCoord(inputMap)
2020
val res1 = this.part1(inputMap, steps, initialCoord)
21+
val part2Input = input.map {
22+
it.fold(StringBuilder()) { strBuilder, c ->
23+
when (c) {
24+
'.' -> strBuilder.append("..")
25+
'@' -> strBuilder.append("@.")
26+
'O' -> strBuilder.append("[]")
27+
'#' -> strBuilder.append("##")
28+
else -> strBuilder
29+
}
30+
}.toMutableList()
31+
}
32+
part2Input.println()
2133
val res2 = this.part2()
2234
return res1 to res2
2335
}
@@ -29,7 +41,7 @@ class Day15 : Application {
2941
steps.forEach { n ->
3042
coord = computeStep(directions[n], coord)
3143
}
32-
input.forEachIndexed {i, list ->
44+
input.forEachIndexed {i, list ->
3345
list.forEachIndexed { idx, c ->
3446
if (c == 'O') {
3547
res += 100 * i + idx
@@ -67,7 +79,8 @@ class Day15 : Application {
6779
val distToEmptyCoord = distToEmptyCoord(dir, coord)
6880
if (distToEmptyCoord == -1) return coord
6981
else {
70-
input[coord.first + dir.first * distToEmptyCoord][coord.second + dir.second * distToEmptyCoord] = 'O'
82+
input[coord.first + dir.first * distToEmptyCoord][coord.second + dir.second * distToEmptyCoord] =
83+
'O'
7184
input[coord.plus(dir).first][coord.plus(dir).second] = '@'
7285
input[coord.first][coord.second] = '.'
7386
return coord.plus(dir)
@@ -79,7 +92,7 @@ class Day15 : Application {
7992
var res = 0
8093
var continueSearch = true
8194
var coords = coord
82-
while(continueSearch) {
95+
while(continueSearch) {
8396
coords = coords.plus(dir)
8497
try {
8598
if (input[coords.first][coords.second] == '.') return ++res

‎src/Day16.kt‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Day16 : Application {
2+
override fun run(fileName: String): Pair<Long, Long> {
3+
val input = readInput(fileName)
4+
5+
(-2 % 7).println()
6+
val res1 = this.part1()
7+
val res2 = this.part2()
8+
return res1 to res2
9+
}
10+
11+
private fun part1(): Long {
12+
var res = 0L
13+
return res
14+
}
15+
16+
private fun part2(): Long {
17+
return 0
18+
}
19+
}

‎src/Day17.kt‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Day17 : Application {
2+
override fun run(fileName: String): Pair<Long, Long> {
3+
val input = readInput(fileName)
4+
5+
val res1 = this.part1()
6+
val res2 = this.part2()
7+
return res1 to res2
8+
}
9+
10+
private fun part1(): Long {
11+
var res = 0L
12+
return res
13+
}
14+
15+
private fun part2(): Long {
16+
return 0
17+
}
18+
}

‎src/Day18.kt‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Day18 : Application {
2+
override fun run(fileName: String): Pair<Long, Long> {
3+
val input = readInput(fileName)
4+
5+
val res1 = this.part1()
6+
val res2 = this.part2()
7+
return res1 to res2
8+
}
9+
10+
private fun part1(): Long {
11+
var res = 0L
12+
return res
13+
}
14+
15+
private fun part2(): Long {
16+
return 0
17+
}
18+
}

‎src/resources/.DS_Store‎

10 KB
Binary file not shown.

0 commit comments

Comments
(0)

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