day 3 part 2
This commit is contained in:
parent
9d13ad81cc
commit
1ec7d19367
@ -2,7 +2,6 @@ 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
|
||||
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user