Day 5 Part 1
This commit is contained in:
parent
6e18c4a72d
commit
3d02cb5d0b
3 changed files with 1436 additions and 0 deletions
38
2024/src/main/kotlin/day5.kt
Normal file
38
2024/src/main/kotlin/day5.kt
Normal file
|
@ -0,0 +1,38 @@
|
|||
fun main() {
|
||||
|
||||
val rules = mutableListOf<Pair<Int, Int>>()
|
||||
var completedRules = false
|
||||
|
||||
var part1 = 0
|
||||
|
||||
for (line in readInput("day5.txt")) {
|
||||
if (completedRules) {
|
||||
val job = line.split(",").map { it.toInt() }
|
||||
assert(job.count() % 2 != 0)
|
||||
val middlePage = job[job.count() / 2]
|
||||
|
||||
var matches = true
|
||||
for ((first, second) in rules) {
|
||||
val firstIndex = job.indexOf(first)
|
||||
val secondIndex = job.indexOf(second)
|
||||
|
||||
if (firstIndex != -1 && secondIndex != -1 && firstIndex > secondIndex) {
|
||||
matches = false
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if (matches) {
|
||||
part1 += middlePage
|
||||
}
|
||||
} else if (line.isEmpty()) {
|
||||
completedRules = true
|
||||
} else {
|
||||
val (first, second) = line.split("|", limit = 2).map { it.toInt() }
|
||||
rules.add(first to second)
|
||||
}
|
||||
}
|
||||
|
||||
println("Part 1: $part1")
|
||||
|
||||
}
|
28
2024/src/main/resources/day5-sample.txt
Normal file
28
2024/src/main/resources/day5-sample.txt
Normal file
|
@ -0,0 +1,28 @@
|
|||
47|53
|
||||
97|13
|
||||
97|61
|
||||
97|47
|
||||
75|29
|
||||
61|13
|
||||
75|53
|
||||
29|13
|
||||
97|29
|
||||
53|29
|
||||
61|53
|
||||
97|53
|
||||
61|29
|
||||
47|13
|
||||
75|47
|
||||
97|75
|
||||
47|61
|
||||
75|61
|
||||
47|29
|
||||
75|13
|
||||
53|13
|
||||
|
||||
75,47,61,53,29
|
||||
97,61,53,29,13
|
||||
75,29,13
|
||||
75,97,47,61,53
|
||||
61,13,29
|
||||
97,13,75,29,47
|
1370
2024/src/main/resources/day5.txt
Normal file
1370
2024/src/main/resources/day5.txt
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Reference in a new issue