day 3 part 1

This commit is contained in:
Araozu 2023-11-01 07:34:58 -05:00
parent 2dcca0e720
commit 9d13ad81cc
2 changed files with 42 additions and 2 deletions

View File

@ -0,0 +1,40 @@
import scala.util.Success
import scala.util.Failure
import scala.util.boundary
object Day03 extends Solution:
override def part_01() =
val file = read_input("03") match
case Success(f) => f
case Failure(ex) =>
return "Error opening file: " + ex.getMessage()
var sum = 0
for line <- file.getLines() do
val char_count = line.length()
val first_segment = line.slice(0, char_count / 2)
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
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() = ""
private def char_to_value(c: Char): Int =
if c >= 'a' && c <= 'z' then c.toInt - 96
else if c >= 'A' && c <= 'Z' then c.toInt - 64 + 26
else
println(s"fed an invalid character: $c")
-1

View File

@ -4,5 +4,5 @@ import scala.util.Random
// Number guessing game // Number guessing game
@main def hello: Unit = @main def hello: Unit =
println("Advent of code 2022 with Scala") println("Advent of code 2022 with Scala")
println(Day02.part_01()) println(Day03.part_01())
println(Day02.part_02()) println(Day03.part_02())