@@ -65,8 +65,8 @@ Now it's a matter of processing each column, which is fairly straightforward.
6565Let's define an [ extension method] ( https://docs.scala-lang.org/scala3/reference/contextual/extension-methods.html ) to improve readability.
6666
6767``` scala
68- extension (xs : IterableOnce [(symbol : String , nums : IterableOnce [String ])])
69- inline def calculate : Long = xs.iterator.collect {
68+ extension (xs : Iterator [(symbol : String , nums : IterableOnce [String ])])
69+ def calculate : Long = xs.iterator.collect {
7070 case (" *" , nums) => nums.iterator.map(_.toLong).product
7171 case (" +" , nums) => nums.iterator.map(_.toLong).sum
7272 }.sum
@@ -202,12 +202,11 @@ Reusing the `calculate` extension method from part 1, we can now finish part 2:
202202
203203``` scala
204204def part2 (input : String ): Long =
205- val lines = input.linesIterator.toVector
206- val ops = lines.last.split(raw " \s+ " ).toVector
205+ val lines = input.linesIterator
206+ val ops = lines.last.split(raw " \s+ " ).iterator
207207 val xss = lines.init.transpose.map(_.mkString.trim).splitBy(" " )
208208
209- (ops lazyZip xss) // zip the operations with the chunks, lazily for efficiency
210- .calculate
209+ (ops zip xss).calculate // zip the operations with the chunks
211210```
212211
213212## Final Code
@@ -220,8 +219,8 @@ extension [A](xs: IterableOnce[A])
220219 if e != sep then cur += e else { b += cur.result(); cur.clear() }
221220 (b += cur.result()).result()
222221
223- extension (xs : IterableOnce [(symbol : String , nums : IterableOnce [String ])])
224- inline def calculate : Long = xs.iterator.collect {
222+ extension (xs : Iterator [(symbol : String , nums : IterableOnce [String ])])
223+ def calculate : Long = xs.iterator.collect {
225224 case (" *" , nums) => nums.iterator.map(_.toLong).product
226225 case (" +" , nums) => nums.iterator.map(_.toLong).sum
227226 }.sum
@@ -238,7 +237,7 @@ def part2(input: String): Long =
238237 val ops = lines.last.split(raw " \s+ " ).toVector
239238 val xss = lines.init.transpose.map(_.mkString.trim).splitBy(" " )
240239
241- (ops lazyZip xss).calculate
240+ (ops zip xss).calculate
242241```
243242
244243## Solutions from the community
0 commit comments