File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change 1+ fun main () {
2+
3+ val pattern = " mul\\ (\\ d*,\\ d*\\ )" .toRegex()
4+
5+ fun String.runProgram () = pattern.findAll(this )
6+ .sumOf { finding ->
7+ finding.value
8+ .drop(4 )
9+ .dropLast(1 )
10+ .split(" ," )
11+ .map { it.toInt() }
12+ .let { it[0 ] * it[1 ] }
13+ }
14+
15+ fun part1 (input : List <String >): Int {
16+
17+ val result = input.sumOf { line -> line.runProgram() }
18+
19+ return result
20+ }
21+
22+ fun part2 (input : List <String >): Int {
23+
24+ val result = run {
25+ val substrings = input.joinToString(" " ).split(" don't()" )
26+ var sum = 0
27+ substrings.forEachIndexed { index, string ->
28+ sum + = when {
29+ index == 0 -> string.runProgram()
30+ else -> string.split(" do()" ).drop(1 ).sumOf { it.runProgram() }
31+ }
32+ }
33+ sum
34+ }
35+
36+ return result
37+ }
38+
39+ val testInput = readInput(" Day03_test" )
40+ val test2Input = readInput(" Day03_test2" )
41+ val input = readInput(" Day03" )
42+
43+ test(161 ) { part1(testInput) }
44+ exec { part1(input) }
45+ test(48 ) { part2(test2Input) }
46+ exec { part2(input) }
47+ }
You can’t perform that action at this time.
0 commit comments