Advertisement
hrabrica

AoC | Day 6 | Part 1

Dec 5th, 2023
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.02 KB | Source Code | 0 0
  1. package year_2023.day_6
  2.  
  3. fun partOne() {
  4.     println("[2023] Day six, part one...")
  5.  
  6.     val times = input().split("\n")[0]
  7.         .substringAfter(":")
  8.         .split(" ")
  9.         .filter { it.isNotBlank() }
  10.         .map { it.trim().toInt() }
  11.  
  12.     val distances = input().split("\n")[1]
  13.         .substringAfter(":")
  14.         .split(" ")
  15.         .filter { it.isNotBlank() }
  16.         .map { it.trim().toInt() }
  17.  
  18.     val waysToBeatPerRound = mutableListOf<Int>()
  19.  
  20.     repeat(times.size) {
  21.         val time = times[it]
  22.         val distance = distances[it]
  23.         var validCounter = 0
  24.  
  25.         (1..time).forEach { timeHolding ->
  26.             val speed = timeHolding
  27.             val remainingTime = time - timeHolding
  28.             val totalDistance = speed * remainingTime
  29.  
  30.             if (totalDistance > distance) {
  31.                 validCounter++
  32.             }
  33.         }
  34.  
  35.         waysToBeatPerRound.add(validCounter)
  36.     }
  37.  
  38.     val result = waysToBeatPerRound.reduce { acc, i -> acc * i }
  39.     println(result)
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement