Created
December 6, 2025 06:27
-
-
Save tomnis/f392b67419dcee33f9fa74cccf638a8c to your computer and use it in GitHub Desktop.
day6part2
This file contains hidden or 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
| def part2(data: Seq[String]): Long = { | |
| val grid: Grid[Char] = data.dropRight(1).toArray.map(_.toCharArray).transpose | |
| var grandTotal: Long = 0 | |
| var curTotal: Long = 0 | |
| var op: Op = null | |
| data.last.zipWithIndex.foreach { case (ch, idx) => | |
| if (ch == '+') { | |
| curTotal = grid(idx).mkString.trim.toLong | |
| op = (a, b) => a + b | |
| } | |
| else if (ch == '*') { | |
| curTotal = grid(idx).mkString.trim.toLong | |
| op = (a, b) => a * b | |
| } | |
| else if (grid(idx).forall(_ == ' ') && ch == ' ') { | |
| grandTotal += curTotal | |
| curTotal = 0 | |
| } | |
| else if (ch == ' ') { | |
| val num = grid(idx).mkString.trim.toLong | |
| curTotal = op(curTotal, num) | |
| } | |
| else { | |
| ??? | |
| } | |
| } | |
| grandTotal += curTotal | |
| grandTotal | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment