Skip to content

Commit bb69d24

Browse files
committed
2023 day1 kotlin
1 parent 442dcbf commit bb69d24

File tree

6 files changed

+1182
-1
lines changed

6 files changed

+1182
-1
lines changed

doc/v2023/day1.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Day 1: Trebuchet?!
2+
3+
## Part 1
4+
5+
Something is wrong with global snow production, and you've been selected to take a look. The Elves have even given you a map; on it, they've used stars to mark the top fifty locations that are likely to be having problems.
6+
7+
You've been doing this long enough to know that to restore snow operations, you need to check all fifty stars by December 25th.
8+
9+
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
10+
11+
You try to ask why they can't just use a weather machine ("not powerful enough") and where they're even sending you ("the sky") and why your map looks mostly blank ("you sure ask a lot of questions") and hang on did you just say the sky ("of course, where do you think snow comes from") when you realize that the Elves are already loading you into a trebuchet ("please hold still, we need to strap you in").
12+
13+
As they're making the final adjustments, they discover that their calibration document (your puzzle input) has been amended by a very young Elf who was apparently just excited to show off her art skills. Consequently, the Elves are having trouble reading the values on the document.
14+
15+
The newly-improved calibration document consists of lines of text; each line originally contained a specific calibration value that the Elves now need to recover. On each line, the calibration value can be found by combining the first digit and the last digit (in that order) to form a single two-digit number.
16+
17+
For example:
18+
19+
```
20+
1abc2
21+
pqr3stu8vwx
22+
a1b2c3d4e5f
23+
treb7uchet
24+
```
25+
26+
In this example, the calibration values of these four lines are 12, 38, 15, and 77. Adding these together produces 142.
27+
28+
Consider your entire calibration document. What is the sum of all of the calibration values?
29+
30+
## Part 2
31+
32+
Your calculation isn't quite right. It looks like some of the digits are actually spelled out with letters: one, two, three, four, five, six, seven, eight, and nine also count as valid "digits".
33+
34+
Equipped with this new information, you now need to find the real first and last digit on each line. For example:
35+
36+
```
37+
two1nine
38+
eightwothree
39+
abcone2threexyz
40+
xtwone3four
41+
4nineeightseven2
42+
zoneight234
43+
7pqrstsixteen
44+
```
45+
46+
In this example, the calibration values are 29, 83, 13, 24, 42, 14, and 76. Adding these together produces 281.
47+
48+
What is the sum of all of the calibration values?

pom.xml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<properties>
1212
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1313
<java.version>17</java.version>
14+
<kotlin.version>1.9.21</kotlin.version>
1415
<easy-rules.version>4.1.0</easy-rules.version>
1516
<lombok.version>1.18.24</lombok.version>
1617
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
@@ -48,7 +49,11 @@
4849
<artifactId>commons-collections4</artifactId>
4950
<version>4.4</version>
5051
</dependency>
51-
52+
<dependency>
53+
<groupId>org.jetbrains.kotlin</groupId>
54+
<artifactId>kotlin-stdlib</artifactId>
55+
<version>${kotlin.version}</version>
56+
</dependency>
5257
</dependencies>
5358

5459
<build>
@@ -63,6 +68,24 @@
6368
<showWarnings>true</showWarnings>
6469
</configuration>
6570
</plugin>
71+
<plugin>
72+
<groupId>org.jetbrains.kotlin</groupId>
73+
<artifactId>kotlin-maven-plugin</artifactId>
74+
<version>${kotlin.version}</version>
75+
<executions>
76+
<execution>
77+
<id>compile</id>
78+
<goals>
79+
<goal>compile</goal>
80+
</goals>
81+
<configuration>
82+
<sourceDirs>
83+
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
84+
</sourceDirs>
85+
</configuration>
86+
</execution>
87+
</executions>
88+
</plugin>
6689
</plugins>
6790
</build>
6891
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package adventofcode
2+
3+
import java.math.BigInteger
4+
import java.security.MessageDigest
5+
import kotlin.io.path.Path
6+
import kotlin.io.path.readLines
7+
8+
fun readInput(name: String) = Path("src/main/resources/$name/input.txt").readLines()
9+
10+
/**
11+
* Converts string to md5 hash.
12+
*/
13+
fun String.md5() = BigInteger(1, MessageDigest.getInstance("MD5").digest(toByteArray()))
14+
.toString(16)
15+
.padStart(32, '0')
16+
17+
/**
18+
* The cleaner shorthand for printing output.
19+
*/
20+
fun Any?.println() = println(this)
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package adventofcode.v2023
2+
3+
import adventofcode.println
4+
import adventofcode.readInput
5+
6+
7+
8+
fun main() {
9+
10+
fun String.getFirstAndLast() = first() to last()
11+
12+
fun part1(input: List<String>): Int {
13+
return input.asSequence()
14+
.map { s -> s.filter { it.isDigit() } }
15+
.map { s -> s.getFirstAndLast() }
16+
.map { (f, l) -> "$f$l".toInt() }
17+
.sum();
18+
}
19+
20+
fun replaceWordsWithNumbers(input: String): String {
21+
val wordMap = mapOf(
22+
"one" to "1",
23+
"two" to "2",
24+
"three" to "3",
25+
"four" to "4",
26+
"five" to "5",
27+
"six" to "6",
28+
"seven" to "7",
29+
"eight" to "8",
30+
"nine" to "9"
31+
)
32+
33+
val result = StringBuilder()
34+
val currentWord = StringBuilder()
35+
val regex = Regex(wordMap.keys.joinToString("|").let { "($it)" })
36+
37+
for (char in input) {
38+
if (char.isLetter()) {
39+
currentWord.append(char)
40+
val matches = regex.find(currentWord.toString())
41+
if (matches != null) {
42+
result.append(wordMap[matches.value])
43+
break
44+
}
45+
} else {
46+
result.append(char)
47+
currentWord.clear()
48+
break
49+
}
50+
}
51+
52+
for (char in input.reversed()) {
53+
if (char.isLetter()) {
54+
currentWord.append(char)
55+
val matches = regex.find(currentWord.toString().reversed())
56+
if (matches != null) {
57+
result.append(wordMap[matches.value])
58+
break
59+
}
60+
} else {
61+
result.append(char)
62+
currentWord.clear()
63+
break
64+
}
65+
}
66+
67+
return result.toString()
68+
}
69+
70+
fun part2(input: List<String>): Int {
71+
return input.asSequence()
72+
.map { s -> replaceWordsWithNumbers(s) }
73+
.map { s -> s.filter { it.isDigit() } }
74+
.map { s -> s.getFirstAndLast() }
75+
.map { (f, l) -> "$f$l".toInt() }
76+
.sum();
77+
}
78+
79+
val input = readInput("v2023/d1")
80+
81+
part1(input).println()
82+
part2(input).println()
83+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
two1nine
2+
eightwothree
3+
abcone2threexyz
4+
xtwone3four
5+
4nineeightseven2
6+
zoneight234
7+
7pqrstsixteen

0 commit comments

Comments
 (0)