-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day04.kt
74 lines (63 loc) · 1.9 KB
/
Day04.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.io.File
import java.util.*
// This list will keep track of the number of scratchcards at each step
var cardCounts = mutableListOf<Int>()
fun getFile(filename: String): MutableList<String> {
val inputs = mutableListOf<String>()
val testFile = File("$filename")
println("Reading file: ${testFile.absolutePath}")
val scannerLines = Scanner(testFile)
while (scannerLines.hasNextLine()) {
inputs.add(scannerLines.nextLine())
}
scannerLines.close()
return inputs
}
fun part1(data: List<String>): Int {
var cardScore = 0
var winNum = 0
var theSum = 0
val cardCounts = MutableList(data.size) { 1 }
for ((x, row) in data.withIndex()) {
val wins = row.split("|")[0].split(":")[1].trim().split(" ")
val nums = row.split("|")[1].trim().split(" ")
for (num in nums) {
if (num in wins) {
winNum++
cardScore = if (cardScore == 0) 1 else cardScore * 2
}
}
for (i in 0 until winNum) {
if (x + i + 1 < cardCounts.size) {
cardCounts[x + i + 1] += cardCounts[x]
}
}
theSum += cardScore
cardScore = 0
winNum = 0
}
println("Card counts: $cardCounts")
return theSum
}
fun part2(data: List<String>): Int {
var cardScore = 0
return cardScore
}
fun main() {
// Test case
// val data = mutableListOf<String>()
val testData = getFile("../testdata1.txt")
var testPoints = part1(testData)
if (testPoints == 13) {
println("Test passed!")
} else {
println("Test failed!")
println("Expected: 13, Actual: $testPoints")
System.exit(1)
}
val data = getFile("../input.txt")
val totalPoints = part1(data)
println("Total points: $totalPoints")
val totalScratchCards = part2(data)
println("Total points: $totalScratchCards")
}