1
0

Fix a bug where 'hgt' was not followed by cm|in

This commit is contained in:
2020-12-04 10:12:39 +01:00
parent 9fa97a1d25
commit 0454187323
2 changed files with 49 additions and 7 deletions
+8 -7
View File
@@ -24,15 +24,16 @@ class Day04(@Text val input: Input<String>) {
fun part1(): Int = entries.count { it.hasRequiredKeys() }
private fun Entry.hasValidValues(): Boolean {
val (k, v) = this
return when (k) {
private fun Entry.hasValidValues() = let { (k, v) ->
when (k) {
"byr" -> v.toInt() in 1920..2002
"iyr" -> v.toInt() in 2010..2020
"eyr" -> v.toInt() in 2020..2030
"hgt" ->
if (v.endsWith("cm")) v.removeSuffix("cm").toInt() in 150..193
else v.removeSuffix("in").toInt() in 59..76
"hgt" -> when {
v.endsWith("cm") -> v.removeSuffix("cm").toInt() in 150..193
v.endsWith("in") -> v.removeSuffix("in").toInt() in 59..76
else -> false
}
"hcl" -> v.matches("^#[0-9a-f]{6}$".toRegex())
"ecl" -> v in arrayOf("amb", "blu", "brn", "gry", "grn", "hzl", "oth")
"pid" -> v.matches("^[0-9]{9}$".toRegex())
@@ -44,7 +45,7 @@ class Day04(@Text val input: Input<String>) {
.asSequence()
.filter { it.hasRequiredKeys() }
.filter { it.all { it.hasValidValues() } }
.count() - 1 // ???
.count()
}
fun main() = with(createDay<Day04>()) {