Skip to content

Commit

Permalink
Kotlin solutions for days 6 and 7
Browse files Browse the repository at this point in the history
  • Loading branch information
romain-gaillard committed Dec 7, 2021
1 parent 5070cdf commit d731ffb
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
2 changes: 2 additions & 0 deletions kotlin/day4.kts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ for(i in 2 until lines.size) {
}
}

println(boards.size)

// Play
var foundWinning = false
for(number in drawnNumbers) {
Expand Down
36 changes: 36 additions & 0 deletions kotlin/day6.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import java.io.File

fun simulateDay(population: Map<Int, Long>): Map<Int, Long> {
var nextPopulation = mutableMapOf<Int, Long>()

for(i in 8 downTo 0) {
if(i > 0) {
nextPopulation.put(i - 1, population.getOrDefault(i, 0))
}
else {
nextPopulation.put(6, nextPopulation.getOrDefault(6, 0) + population.getOrDefault(0, 0))
nextPopulation.put(8, nextPopulation.getOrDefault(8, 0) + population.getOrDefault(0, 0))
}
}

return nextPopulation
}

fun simulateUntil(population: Map<Int, Long>, days: Int): Map<Int, Long> {
var nextPopulation = population.toMap()

for(i in 1..days) {
nextPopulation = simulateDay(nextPopulation)
}

return nextPopulation
}

val fileName = if(args.size > 0) args[0] else "day6.txt"
val lines = File(fileName).readLines()

// Convert the comma-separated list of lanternfish counters to a frequency map <Int, Long>
var population = lines[0].split(",").map { it.trim().toInt() }.groupingBy { it }.eachCount().mapValues { it.value.toLong() }

println(simulateUntil(population, 80).values.sum())
println(simulateUntil(population, 256).values.sum())
34 changes: 34 additions & 0 deletions kotlin/day7.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import java.io.File

val fileName = if(args.size > 0) args[0] else "day7.txt"
val lines = File(fileName).readLines()

// Convert the comma-separated list of crabs' horizontal positions to a List of Int
var population = lines[0].split(",").map { it.trim().toInt() }

fun computeFuelConsumption(population: List<Int>, secondPart: Boolean): Int {
var bestFuelConsumption = -1

// Try every horizontal position to see which one is the best
for(i in 0 until population.size) {
// Compute the fuel cost needed to move each crab to that position
var currentFuelConsumption = 0

for(j in 0 until population.size) {
val distance = Math.abs(population[j] - i) + 1
// The key to do the second part quickly was just to notice that we can use the formula
// n*(n-1)/2 to have the sum of the first n integers
val cost = if(!secondPart) distance else distance*(distance-1)/2

currentFuelConsumption += cost
}

if(bestFuelConsumption == -1 || currentFuelConsumption < bestFuelConsumption)
bestFuelConsumption = currentFuelConsumption
}

return bestFuelConsumption
}

println(computeFuelConsumption(population, false))
println(computeFuelConsumption(population, true))

0 comments on commit d731ffb

Please sign in to comment.