Compare commits
No commits in common. "bd84c48a2143f4860c40f88033807e2bcdba12ce" and "12240f6d8c584c2f0b15e34320fc5f9f7faf3ac8" have entirely different histories.
bd84c48a21
...
12240f6d8c
@ -1,57 +0,0 @@
|
||||
package day2
|
||||
|
||||
import scala.collection.immutable.HashMap
|
||||
import scala.collection.mutable.ArrayBuffer
|
||||
import scala.io.{BufferedSource, Source}
|
||||
|
||||
object Puzzle1 {
|
||||
var games: Array[String] = new Array(0)
|
||||
def loadInput(path: String): Unit = {
|
||||
val source: BufferedSource = Source.fromFile(path)
|
||||
games = source.getLines().toArray
|
||||
source.close()
|
||||
}
|
||||
def getPossible(content: Map[String, Int]): ArrayBuffer[Int] = {
|
||||
val result: ArrayBuffer[Int] = new ArrayBuffer()
|
||||
|
||||
for (game: String <- games) {
|
||||
val parts: Array[String] = game.split(": ")
|
||||
val gameId: Int = parts(0).split(" ")(1).toInt
|
||||
val sets: Array[String] = parts(1).split("; ")
|
||||
|
||||
if (isGamePossible(sets, content)) {
|
||||
result.addOne(gameId)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
def isGamePossible(sets: Array[String], content: Map[String, Int]): Boolean = {
|
||||
for (set: String <- sets) {
|
||||
val groups: Array[String] = set.split(", ")
|
||||
for (group: String <- groups) {
|
||||
val parts: Array[String] = group.split(" ")
|
||||
val count: Int = parts(0).toInt
|
||||
val color: String = parts(1)
|
||||
|
||||
if (content(color) < count) return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
def solve(path: String, content: Map[String, Int]): Int = {
|
||||
loadInput(path)
|
||||
val possibleGames: Array[Int] = getPossible(content).toArray
|
||||
return possibleGames.sum
|
||||
}
|
||||
def main(args: Array[String]): Unit = {
|
||||
val solution: Int = solve("res/day2/input1.txt", HashMap(
|
||||
"red" -> 12,
|
||||
"green" -> 13,
|
||||
"blue" -> 14
|
||||
))
|
||||
println(solution)
|
||||
}
|
||||
}
|
@ -1,59 +0,0 @@
|
||||
package day2
|
||||
|
||||
import scala.collection.mutable
|
||||
import scala.io.{BufferedSource, Source}
|
||||
|
||||
object Puzzle2 {
|
||||
var games: Array[String] = new Array(0)
|
||||
def loadInput(path: String): Unit = {
|
||||
val source: BufferedSource = Source.fromFile(path)
|
||||
games = source.getLines().toArray
|
||||
source.close()
|
||||
}
|
||||
|
||||
def getPowers(): Array[Int] = {
|
||||
val powers: Array[Int] = new Array(games.length)
|
||||
for ((game: String, i: Int) <- games.zipWithIndex) {
|
||||
powers(i) = getPower(game)
|
||||
}
|
||||
return powers
|
||||
}
|
||||
|
||||
def getPower(game: String): Int = {
|
||||
var power: Int = 1
|
||||
|
||||
val sets: Array[String] = game.split(": ")(1).split("; ")
|
||||
val minCounts: mutable.Map[String, Int] = new mutable.HashMap()
|
||||
|
||||
for (set: String <- sets) {
|
||||
val groups: Array[String] = set.split(", ")
|
||||
for (group: String <- groups) {
|
||||
val parts: Array[String] = group.split(" ")
|
||||
val count: Int = parts(0).toInt
|
||||
val color: String = parts(1)
|
||||
|
||||
if (minCounts.contains(color)) {
|
||||
minCounts(color) = math.max(minCounts(color), count)
|
||||
} else {
|
||||
minCounts(color) = count
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
minCounts.foreach((p: (String, Int)) => {
|
||||
power *= p._2
|
||||
})
|
||||
return power
|
||||
}
|
||||
|
||||
def solve(path: String): Int = {
|
||||
loadInput(path)
|
||||
val powers: Array[Int] = getPowers()
|
||||
return powers.sum
|
||||
}
|
||||
|
||||
def main(args: Array[String]): Unit = {
|
||||
val solution: Int = solve("res/day2/input1.txt")
|
||||
println(solution)
|
||||
}
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
package day2
|
||||
|
||||
import org.scalatest.funsuite.AnyFunSuite
|
||||
|
||||
import scala.collection.immutable.HashMap
|
||||
|
||||
class Puzzle1Test extends AnyFunSuite {
|
||||
test("Puzzle1.solve") {
|
||||
assert(Puzzle1.solve("tests_res/day2/input1.txt", HashMap(
|
||||
"red" -> 12,
|
||||
"green" -> 13,
|
||||
"blue" -> 14
|
||||
)) == 8)
|
||||
}
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
package day2
|
||||
|
||||
import org.scalatest.funsuite.AnyFunSuite
|
||||
|
||||
class Puzzle2Test extends AnyFunSuite {
|
||||
test("Puzzle2.solve") {
|
||||
assert(Puzzle2.solve("tests_res/day2/input1.txt") == 2286)
|
||||
}
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
|
||||
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
|
||||
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
|
||||
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
|
||||
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green
|
Loading…
Reference in New Issue
Block a user