File tree Expand file tree Collapse file tree 2 files changed +1049
-0
lines changed
resources/adventofcode2023 Expand file tree Collapse file tree 2 files changed +1049
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments