Skip to content

Commit

Permalink
Kotlin solution for day 8
Browse files Browse the repository at this point in the history
  • Loading branch information
romain-gaillard committed Dec 8, 2021
1 parent d731ffb commit 53829b7
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 3 deletions.
6 changes: 3 additions & 3 deletions kotlin/day7.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ 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) {
for(i in population.indices) {
// Compute the fuel cost needed to move each crab to that position
var currentFuelConsumption = 0
for(j in 0 until population.size) {

for(j in population.indices) {
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
Expand Down
87 changes: 87 additions & 0 deletions kotlin/day8.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import java.io.File

fun countCharsInCommon(a: String, b: String): Int {
var counter = 0
for(c in a) {
if(c in b)
counter++
}

return counter
}

val fileName = if(args.size > 0) args[0] else "day8.txt"

var partOne = 0
var partTwo = 0

File(fileName).forEachLine {
var (inputs, outputs) = it.split("|").map { it.trim() }
.map { it.split(" ")}.map { it.map { it.trim() } }

// Count the number of signal patterns for 2, 3, 4, and 7 segements
// (corresponding respectively to digits 1, 7, 4, 8)
partOne += outputs.filter{ it.length == 2 || it.length == 3 || it.length == 4 || it.length == 7 }.size

// We sort the signal patterns alphabetically so that they can be compared easily to see if they match
inputs = inputs.map { it.toCharArray().sorted().joinToString("") }
outputs = outputs.map { it.toCharArray().sorted().joinToString("") }

// Building a list containing both inputs and outputs from which we will make the analysis on the
// signal patterns
val inputsAndOutputs = (inputs + outputs).distinct().toList()

// We'll store the signal patterns that correspond to each digit
var digitStr = Array(10 ) { "" }

// Note: for all the deductions below, we assume all the 10 digits are always present in an entry

// Case for 1 - contains 2 segments
digitStr[1] = inputsAndOutputs.filter { it.length == 2 }[0]

// Case for 4 - contains 7 segments
digitStr[4] = inputsAndOutputs.filter { it.length == 4 }[0]

// Case for 7 - contains 3 segments
digitStr[7] = inputsAndOutputs.filter { it.length == 3 }[0]

// Case for 8 - contains 7 segments
digitStr[8] = inputsAndOutputs.filter { it.length == 7 }[0]

// Case for 2, 3, 5 - contain 6 segments
for(currentDigit in inputsAndOutputs.filter { it.length == 6 }) {
if(countCharsInCommon(currentDigit, digitStr[7]) == 3) {
// It's a 0 or a 9
if(countCharsInCommon(currentDigit, digitStr[4]) == 4) {
digitStr[9] = currentDigit
}
else {
digitStr[0] = currentDigit
}
}
else {
digitStr[6] = currentDigit
}
}

// Case for 2, 3, 5 - contain 5 segments
for(currentDigit in inputsAndOutputs.filter { it.length == 5 }) {
if (countCharsInCommon(currentDigit, digitStr[4]) == 3) {
// It's a 3 or a 5
if (countCharsInCommon(currentDigit, digitStr[1]) == 2) {
digitStr[3] = currentDigit
} else {
digitStr[5] = currentDigit
}
} else {
digitStr[2] = currentDigit
}
}

// Map each character of the signal pattern to the correspondig digit, build a string containing the number
// And then, convert that to an Int
partTwo += outputs.map { digitStr.indexOf(it) }.joinToString("","", "").toInt(10)
}

println(partOne)
println(partTwo)

0 comments on commit 53829b7

Please sign in to comment.