-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5070cdf
commit d731ffb
Showing
3 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |