Skip to content

Commit 8db44e3

Browse files
committed
upload 2022
1 parent 2a2d68f commit 8db44e3

34 files changed

+3110
-418
lines changed

adventofcode2022/src/main/kotlin/com/matsemann/adventofcode2022/Day00.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.matsemann.adventofcode2022
22

3+
import com.matsemann.adventofcode2022.utils.*
34

45
fun day00_1(lines: List<String>): Any {
56
lines.forEach { println(it)}

adventofcode2022/src/main/kotlin/com/matsemann/adventofcode2022/Day01.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.matsemann.adventofcode2022
22

3+
import com.matsemann.adventofcode2022.utils.*
4+
35
fun day01_1(lines: List<String>): Any {
46
return lines
57
.splitBy { it == "" }
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.matsemann.adventofcode2022
2+
3+
import com.matsemann.adventofcode2022.utils.*
4+
5+
fun day02_1(lines: List<String>): Any {
6+
return lines
7+
.map {
8+
when (it) {
9+
"A X" -> 1 + 3
10+
"A Y" -> 2 + 6
11+
"A Z" -> 3 + 0
12+
"B X" -> 1 + 0
13+
"B Y" -> 2 + 3
14+
"B Z" -> 3 + 6
15+
"C X" -> 1 + 6
16+
"C Y" -> 2 + 0
17+
"C Z" -> 3 + 3
18+
else -> 0
19+
}
20+
}.sum()
21+
}
22+
23+
fun day02_2(lines: List<String>): Any {
24+
return lines
25+
.map {
26+
when (it) {
27+
"A X" -> 3 + 0
28+
"A Y" -> 1 + 3
29+
"A Z" -> 2 + 6
30+
"B X" -> 1 + 0
31+
"B Y" -> 2 + 3
32+
"B Z" -> 3 + 6
33+
"C X" -> 2 + 0
34+
"C Y" -> 3 + 3
35+
"C Z" -> 1 + 6
36+
else -> 0
37+
}
38+
}.sum()
39+
}
40+
41+
fun main() {
42+
43+
// run("1", fileName = "day02_ex.txt", func = ::day02_1)
44+
// run("2", fileName = "day02_ex.txt", func = ::day02_2)
45+
46+
run("1", fileName = "day02.txt", func = ::day02_1)
47+
run("2", fileName = "day02.txt", func = ::day02_2)
48+
}
49+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.matsemann.adventofcode2022
2+
3+
import com.matsemann.adventofcode2022.utils.*
4+
5+
fun day03_1(lines: List<String>): Any {
6+
return lines.map {
7+
val first = it.take(it.length / 2).toSet()
8+
val second = it.takeLast(it.length / 2).toSet()
9+
10+
(first intersect second).first().let {
11+
if (it.isUpperCase()) {
12+
it - 'A' + 27
13+
} else {
14+
it - 'a' + 1
15+
}
16+
}
17+
}.sum()
18+
}
19+
20+
21+
fun day03_2(lines: List<String>): Any {
22+
return lines
23+
.chunked(3)
24+
.map { it.map(String::toSet)}
25+
.map {(first, second, third) ->
26+
(first intersect second intersect third).first().let {
27+
if (it.isUpperCase()) {
28+
it - 'A' + 27
29+
} else {
30+
it - 'a' + 1
31+
}
32+
}
33+
}.sum()
34+
}
35+
36+
fun main() {
37+
38+
// run("1", fileName = "day03_ex.txt", func = ::day03_1)
39+
// run("2", fileName = "day03_ex.txt", func = ::day03_2)
40+
41+
run("1", fileName = "day03.txt", func = ::day03_1)
42+
run("2", fileName = "day03.txt", func = ::day03_2)
43+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.matsemann.adventofcode2022
2+
3+
import com.matsemann.adventofcode2022.utils.*
4+
import com.matsemann.adventofcode2022.utils.IntVec.Companion.toIntVec
5+
6+
7+
fun day04_1(lines: List<String>): Any {
8+
return lines.map { it.split(",") }
9+
.map { (first, second) ->
10+
first.toIntVec("-") to second.toIntVec("-")
11+
}.count { (first, second) ->
12+
first.x >= second.x && first.y <= second.y
13+
|| second.x >= first.x && second.y <= first.y
14+
}
15+
}
16+
17+
18+
fun day04_1_2(lines: List<String>): Any {
19+
return lines
20+
.map { it.split(",", "-").ints() }
21+
.count { (x1, y1, x2, y2) ->
22+
x1 >= x2 && y1 <= y2
23+
|| x2 >= x1 && y2 <= y1
24+
}
25+
}
26+
27+
fun day04_2_2(lines: List<String>): Any {
28+
return lines
29+
.map { it.split(",", "-").ints() }
30+
.count { (x1, y1, x2, y2) ->
31+
y1 >= x2 && x1 <= y2
32+
}
33+
}
34+
35+
fun main() {
36+
37+
run("1", fileName = "day04_ex.txt", func = ::day04_1_2)
38+
run("2", fileName = "day04_ex.txt", func = ::day04_2_2)
39+
40+
// run("1", fileName = "day04.txt", func = ::day04_2)
41+
// run("2", fileName = "day04.txt", func = ::day04_2_2)
42+
}
43+
44+
/*
45+
OUTPUT
46+
======
47+
Done. Took 53ms to run
48+
Result for 1: 536
49+
Copied to clipboard!
50+
51+
Done. Took 3ms to run
52+
Result for 2: 845
53+
Copied to clipboard!
54+
55+
*/
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.matsemann.adventofcode2022
2+
3+
import com.matsemann.adventofcode2022.utils.*
4+
5+
fun day05_1(lines: List<String>): Any {
6+
val (stacks, moves) = lines.splitBy { it == "" }
7+
8+
val crates = stacks.map { crt ->
9+
crt.chunked(4).map { it[1] }
10+
}.transpose().toMutableList()
11+
12+
moves.map { it.allInts() }
13+
.forEach { (numMoves, from, to) ->
14+
repeat(numMoves) {
15+
val popped = crates[from].removeFirst()
16+
crates[to].add(0, popped)
17+
}
18+
}
19+
20+
return crates.map { it.first() }.joinToString("")
21+
}
22+
23+
24+
fun day05_2(lines: List<String>): Any {
25+
val (stacks, moves) = lines.splitBy { it == "" }
26+
val crates = DefaultMap<Int, _> { mutableListOf<Char>()}
27+
28+
stacks.forEach { crt ->
29+
crt.chunked(4).map { it[1] }.mapIndexed { i, c ->
30+
if (c != ' ')
31+
crates[i].add(c)
32+
}
33+
}
34+
35+
moves.map { it.allInts() }.forEach { (num, from, to) ->
36+
val toMove = crates[from - 1].take(num)
37+
crates[from-1] = crates[from-1].drop(num).toMutableList()
38+
crates[to - 1].addAll(0, toMove)
39+
}
40+
41+
return crates.map { it.value.first() }.joinToString("")
42+
}
43+
44+
fun main() {
45+
46+
run("1", fileName = "day05_ex.txt", func = ::day05_1)
47+
run("2", fileName = "day05_ex.txt", func = ::day05_2)
48+
49+
run("1", fileName = "day05.txt", func = ::day05_1)
50+
run("2", fileName = "day05.txt", func = ::day05_2)
51+
}
52+
53+
/*
54+
Done. Took 39ms to run
55+
Result for 1: CMZ
56+
Copied to clipboard!
57+
58+
Done. Took 1ms to run
59+
Result for 2: MCD
60+
Copied to clipboard!
61+
62+
Done. Took 15ms to run
63+
Result for 1: FWNSHLDNZ
64+
Copied to clipboard!
65+
66+
Done. Took 6ms to run
67+
Result for 2: RNRGDNFQG
68+
Copied to clipboard!
69+
*/
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.matsemann.adventofcode2022
2+
3+
import com.matsemann.adventofcode2022.utils.*
4+
5+
fun day06_1(lines: List<String>): Any {
6+
return lines[0].windowed(4).indexOfFirst {
7+
it.toSet().size == 4
8+
} + 4
9+
}
10+
11+
fun day06_2(lines: List<String>): Any {
12+
return lines[0].windowed(14).indexOfFirst {
13+
it.toSet().size == 14
14+
} + 14
15+
}
16+
17+
fun main() {
18+
19+
// run("1", fileName = "day06_ex.txt", func = ::day06_1)
20+
// run("2", fileName = "day06_ex.txt", func = ::day06_2)
21+
22+
run("1", fileName = "day06.txt", func = ::day06_1)
23+
run("2", fileName = "day06.txt", func = ::day06_2)
24+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.matsemann.adventofcode2022
2+
3+
import com.matsemann.adventofcode2022.utils.*
4+
5+
data class Directory(val name: String, val parent: Directory?, val children: MutableList<Directory> = mutableListOf(), var files: Int = 0, var calculatedSize: Int = 0)
6+
7+
fun day07(lines: List<String>): Any {
8+
val root = Directory("/", parent = null)
9+
var current = root
10+
lines.drop(1).forEach {
11+
if (it.startsWith("$ cd ..")) {
12+
current = current.parent!!
13+
} else if (it.startsWith("$ cd")) {
14+
val path = it.drop(5)
15+
val newDir = Directory(path, parent = current)
16+
current.children.add(newDir)
17+
current = newDir
18+
} else if (it[0].isDigit()) {
19+
val size = it.split(" ")[0].toInt()
20+
current.files += size
21+
}
22+
}
23+
24+
traverse_calculate(root)
25+
val all = flatten(root)
26+
27+
val part1 = all.filter { it.calculatedSize < 100000 }.sumOf { it.calculatedSize }
28+
29+
val currentUsed = all.sumOf { it.files }
30+
val unused = 70000000 - currentUsed
31+
val toDelete = 30000000 - unused
32+
val part2 = all.sortedBy { it.calculatedSize }.first { it.calculatedSize > toDelete }.calculatedSize
33+
return part1 to part2
34+
}
35+
36+
fun traverse_calculate(directory: Directory): Int {
37+
val sizes = directory.children.sumOf { traverse_calculate(it) }
38+
directory.calculatedSize = sizes + directory.files
39+
return directory.calculatedSize
40+
}
41+
42+
fun flatten(directory: Directory): List<Directory> {
43+
val children = directory.children.flatMap { flatten(it) }
44+
return children + directory
45+
}
46+
47+
48+
fun main() {
49+
50+
run("1", fileName = "day07_ex.txt", func = ::day07)
51+
// run("2", fileName = "day07_ex.txt", func = ::day07_2)
52+
53+
run("1", fileName = "day07.txt", func = ::day07)
54+
// run("2", fileName = "day07.txt", func = ::day07_2)
55+
}
56+
57+
/*
58+
OUTPUT
59+
======
60+
Done. Took 37ms to run
61+
Result for 1: (95437, 24933642)
62+
Copied to clipboard!
63+
64+
Done. Took 4ms to run
65+
Result for 1: (1315285, 9847279)
66+
Copied to clipboard!
67+
68+
*/

0 commit comments

Comments
 (0)