From 1ec7d193677e6777d875aa3ab378376df2fff360 Mon Sep 17 00:00:00 2001 From: Araozu Date: Wed, 1 Nov 2023 08:01:44 -0500 Subject: [PATCH] day 3 part 2 --- src/main/scala/Day03.scala | 59 ++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 6 deletions(-) diff --git a/src/main/scala/Day03.scala b/src/main/scala/Day03.scala index c5e549e..959e57a 100644 --- a/src/main/scala/Day03.scala +++ b/src/main/scala/Day03.scala @@ -2,9 +2,8 @@ import scala.util.Success import scala.util.Failure import scala.util.boundary - object Day03 extends Solution: - override def part_01() = + override def part_01() = val file = read_input("03") match case Success(f) => f case Failure(ex) => @@ -18,18 +17,67 @@ object Day03 extends Solution: val second_segment = line.slice(char_count / 2, char_count) val set = first_segment.toList.toSet - val res_char = second_segment.toCharArray().find((char) => set.contains(char)) match + val res_char = second_segment + .toCharArray() + .find((char) => set.contains(char)) match case Some(c) => c case None => return "Error: Input didn't have a repeating character" sum += char_to_value(res_char) - s"part 1: $sum" - override def part_02() = "" + override def part_02() = + val file = read_input("03") match + case Success(f) => f + case Failure(ex) => + return "Error opening file: " + ex.getMessage() + var (line0, line1, line2) = ("", "", "") + var line_count = 0 + var sum = 0 + + for line <- file.getLines() do + line_count match + case 0 => + line0 = line + line_count += 1 + case 1 => + line1 = line + line_count += 1 + case 2 => + line2 = line + line_count = 0 + + // Process the 3 lines + var map = collection.mutable.Map[Char, Int]() + + for + char <- line0 + if !map.contains(char) + do map(char) = 1 + + for + char <- line1 + if map.contains(char) + do map(char) = 2 + + for + char <- line2 + if map.contains(char) + if map(char) == 2 + do map(char) = 3 + + val result = map.find((_, count) => count == 3) match + case Some((char, _)) => char + case None => + println("Repeat not found in group.") + '0' + + sum += char_to_value(result) + + s"part 2: $sum" private def char_to_value(c: Char): Int = if c >= 'a' && c <= 'z' then c.toInt - 96 @@ -37,4 +85,3 @@ object Day03 extends Solution: else println(s"fed an invalid character: $c") -1 - \ No newline at end of file