adamratzman
12/05/2020, 4:26 AMKroppeb
12/05/2020, 5:12 AMDavid Whittaker
12/05/2020, 5:16 AMDavid Whittaker
12/05/2020, 5:21 AM.sort()
here - made the second part super easy!adamratzman
12/05/2020, 5:22 AMadamratzman
12/05/2020, 5:23 AMprivate val input = readInput("input5.txt")
fun main() {
val numbers = input.split("\n").map { line ->
var lowRange = 0
var highRange = 127
(0..6).forEach { if (line[it] == 'F') highRange -= (highRange - lowRange + 1) /2 else lowRange += (highRange - lowRange + 1) / 2 }
val rowNum = highRange
lowRange = 0
highRange = 7
(0..2).forEach { if (line.takeLast(3)[it] == 'L') highRange -= (highRange - lowRange + 1) /2 else lowRange += (highRange - lowRange + 1) / 2 }
val col = highRange
rowNum * 8 + col to rowNum
}
numbers.map { it.first }.maxOrNull()!!.let { println("Part 1: $it") }
val filtered = numbers.filter { it.second != 0 && it.second != 127 }.map { it.first }
filtered.forEach { one ->
filtered.forEach { two ->
if (one - two == 2) {
if ((one - 1) !in filtered) println("Part 2: ${one - 1}")
}
}
}
}
Kroppeb
12/05/2020, 5:23 AMKroppeb
12/05/2020, 5:24 AMadamratzman
12/05/2020, 5:24 AMadamratzman
12/05/2020, 5:24 AMDavid Whittaker
12/05/2020, 5:25 AMwhen
again 🙂Kroppeb
12/05/2020, 5:25 AMit.map { if (it == 'B' || it == 'R') '1' else '0' }.joinToString("").toInt(2)
Kroppeb
12/05/2020, 5:25 AMDavid Whittaker
12/05/2020, 5:25 AMKroppeb
12/05/2020, 5:25 AMKroppeb
12/05/2020, 5:25 AMval x = it.substring(0, 7).mapIndexed { a, b -> if (b == 'B') 2.0.pow(6 - a) else 0.0 }.sumBy { it.toInt() }
Kroppeb
12/05/2020, 5:26 AMx * 8 + y
David Whittaker
12/05/2020, 5:26 AMKroppeb
12/05/2020, 5:26 AMadamratzman
12/05/2020, 5:26 AMadamratzman
12/05/2020, 5:27 AMwhen
?David Whittaker
12/05/2020, 5:32 AMDavid Whittaker
12/05/2020, 5:36 AMline.forEach {
when (it) {
'F' -> rowH = (rowL + rowH )/2
'B' -> rowL = (rowL + rowH + 1)/2
'L' -> colH = (colL + colH)/2
'R' -> colL = (colL + colH +1)/2
}
}
David Whittaker
12/05/2020, 5:39 AMvar (rowL, rowH, colL, colH) = listOf(0, 127, 0, 8)
adamratzman
12/05/2020, 5:40 AMprivate val input = readInput("input5.txt")
fun main() {
val numbers = input.split("\n").map { line ->
val row = line.take(7).map { if (it == 'F') 0 else 1 }.joinToString("").toInt(2)
val col = line.takeLast(3).map { if (it == 'L') 0 else 1 }.joinToString("").toInt(2)
row * 8 + col to row
}
numbers.map { it.first }.maxOrNull()!!.let { println("Part 1: $it") }
val filtered = numbers.filter { it.second != 0 && it.second != 127 }.map { it.first }
filtered.forEach { one ->
filtered.forEach { two ->
if (one - two == 2 && (one - 1) !in filtered) println("Part 2: ${one - 1}")
}
}
}
adamratzman
12/05/2020, 5:41 AMJoris PZ
12/05/2020, 8:26 AMval p05 = suspend {
val seating = SeatNode()
input.lines().forEach {
it.fold(seating) { node, ch -> node.insert(ch) }
}
seating.seats.maxOrNull().print { "Part 1: $it" }
(0..1023).toList()
.filter { it !in seating }
.single { (it - 1) in seating && (it + 1) in seating }
.print { "Part 2: $it" }
}
And the timings:
| Platform | Average (ms) | Measurements (ms) |
| -----------------| -------------:|------------------:|
| JVM (OpenJDK 11) | 3.9±6.0 | `29, 5, 2, 2, 2, 2, 4, 3, 2, 2, 2, 1, 1, 1, 2, 1, 1, 1, 5, 1` |
| Node JS | 8.8±8.1 | `35, 24, 13, 17, 8, 9, 4, 4, 4, 5, 3, 5, 4, 5, 5, 3, 4, 3, 4, 5` |
| Native | 18.9±2.6 | `18, 18, 16, 19, 18, 18, 27, 18, 18, 16, 18, 16, 19, 17, 22, 16, 18, 17, 22, 17` |
andyb
12/05/2020, 9:32 AMfun String.toSeatId(): Int = this.fold(0){acc,c -> 2 * acc + if ( c=='B'||c=='R') 1 else 0}
fun main() {
val boardingPasses = resourceFile("day05/BoardingPass.txt").readLines()
val seatIds = boardingPasses.map { it.toSeatId() }
println(seatIds.maxOrNull())
println((seatIds.minOrNull()!!..seatIds.maxOrNull()!!).dropWhile { it in seatIds }.first())
}
ephemient
12/05/2020, 11:22 AMephemient
12/05/2020, 11:23 AMF|B|L|R
constants in their code anyway ;)mickeelm
12/05/2020, 11:29 AM0..1023
is 0..12
, 727
(i.e. my answer) and 979..1023
. I would've expected gaps at both ends as well. According to a colleague he has those front and end gaps so I was wondering if I just got a very generous puzzle input out of luck... EDIT: I may have misunderstood him.mickeelm
12/05/2020, 11:30 AMNir
12/05/2020, 3:37 PMfun String.asBinary(one: Char) = reversed()
.withIndex()
.map { 2.0.pow(it.index) * if(it.value == one) 1 else 0 }
.sum().toInt()
fun toId(row: Int, col: Int) = row * 8 + col
fun getInput() = (aocDataDir / "day5.txt").useLines { lines ->
lines.map { line ->
toId(line.substring(0,7).asBinary('B'), line.substring(7).asBinary('R'))
}.toList()
}
fun part1() = getInput().max().also { println(it) }
fun part2(): Int {
val ids = getInput().toSet()
return (0..toId(127, 7)).asSequence().filter {
it !in ids && (it+1) in ids && (it-1) in ids
}.single().also { println(it) }
}
todd.ginsberg
12/05/2020, 3:45 PMclass Day05(private val input: List<String>) {
fun solvePart1(): Int =
input.map { seatId(it) }.maxOrNull() ?: throw IllegalStateException("No answer")
fun solvePart2(): Int =
input.map { seatId(it) }.sorted().run {
(((first()+last())/2.0) * (size+1)).toInt() - sum()
}
private fun seatId(pattern: String): Int =
pattern.map { if(it in setOf('B', 'R')) '1' else '0' }.joinToString("").toInt(2)
}
I want to clean up Part 2. But essentially I'm figuring out what the total should be and then figuring out what it actually is, and the missing one is my seat. That took me a while to get right, but I knew what the answer was because I did a manual loop over the sorted list to find the missing one to get the star and the right answer before I rewrote that bit.todd.ginsberg
12/05/2020, 3:51 PMtodd.ginsberg
12/05/2020, 4:02 PMsum()
) but it looks cleaner (to me). I might change it though.todd.ginsberg
12/05/2020, 4:28 PMfun solvePart2(): Int =
input.map { seatId(it) }.sorted().run {
(first()..last()).zip(this).first { it.first != it.second }.first
}
Edgars
12/05/2020, 4:30 PMzipWithNext
on a sorted list and find the first pair that doesn't fit. Might be a bit more easier to explain than zipping with the range, but idk. Link.todd.ginsberg
12/05/2020, 4:42 PMtodd.ginsberg
12/05/2020, 4:46 PMEdgars
12/05/2020, 4:47 PMtodd.ginsberg
12/05/2020, 4:57 PMmickeelm
12/05/2020, 4:57 PMtodd.ginsberg
12/05/2020, 4:58 PMmickeelm
12/05/2020, 4:58 PMEdgars
12/05/2020, 5:03 PMmickeelm
12/05/2020, 5:04 PMtodd.ginsberg
12/05/2020, 5:07 PMbjonnh
12/05/2020, 5:49 PMimport helpers.linesFile
import helpers.rFindGroupsDestructured
fun main() {
val seats = linesFile("data/2020/05_input.txt").map { line ->
val (valRaw) = line.rFindGroupsDestructured("([FB]{7}[LR]{3})")
val value = valRaw.map { if (it in setOf('B', 'R')) 1 else 0 }.joinToString("").toInt(2)
value.shr(3) * 8 + (value.and(7))
}
println("Part 1: ${seats.maxOf { it }}")
val ids = seats.map { it }.toSet()
println("Part 2: ${((ids.minOrNull() ?: 0)..(ids.maxOrNull() ?: 1023)).toSet() - ids}")
}
bjonnh
12/05/2020, 5:49 PMbjonnh
12/05/2020, 5:51 PMNir
12/05/2020, 5:51 PMNir
12/05/2020, 5:52 PMbjonnh
12/05/2020, 5:52 PMfun main() {
val seats = linesFile("data/2020/05_input.txt").map { line ->
val value = line.map { if (it in setOf('B', 'R')) 1 else 0 }.joinToString("").toInt(2)
value.shr(3) * 8 + (value.and(7))
}
println("Part 1: ${seats.maxOf { it }}")
val ids = seats.sorted()
println("Part 2: ${((ids.first()..ids.last()).toSet() - ids.toSet()).first()}")
}
bjonnh
12/05/2020, 5:53 PMbjonnh
12/05/2020, 5:54 PMJakub Gwóźdź
12/05/2020, 8:13 PMadamratzman
12/05/2020, 8:21 PMJakub Gwóźdź
12/05/2020, 8:48 PMephemient
12/06/2020, 1:22 AMbjonnh
12/06/2020, 1:54 AMbjonnh
12/06/2020, 2:11 AMbjonnh
12/06/2020, 2:11 AMJakub Gwóźdź
12/06/2020, 4:51 AMadamratzman
12/06/2020, 4:51 AM