@@ -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
@@ -119,7 +119,7 @@ Which is exactly what we need to calculate cephalopod math!
119119``` scala
120120def part2 (input : String ) =
121121 val lines = input.linesIterator.toVector // get list of lines
122- val ops = lines.last.split(raw " \s+ " ).toVector // we'll use them later
122+ val ops = lines.last.split(raw " \s+ " ).iterator // we'll use them later
123123 lines
124124 .init // transposing requires all rows to be of equal length, so remove symbols from last line for simplicity
125125 .transpose
@@ -148,7 +148,7 @@ Now we can easily convert each column into cephalopod number strings:
148148``` scala
149149def part2 (input : String ) =
150150 val lines = input.linesIterator.toVector // get list of lines
151- val ops = lines.last.split(raw " \s+ " ).toVector // we'll use them later
151+ val ops = lines.last.split(raw " \s+ " ).iterator // we'll use them later
152152 lines.init.transpose.map(_.mkString.trim)
153153
154154// Vector(
@@ -187,7 +187,7 @@ extension [A](xs: IterableOnce[A])
187187``` scala
188188def part2 (input : String ) =
189189 val lines = input.linesIterator.toVector // get list of lines
190- val ops = lines.last.split(raw " \s+ " ).toVector // we'll use them later
190+ val ops = lines.last.split(raw " \s+ " ).iterator // we'll use them later
191191 lines.init.transpose.map(_.mkString.trim).splitBy(" " )
192192
193193// Vector(
@@ -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
@@ -235,10 +234,10 @@ def part1(input: String): Long = input.linesIterator.toVector
235234
236235def part2 (input : String ): Long =
237236 val lines = input.linesIterator.toVector
238- val ops = lines.last.split(raw " \s+ " ).toVector
237+ val ops = lines.last.split(raw " \s+ " ).iterator
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