Day 6
This commit is contained in:
parent
7a9322e593
commit
e7e65f8c9d
2 changed files with 37 additions and 0 deletions
32
src/day06.scala
Normal file
32
src/day06.scala
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
package aoc
|
||||
package day06
|
||||
|
||||
val dayNumber = "06"
|
||||
|
||||
@main def part1: Unit =
|
||||
println(part1(loadInput(dayNumber)))
|
||||
|
||||
@main def part2: Unit =
|
||||
println(part2(loadInput(dayNumber)))
|
||||
|
||||
def part1(input: String): String =
|
||||
val lines = input.split('\n').map(_.trim)
|
||||
val numbers = lines.init.map(_.split("\\s+").map(_.toLong)).transpose
|
||||
val ops = lines.last.split("\\s+").map[(Long, Long) => Long] {
|
||||
case "*" => _ * _
|
||||
case "+" => _ + _
|
||||
}
|
||||
numbers.zip(ops).map((numbers, op) => numbers.reduce(op)).sum.toString
|
||||
|
||||
def part2(input: String): String =
|
||||
val columns = input.split('\n').map(_.toArray).transpose.map(_.mkString).mkString("\n")
|
||||
val problems = columns.split("\n +\n")
|
||||
problems
|
||||
.map(p =>
|
||||
val lines = p.split('\n')
|
||||
val numbers = lines.map(_.filter(_.isDigit)).filterNot(_.isBlank).map(_.toLong)
|
||||
val op: (Long, Long) => Long = lines.head.last match
|
||||
case '*' => _ * _
|
||||
case '+' => _ + _
|
||||
numbers.reduce(op)
|
||||
).sum.toString
|
||||
Loading…
Add table
Add a link
Reference in a new issue