File tree Expand file tree Collapse file tree 1 file changed +38
-4
lines changed Expand file tree Collapse file tree 1 file changed +38
-4
lines changed Original file line number Diff line number Diff line change @@ -2,14 +2,48 @@ package adventofcode2023
2
2
3
3
import java.io.File
4
4
5
- fun part1 (file : File ) {
6
- println (file.readLines() .sumOf { line ->
5
+ fun part1 (inputs : List < String > ) {
6
+ println (inputs .sumOf { line ->
7
7
val digits = line.filter { char -> char.isDigit() }
8
8
digits.first().digitToInt() * 10 + digits.last().digitToInt()
9
9
})
10
10
}
11
11
12
- fun main (args : Array <String >) {
13
- val inputs = File (" resources/adventofcode2023/Day01.txt" )
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()
14
47
part1(inputs)
48
+ part2(inputs)
15
49
}
You can’t perform that action at this time.
0 commit comments