Skip to content

Commit faeaa49

Browse files
committed
3 solution
1 parent fcd515e commit faeaa49

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/Day03.kt

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
fun main() {
3+
var lines = readInput("files/day03")
4+
5+
// find all matches for mul(digits,digits)
6+
val regex = """mul\((?<first>\d+),(?<second>\d+)\)""".toRegex()
7+
var total = 0
8+
// lines.map{ line:String ->
9+
// var matchResult = regex.find(line)
10+
//
11+
// if (matchResult != null) {
12+
// while(matchResult?.groups?.get("first") != null) {
13+
// val first = matchResult.groups["first"]?.value?.toInt()
14+
// val second = matchResult.groups["second"]?.value?.toInt()
15+
// total += first!! * second!!
16+
//
17+
// matchResult = matchResult.next()
18+
// }
19+
// }
20+
// }
21+
// println(total)
22+
23+
// part 2
24+
25+
val regex2 = """don't.*?do\(\)""".toRegex()
26+
val regex3 = """don't.*$""".toRegex()
27+
28+
total = 0;
29+
lines.map{ line:String ->
30+
println(line)
31+
var replaced = line.replace(regex2, "")
32+
replaced = replaced.replace(regex3, "")
33+
println(replaced)
34+
println("------")
35+
var matchResult = regex.find(replaced)
36+
37+
if (matchResult != null) {
38+
while(matchResult?.groups?.get("first") != null) {
39+
val first = matchResult!!.groups["first"]?.value?.toInt()
40+
val second = matchResult!!.groups["second"]?.value?.toInt()
41+
total += first!! * second!!
42+
43+
matchResult = matchResult!!.next()
44+
}
45+
}
46+
}
47+
48+
println(total)
49+
}

0 commit comments

Comments
 (0)