Skip to content

Commit b9e1b52

Browse files
authored
Merge pull request #1 from palinkiewicz/2023_d01
2023 Day 01
2 parents 9e3054a + 5276a03 commit b9e1b52

File tree

2 files changed

+1049
-0
lines changed

2 files changed

+1049
-0
lines changed

adventofcode2023/Day01.kt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package adventofcode2023
2+
3+
import java.io.File
4+
5+
fun part1(inputs: List<String>) {
6+
println(inputs.sumOf { line ->
7+
val digits = line.filter { char -> char.isDigit() }
8+
digits.first().digitToInt() * 10 + digits.last().digitToInt()
9+
})
10+
}
11+
12+
fun part2(inputs: List<String>) {
13+
val digits = setOf(
14+
Pair("one", '1'),
15+
Pair("two", '2'),
16+
Pair("three", '3'),
17+
Pair("four", '4'),
18+
Pair("five", '5'),
19+
Pair("six", '6'),
20+
Pair("seven", '7'),
21+
Pair("eight", '8'),
22+
Pair("nine", '9')
23+
)
24+
25+
fun findDigit(line: String, indices: IntProgression = line.indices): Int {
26+
for (i in indices) {
27+
for (digit in digits) {
28+
if ((line.length >= i + digit.first.length
29+
&& line.substring(i, i + digit.first.length) == digit.first)
30+
|| line[i] == digit.second
31+
) {
32+
return digit.second.digitToInt()
33+
}
34+
}
35+
}
36+
37+
return 0
38+
}
39+
40+
println(inputs.sumOf { line ->
41+
findDigit(line) * 10 + findDigit(line, line.indices.reversed())
42+
})
43+
}
44+
45+
fun main() {
46+
val inputs = File("resources/adventofcode2023/Day01.txt").readLines()
47+
part1(inputs)
48+
part2(inputs)
49+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy