-
Notifications
You must be signed in to change notification settings - Fork 0
/
2023-day01.scala
40 lines (28 loc) · 1.08 KB
/
2023-day01.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package day01
import scala.language.experimental.namedTuples
type Digits = (tens: Int, units: Int)
extension (ds: Digits) def toInt = ds.tens * 10 + ds.units
def basicMatch(line: String) = (
tens = line.iterator.filter(_.isDigit).next().asDigit,
units = line.reverseIterator.filter(_.isDigit).next().asDigit
)
def solution(input: String, search: String => Digits): Int =
input.linesIterator.map(search(_).toInt).sum
def part1(input: String): Int = solution(input, basicMatch)
val fullDigits =
IArray(
"one", "two", "three",
"four", "five", "six",
"seven", "eight", "nine"
) ++ (1 to 9).map(_.toString)
def fullMatch(line: String) =
(
tens = line.tails.map(t => fullDigits.indexWhere(t.startsWith)).filter(_ >= 0).next() % 9 + 1,
units = line.inits.map(t => fullDigits.indexWhere(t.endsWith)).filter(_ >= 0).next() % 9 + 1
)
def part2(input: String): Int = solution(input, fullMatch)
import challenges.*
@main def part1: Unit =
println(s"the answer is ${part1(inputToday())}")
@main def part2: Unit =
println(s"the answer is ${part2(inputToday())}")