From 1214979d4dd5c01be8875718ba0457c88d783e0e Mon Sep 17 00:00:00 2001 From: kamoshi <18511281+kamoshi@users.noreply.github.com> Date: Sun, 8 Dec 2019 08:31:40 +0100 Subject: [PATCH] Update worksheet8.sc --- 2019/Scala/day08/worksheet8.sc | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/2019/Scala/day08/worksheet8.sc b/2019/Scala/day08/worksheet8.sc index 1b59d68..ecd3508 100644 --- a/2019/Scala/day08/worksheet8.sc +++ b/2019/Scala/day08/worksheet8.sc @@ -1,8 +1,33 @@ import kamlib.Reader -val zeroes = Reader.readString("/input8.txt").toList.map(_.asDigit).grouped(150).toList.map(x => (x.foldLeft(0){(acc, elem) => if (elem==0) acc+1 else acc}, x)) -val minimum = zeroes.minBy(_._1)._2 +val minimum = Reader.readString("/input8.txt").toList + .map(_.asDigit).grouped(150).toList + .map(x => (x.foldLeft(0){(acc, elem) => if (elem==0) acc+1 else acc}, x)) + .minBy(_._1)._2 val ones = minimum.foldLeft(0){(acc, next) => if (next==1) acc+1 else acc} val twos = minimum.foldLeft(0){(acc, next) => if (next==2) acc+1 else acc} println(ones*twos) + +// =========== PART 2 =================== // + +val array = Reader.readString("/input8.txt").toArray.map(_.asDigit) +val outputInts2 = Array.ofDim[Int](150) + +@scala.annotation.tailrec +def determineColor(array: Array[Int], nextPointer: Int): Int = +{ + array(nextPointer) match + { + case 0 => 0 + case 1 => 1 + case 2 => determineColor(array, nextPointer+150) + } +} + +for(i<-0 until 150) +{ + outputInts2(i) = determineColor(array, i) +} + +outputInts2.map(x => if (x == 1) 'X' else '.').grouped(25).foreach(x => println(x.mkString))