Skip to content

Commit 64775b4

Browse files
Will Molloywill-molloy
authored andcommitted
Refactor Day 1 - reusable function for part 1/2
GitOrigin-RevId: 0995aa825e3a0399b9d23bf4131303f7c2669368
1 parent 6d39000 commit 64775b4

File tree

2 files changed

+23
-51
lines changed
  • advent-of-code/src/main/kotlin/com/willmolloy/adventofcode/_2025

2 files changed

+23
-51
lines changed

advent-of-code/src/main/kotlin/com/willmolloy/adventofcode/_2025/Day1.kt

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,17 @@ import com.willmolloy.adventofcode.common.Input
66
/** https://adventofcode.com/2025/day/1 */
77
object Day1 : Day(2025, 1) {
88

9-
override fun part1(input: Input): Any {
10-
val lines = input.lines()
11-
12-
var res = 0
13-
var dial = 50
14-
15-
for (line in lines) {
16-
var nums = line.substring(1).toInt()
17-
18-
when (line[0]) {
19-
'L' -> {
20-
// dec
21-
while (nums-- > 0) {
22-
dial--
23-
// TODO there must be a better way - modulo? But then part2 is broken...?
24-
if (dial == -1) {
25-
dial = 99
26-
}
27-
}
28-
}
29-
'R' -> {
30-
// inc
31-
while (nums-- > 0) {
32-
dial++
33-
if (dial == 100) {
34-
dial = 0
35-
}
36-
}
37-
}
38-
}
39-
40-
if (dial == 0) {
41-
res++
42-
}
43-
}
9+
override fun part1(input: Input) = solve(input).first
4410

45-
return res
46-
}
11+
override fun part2(input: Input) = solve(input).second
4712

48-
// just move the res count into the inner loops...
49-
override fun part2(input: Input): Any {
13+
private fun solve(input: Input): Pair<Int, Int> {
5014
val lines = input.lines()
5115

52-
var res = 0
16+
var part1Res = 0
17+
// just move the res count into the inner loops...
18+
var part2Res = 0
19+
5320
var dial = 50
5421

5522
for (line in lines) {
@@ -60,11 +27,13 @@ object Day1 : Day(2025, 1) {
6027
// dec
6128
while (nums-- > 0) {
6229
dial--
30+
// TODO there must be a better way - modulo? But then part2 is broken...?
6331
if (dial == -1) {
6432
dial = 99
6533
}
34+
6635
if (dial == 0) {
67-
res++
36+
part2Res++
6837
}
6938
}
7039
}
@@ -75,14 +44,19 @@ object Day1 : Day(2025, 1) {
7544
if (dial == 100) {
7645
dial = 0
7746
}
47+
7848
if (dial == 0) {
79-
res++
49+
part2Res++
8050
}
8151
}
8252
}
8353
}
54+
55+
if (dial == 0) {
56+
part1Res++
57+
}
8458
}
8559

86-
return res
60+
return Pair(part1Res, part2Res)
8761
}
8862
}

advent-of-code/src/main/kotlin/com/willmolloy/adventofcode/_2025/Day2.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,16 @@ import com.willmolloy.adventofcode.common.Input
66
/** https://adventofcode.com/2025/day/2 */
77
object Day2 : Day(2025, 2) {
88

9-
override fun part1(input: Input): Any {
10-
return solve(input) { it.take(it.length / 2) == it.drop(it.length / 2) }
11-
}
9+
override fun part1(input: Input) =
10+
solve(input) { id -> id.take(id.length / 2) == id.drop(id.length / 2) }
1211

13-
override fun part2(input: Input): Any {
14-
return solve(input) {
15-
((1..it.length / 2).any { chunkSize ->
16-
val chunked = it.chunked(chunkSize)
12+
override fun part2(input: Input) =
13+
solve(input) { id ->
14+
((1..id.length / 2).any { chunkSize ->
15+
val chunked = id.chunked(chunkSize)
1716
chunked.all { s -> s == chunked.first() }
1817
})
1918
}
20-
}
2119

2220
private fun solve(input: Input, test: (String) -> Boolean): Long {
2321
val ranges = input.string().split(",")

0 commit comments

Comments
 (0)