Created
January 8, 2021 12:04
-
-
Save sungkmi/3506ba60a555442770b56a03834fc04a to your computer and use it in GitHub Desktop.
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
package sungkmi.aoc2020.day5 | |
case class Seat(row: Int, column: Int) | |
extension (s: Seat): | |
def id: Int = s.row * 8 + s.column | |
def parseSeat(s: String): Seat = | |
val (rowString, columnString) = s splitAt 7 | |
def parseBinaryString(zero: Char, one: Char)(str: String): Int = BigInt(str.map{ | |
case c if c == zero => '0' | |
case c if c == one => '1' | |
}, 2).toInt | |
val row = parseBinaryString('F', 'B')(rowString) | |
val column = parseBinaryString('L', 'R')(columnString) | |
Seat(row, column) | |
@main def part1: Unit = | |
val ans = seats.map(_.id).max | |
println(ans) | |
@main def part2: Unit = | |
val ans = Range(55, 906) `diff` seats.map(_.id).sorted | |
println(ans) | |
lazy val seatStrings: Seq[String] = input.split("\n").toSeq | |
lazy val seats: Seq[Seat] = seatStrings `map` parseSeat | |
// lazy val input = """ ... |
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
package sungkmi.aoc2020.day5 | |
class Day5Test extends munit.FunSuite { | |
test("parse seat") { | |
val s = "FBFBBFFRLR" | |
val expected = Seat(44,5) | |
assertEquals(parseSeat(s), expected) | |
} | |
test("seat id") { | |
val s = "FBFBBFFRLR" | |
val expected = 357 | |
assertEquals(parseSeat(s).id, expected) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment