c28f1359358d — Quintillus 7 months ago
Splitting up the Day Five code and starting to add a bit of quality.

Still definitely don't have the tests in order, but there's more hope of
being able to build on it if need be.

Also starting to listen to IntelliJ's commit code analysis.  (Re-?)learned
primary constructor syntax, that's pretty cool.
2 files changed, 41 insertions(+), 29 deletions(-)

M src/main/kotlin/com/ajtjp/adventOfCode/airplaneSeats/AirplaneSeats.kt => src/main/kotlin/com/ajtjp/adventOfCode/airplaneSeats/AirplaneSeat.kt
M src/test/kotlin/com/ajtjp/adventOfCode/airplaneSeats/AirplaneSeatsTest.kt => src/test/kotlin/com/ajtjp/adventOfCode/airplaneSeats/AirplaneSeatTest.kt
M src/main/kotlin/com/ajtjp/adventOfCode/airplaneSeats/AirplaneSeats.kt => src/main/kotlin/com/ajtjp/adventOfCode/airplaneSeats/AirplaneSeat.kt +36 -27
@@ 1,36 1,18 @@ 
 package com.ajtjp.adventOfCode.airplaneSeats
 
-class AirplaneSeats {
+class AirplaneSeat(private val row: Int, private val column: Int) {
+
+    fun score() : Int {
+        return row * 8 + column
+    }
+
     companion object {
-        fun solveThePuzzle(puzzleInput: String) {
+        fun solveThePuzzle(puzzleInput: String): Int {
             val seatIds: MutableList<Int> = ArrayList()
             var max = 0
             for (line in puzzleInput.lines()) {
-                var rowMin = 0
-                var rowMax = 127
-                var colMax = 7
-                var colMin = 0
-                var diffPerRow = 64
-                var diffPerCol = 4
-                for (char in line.toCharArray()) {
-                    if (char == 'F') {
-                        rowMax -= diffPerRow
-                        diffPerRow/=2
-                    }
-                    else if (char == 'B') {
-                        rowMin += diffPerRow
-                        diffPerRow/=2
-                    }
-                    else if (char == 'L') {
-                        colMax -= diffPerCol
-                        diffPerCol/=2
-                    }
-                    else if (char == 'R') {
-                        colMin += diffPerCol
-                        diffPerCol/=2
-                    }
-                }
-                val score = rowMin * 8 + colMin
+                val seat = findSeat(line)
+                val score = seat.score()
                 if (score > max) {
                     max = score
                 }

          
@@ 45,6 27,33 @@ class AirplaneSeats {
                     println("missing $i")
                 }
             }
+
+            return max
+        }
+
+        private fun findSeat(seatSpecification: String): AirplaneSeat {
+            var rowMin = 0
+            var rowMax = 127
+            var colMax = 7
+            var colMin = 0
+            var diffPerRow = 64
+            var diffPerCol = 4
+            for (char in seatSpecification.toCharArray()) {
+                if (char == 'F') {
+                    rowMax -= diffPerRow
+                    diffPerRow /= 2
+                } else if (char == 'B') {
+                    rowMin += diffPerRow
+                    diffPerRow /= 2
+                } else if (char == 'L') {
+                    colMax -= diffPerCol
+                    diffPerCol /= 2
+                } else if (char == 'R') {
+                    colMin += diffPerCol
+                    diffPerCol /= 2
+                }
+            }
+            return AirplaneSeat(rowMin, colMin)
         }
     }
 }
  No newline at end of file

          
M src/test/kotlin/com/ajtjp/adventOfCode/airplaneSeats/AirplaneSeatsTest.kt => src/test/kotlin/com/ajtjp/adventOfCode/airplaneSeats/AirplaneSeatTest.kt +5 -2
@@ 1,8 1,9 @@ 
 package com.ajtjp.adventOfCode.airplaneSeats
 
+import org.junit.Assert.assertEquals
 import org.junit.Test
 
-class AirplaneSeatsTest {
+class AirplaneSeatTest {
 
     val dayFiveInput = """
 BFBFBFFRRL

          
@@ 897,6 898,8 @@ FBBBBBBLRR
 
     @Test
     fun dayFive_silver() {
-        AirplaneSeats.solveThePuzzle(dayFiveInput)
+        val max = AirplaneSeat.solveThePuzzle(dayFiveInput)
+
+        assertEquals(894, max)
     }
 }
  No newline at end of file