adamratzman
12/04/2020, 4:58 AMbjonnh
12/04/2020, 4:59 AMadamratzman
12/04/2020, 5:24 AMprivate val input = readInput("input4.txt")
fun main() {
val all = listOf("byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid")
input.split("\n\n").filter { passport ->
val found =
passport.split(" ", "\n").filter { it.isNotBlank() }.map { it.split(":")[0] to it.split(":")[1] }.toMap()
found.keys.containsAll(all)
}.size.let { println("Part 1: $it") }
input.split("\n\n").filter { passport ->
val found =
passport.split(" ", "\n").filter { it.isNotBlank() }.map { it.split(":")[0] to it.split(":")[1] }.toMap()
if (!found.keys.containsAll(all)) false
else {
found["byr"]?.toIntOrNull()?.let { it in 1920..2002 } == true &&
found["iyr"]?.toIntOrNull()?.let { it in 2010..2020 } == true &&
found["eyr"]?.toIntOrNull()?.let { it in 2020..2030 } == true &&
found["pid"]?.toIntOrNull()?.let { found["pid"]!!.length == 9 } == true &&
found["ecl"] in "amb blu brn gry grn hzl oth".split(" ") &&
found["hcl"]!![0] == '#'
&& found["hcl"]!!.substring(found["hcl"]!!.length - 6).all { it in '0'..'9' || it in 'a'..'f' }
&& if (found["hgt"]!!.endsWith("cm") || found["hgt"]!!.endsWith("in")) {
if (found["hgt"]!!.endsWith("cm")) found["hgt"]!!.removeSuffix("cm").toInt() in 150..193
else found["hgt"]!!.removeSuffix("in").toInt() in 59..76
} else false
}
}.size.let { println("Part 2: $it") }
}
bjonnh
12/04/2020, 5:35 AMadamratzman
12/04/2020, 5:39 AMbjonnh
12/04/2020, 5:39 AMadamratzman
12/04/2020, 5:40 AMbjonnh
12/04/2020, 5:40 AMbjonnh
12/04/2020, 5:40 AMbjonnh
12/04/2020, 5:40 AMbjonnh
12/04/2020, 5:40 AMadamratzman
12/04/2020, 5:42 AMbjonnh
12/04/2020, 5:44 AMbjonnh
12/04/2020, 5:45 AMbjonnh
12/04/2020, 5:45 AMbjonnh
12/04/2020, 5:45 AMadamratzman
12/04/2020, 5:46 AMbjonnh
12/04/2020, 5:46 AMbjonnh
12/04/2020, 5:46 AMbjonnh
12/04/2020, 6:05 AMbjonnh
12/04/2020, 6:26 AMbjonnh
12/04/2020, 6:26 AMJoris PZ
12/04/2020, 8:43 AMval validators = mapOf<String, (String?) -> Boolean>(
"cid" to { true },
"byr" to { it?.toInt() in 1920..2002 },
"iyr" to { it?.toInt() in 2010..2020 },
"eyr" to { it?.toInt() in 2020..2030 },
"hcl" to { it?.matches(Regex("""^#[0-9a-f]{6}$""")) ?: false },
"ecl" to { it?.matches(Regex("""^amb|blu|brn|gry|grn|hzl|oth$""")) ?: false },
"pid" to { it?.matches(Regex("""^[0-9]{9}$""")) ?: false },
"hgt" to {
it?.let {
Regex("""(\d*)(cm|in)""").matchEntire(it)?.let { match ->
val (value, unit) = match.destructured
when (unit) {
"cm" -> value.toInt() in 150..193
"in" -> value.toInt() in 59..76
else -> false
}
}
} ?: false
}
)
Timings per platform:
| Platform | Average (ms) | Measurements (ms) |
| -----------------| ----------------:|------------------:|
| JVM (OpenJDK 11) | 11.3±14,5 | `70, 21, 16, 17, 10, 10, 8, 11, 6, 6, 5, 5, 4, 4, 6, 5, 3, 4, 3, 2` |
| Node JS | 14.8±13.5 | `65, 26, 35, 17, 8, 8, 9, 7, 7, 8, 8, 8, 12, 8, 8, 12, 10, 12, 10, 8` |
| Native | 56.2±6.0 | `53, 69, 51, 51, 47, 60, 59, 47, 56, 49, 52, 56, 53, 57, 62, 65, 56, 48, 64, 57` |
Nmrsmn
12/04/2020, 10:25 AMJoris PZ
12/04/2020, 11:01 AMNir
12/04/2020, 3:41 PMpackage day4
import utils.*
fun String.popSuffix(suffix: String) = if (endsWith(suffix)) removeSuffix(suffix) else null
val fields = listOf<Pair<String, (String) -> Boolean>>(
"byr" to { it.toInt() in 1920..2002 },
"iyr" to { it.toInt() in 2010..2020 },
"eyr" to { it.toInt() in 2020..2030 },
"hgt" to {
it.popSuffix("cm")?.let { it.toInt() in 150..193 } ?: it.popSuffix("in")?.let { it.toInt() in 59..76 } ?: false
},
"hcl" to { it[0] == '#' && it.substring(1).all { c -> c.isDigit() || c in 'a'..'f' } },
"ecl" to { it in listOf("amb", "blu", "brn", "gry", "grn", "hzl", "oth") },
"pid" to { it.length == 9 && it.all { c -> c.isDigit() } })
fun getInput() = (aocDataDir / "day4.txt").readText()
.split("\n\n")
.map { passportString ->
passportString
.split(" ", "\n")
.filter { !it.isEmpty() }
.associate { it.split(":").let { it[0] to it[1] } }
}
fun Map<String, String>.passportValid1() = fields.all { it.first in this }
fun part1() = getInput().count { p -> fields.all { it.first in p } }.also { println(it) }
fun part2() = getInput()
.count { p -> fields.all { f -> p[f.first]?.let { f.second(it) } ?: false } }
.also { println(it) }
Nir
12/04/2020, 3:42 PMUnAn
12/04/2020, 4:58 PMJoris PZ
12/04/2020, 5:00 PMUnAn
12/04/2020, 5:05 PMUnAn
12/04/2020, 5:05 PMimport java.io.File
import java.io.InputStream
fun main(args: Array<String>) {
val inputStream: InputStream = File("C:\\Users\\jayso\\AppData\\Roaming\\JetBrains\\IdeaIC2020.3\\scratches\\scratch.txt").inputStream()
val lista = mutableListOf<String>()
inputStream.bufferedReader().forEachLine { lista.add(it) }
var count=0
var countInt=0
var validI= 0
for (riga in lista) {
if(riga.length==0) {
if(countInt>=7&&validI==7){
count++
}
countInt=0
validI=0
}else{
val temp = riga.split(" ")
for (item in temp) {
var listaInt= item.split(":")
if(!listaInt[0].equals("cid")){
countInt++
when(listaInt[0]){
"ecl"-> if(listaInt[1].matches(Regex("amb|blu|brn|gry|grn|hzl|oth"))) validI++
"byr"-> if(listaInt[1].toInt().let{it in 1920..2002 }) validI++
"iyr"-> if(listaInt[1].toInt().let{it in 2010..2020 }) validI++
"eyr"-> if(listaInt[1].toInt().let{it in 2020..2030 }) validI++
"hgt"-> if(listaInt[1].contains("in")){
if(listaInt[1].filter { it.isDigit() }.toInt().let { it in 59..76 }) validI++
}else{
if(listaInt[1].filter { it.isDigit() }.toInt().let { it in 150..193 }) validI++
}
"hcl"-> if(listaInt[1].split("#")[0].all { it in '0'..'9' || it in 'a'..'f'}) validI++
"pid"-> if(listaInt[1].length==9) validI++
}
}
}
}
}
println(count)
}
Should probably improve my variables namingadamratzman
12/04/2020, 6:54 PMfun readInput(filename: String): String {
return File(ClassLoader.getSystemResource(filename).file).readText()
}
Use filter/map/count
Do not use .equals - kotlin will do string equality normally
toInt/toX can throw exceptions if there’s a bad format, instead you might want to use toIntOrNull in case you’re trying to parse an invalid integer (then ?.let {} ? :false)UnAn
12/04/2020, 10:30 PMadamratzman
12/05/2020, 1:06 AMbjonnh
12/05/2020, 2:20 AMbjonnh
12/05/2020, 2:20 AMadamratzman
12/05/2020, 2:47 AMadamratzman
12/05/2020, 2:47 AMbjonnh
12/05/2020, 2:48 AMbjonnh
12/05/2020, 2:48 AMbjonnh
12/05/2020, 2:48 AMJoris PZ
12/05/2020, 8:27 AM