-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay05Part1.worksheet.sc
55 lines (46 loc) · 1.38 KB
/
Day05Part1.worksheet.sc
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import scala.compiletime.ops.int
import scala.collection.immutable.{Range => _}
type Range = scala.collection.immutable.NumericRange[Long]
val input = io.Source.fromResource("2023/day-05.txt").getLines()
val seeds =
val s"seeds: $seedsStr" = (input.next(): @unchecked)
seedsStr.split(" ").map(_.toLong).toList
input.next()
def extractRanges(): List[(Range, Range)] =
val lines = input.takeWhile(_.nonEmpty)
lines.map {
case s"$a $b $c" =>
val dest = a.toLong
val source = b.toLong
val length = c.toLong
(dest until dest + length, source until source + length)
}.toList
def next(current: List[Long], ranges: List[(Range, Range)]): List[Long] =
current.map: c =>
ranges.find(_._2.contains(c)).fold(c): (d, s) =>
println(s"found $c in $s going to $d")
// d.min + (c - s.min)
c + (d.min - s.min)
var current = seeds
println(current)
input.next()
current = next(current, extractRanges())
println(current)
input.next()
current = next(current, extractRanges())
println(current)
input.next()
current = next(current, extractRanges())
println(current)
input.next()
current = next(current, extractRanges())
println(current)
input.next()
current = next(current, extractRanges())
println(current)
input.next()
current = next(current, extractRanges())
println(current)
input.next()
val locations = next(current, extractRanges())
val ans1 = locations.min