Skip to content

Commit 3e80f7e

Browse files
authored
Merge pull request #5 from palinkiewicz/2023_d04
2023 Day 04
2 parents 9cb26d2 + e6b7a87 commit 3e80f7e

File tree

3 files changed

+262
-4
lines changed

3 files changed

+262
-4
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ In this repository, I am going to store my solutions to the [Advent of Code](htt
55
I started taking part in the event in 2023 but may also consider doing tasks from the previous years.
66

77
## 2023
8-
| | My solution | My inputs |
9-
| --- | --- | --- |
10-
| [Day 1: Trebuchet?!](https://adventofcode.com/2023/day/1) | [Solution](/adventofcode2023/Day01.kt) | [Inputs](/resources/adventofcode2023/Day01.txt) |
8+
| | My solution | My inputs |
9+
|--------------------------------------------------------------|----------------------------------------|-------------------------------------------------|
10+
| [Day 1: Trebuchet?!](https://adventofcode.com/2023/day/1) | [Solution](/adventofcode2023/Day01.kt) | [Inputs](/resources/adventofcode2023/Day01.txt) |
1111
| [Day 2: Cube Conundrum](https://adventofcode.com/2023/day/2) | [Solution](/adventofcode2023/Day02.kt) | [Inputs](/resources/adventofcode2023/Day02.txt) |
12-
| [Day 3: Gear Ratios](https://adventofcode.com/2023/day/3) | [Solution](/adventofcode2023/Day03.kt) | [Inputs](/resources/adventofcode2023/Day03.txt) |
12+
| [Day 3: Gear Ratios](https://adventofcode.com/2023/day/3) | [Solution](/adventofcode2023/Day03.kt) | [Inputs](/resources/adventofcode2023/Day03.txt) |
13+
| [Day 4: Scratchcards](https://adventofcode.com/2023/day/4) | [Solution](/adventofcode2023/Day04.kt) | [Inputs](/resources/adventofcode2023/Day04.txt) |

adventofcode2023/Day04.kt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package adventofcode2023
2+
3+
import java.io.File
4+
import kotlin.math.pow
5+
6+
object Day04 {
7+
private fun countCorrectNumbersOnCard(line: String): Int {
8+
val numbers = line.split(": ")[1].split(" | ")
9+
val winningNumbers = numbers[0].split(" ").filter { it != "" }
10+
val actualNumbers = numbers[1].split(" ").filter { it != "" }
11+
return actualNumbers.count { winningNumbers.contains(it) }
12+
}
13+
14+
fun part1(inputs: List<String>) {
15+
println(inputs.sumOf { line ->
16+
val correctNumbersCount = countCorrectNumbersOnCard(line)
17+
if (correctNumbersCount == 0) 0 else 2.0.pow(correctNumbersCount - 1).toInt()
18+
})
19+
}
20+
21+
fun part2(inputs: List<String>) {
22+
val scratchcardsCounters = IntArray(inputs.size) { 1 }
23+
24+
for (i in inputs.indices) {
25+
val correctNumbersCount = countCorrectNumbersOnCard(inputs[i])
26+
for (j in i + 1..i + correctNumbersCount) {
27+
scratchcardsCounters[j] += scratchcardsCounters[i]
28+
}
29+
}
30+
31+
println(scratchcardsCounters.sum())
32+
}
33+
}
34+
35+
fun main() {
36+
val inputs = File("resources/adventofcode2023/Day04.txt").readLines()
37+
Day04.part1(inputs)
38+
Day04.part2(inputs)
39+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy