Compare commits
No commits in common. "3f45cddfb18089e448ebc520494eba32c03e2799" and "8cbe1a8f52098dc382e7dbcf9cd0592a4535ef1a" have entirely different histories.
3f45cddfb1
...
8cbe1a8f52
@ -1,87 +0,0 @@
|
||||
package day3
|
||||
|
||||
import scala.io.{BufferedSource, Source}
|
||||
|
||||
object Puzzle1 {
|
||||
val OFFSETS: Array[Array[Int]] = Array(
|
||||
Array(-1,-1), Array( 0,-1), Array( 1,-1),
|
||||
Array(-1, 0), Array( 1, 0),
|
||||
Array(-1, 1), Array( 0, 1), Array( 1, 1)
|
||||
)
|
||||
var width = 0
|
||||
var height = 0
|
||||
|
||||
var schematic: Array[Array[Char]] = Array.empty
|
||||
def loadInput(path: String): Unit = {
|
||||
val source: BufferedSource = Source.fromFile(path)
|
||||
val lines: Array[String] = source.getLines().toArray
|
||||
height = lines.length
|
||||
width = if (height == 0) 0 else lines(0).length
|
||||
schematic = new Array(height)
|
||||
|
||||
var i = 0
|
||||
for (line: String <- lines) {
|
||||
schematic(i) = line.split("").map(s => s(0))
|
||||
i += 1
|
||||
}
|
||||
|
||||
source.close()
|
||||
}
|
||||
|
||||
def getNumber(sx: Int, sy: Int): String = {
|
||||
var x: Int = sx
|
||||
var number: String = ""
|
||||
var c: Char = schematic(sy)(sx)
|
||||
do {
|
||||
number += c
|
||||
x += 1
|
||||
if (x < width) c = schematic(sy)(x)
|
||||
} while ('0' <= c && c <= '9' && x < width)
|
||||
return number
|
||||
}
|
||||
|
||||
def isPartNumber(sx: Int, sy: Int, len: Int): Boolean = {
|
||||
for (x: Int <- sx until sx+len) {
|
||||
for (offset: Array[Int] <- OFFSETS) {
|
||||
val x2: Int = x + offset(0)
|
||||
val y2: Int = sy + offset(1)
|
||||
if (0 <= x2 && x2 < width && 0 <= y2 && y2 < height) {
|
||||
val c: Char = schematic(y2)(x2)
|
||||
if (c != '.' && (c < '0' || c > '9')) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
def solve(path: String): Int = {
|
||||
loadInput(path)
|
||||
|
||||
var sum: Int = 0
|
||||
var y: Int = 0
|
||||
var x: Int = 0
|
||||
|
||||
while (y < height) {
|
||||
while (x < width) {
|
||||
val c: Char = schematic(y)(x)
|
||||
if ('0' <= c && c <= '9') {
|
||||
val number: String = getNumber(x, y)
|
||||
if (isPartNumber(x, y, number.length)) {
|
||||
sum += number.toInt
|
||||
x += number.length - 1
|
||||
}
|
||||
}
|
||||
x += 1
|
||||
}
|
||||
y += 1
|
||||
x = 0
|
||||
}
|
||||
return sum
|
||||
}
|
||||
def main(args: Array[String]): Unit = {
|
||||
val solution: Int = solve("res/day3/input1.txt")
|
||||
println(solution)
|
||||
}
|
||||
}
|
@ -1,104 +0,0 @@
|
||||
package day3
|
||||
|
||||
import scala.collection.mutable.ArrayBuffer
|
||||
import scala.io.{BufferedSource, Source}
|
||||
|
||||
object Puzzle2 {
|
||||
case class Number(x: Int, y: Int, value: Int, length: Int)
|
||||
case class Gear(x: Int, y: Int, var ratio: Int = 1, var neighbours: Int = 0)
|
||||
|
||||
var width = 0
|
||||
var height = 0
|
||||
|
||||
var schematic: Array[Array[Char]] = Array.empty
|
||||
var numbers: ArrayBuffer[Number] = new ArrayBuffer()
|
||||
var gears: ArrayBuffer[Gear] = new ArrayBuffer()
|
||||
|
||||
def loadInput(path: String): Unit = {
|
||||
val source: BufferedSource = Source.fromFile(path)
|
||||
val lines: Array[String] = source.getLines().toArray
|
||||
height = lines.length
|
||||
width = if (height == 0) 0 else lines(0).length
|
||||
schematic = new Array(height)
|
||||
|
||||
var i = 0
|
||||
for (line: String <- lines) {
|
||||
schematic(i) = line.split("").map(s => s(0))
|
||||
i += 1
|
||||
}
|
||||
|
||||
source.close()
|
||||
}
|
||||
|
||||
def getNumber(sx: Int, sy: Int): String = {
|
||||
var x: Int = sx
|
||||
var number: String = ""
|
||||
var c: Char = schematic(sy)(sx)
|
||||
do {
|
||||
number += c
|
||||
x += 1
|
||||
if (x < width) c = schematic(sy)(x)
|
||||
} while ('0' <= c && c <= '9' && x < width)
|
||||
return number
|
||||
}
|
||||
|
||||
def findNumbers(): Unit = {
|
||||
var y: Int = 0
|
||||
var x: Int = 0
|
||||
|
||||
while (y < height) {
|
||||
while (x < width) {
|
||||
val c: Char = schematic(y)(x)
|
||||
if ('0' <= c && c <= '9') {
|
||||
val number: String = getNumber(x, y)
|
||||
numbers.addOne(Number(x, y, number.toInt, number.length))
|
||||
x += number.length - 1
|
||||
}
|
||||
x += 1
|
||||
}
|
||||
y += 1
|
||||
x = 0
|
||||
}
|
||||
}
|
||||
|
||||
def findGears(): Unit = {
|
||||
for (y: Int <- 0 until height) {
|
||||
for (x: Int <- 0 until width) {
|
||||
if (schematic(y)(x) == '*') {
|
||||
gears.addOne(Gear(x, y))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def computePowers(): Unit = {
|
||||
for (gear: Gear <- gears) {
|
||||
for (number: Number <- numbers) {
|
||||
if (areNeighbours(gear, number)) {
|
||||
gear.ratio *= number.value
|
||||
gear.neighbours += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def areNeighbours(gear: Gear, number: Number): Boolean = {
|
||||
if (gear.x > number.x + number.length) return false
|
||||
if (gear.y > number.y + 1) return false
|
||||
if (number.x > gear.x + 1) return false
|
||||
if (number.y > gear.y + 1) return false
|
||||
return true
|
||||
}
|
||||
|
||||
def solve(path: String): Int = {
|
||||
loadInput(path)
|
||||
findNumbers()
|
||||
findGears()
|
||||
computePowers()
|
||||
return gears.filter(g => g.neighbours == 2).map(g => g.ratio).sum
|
||||
}
|
||||
def main(args: Array[String]): Unit = {
|
||||
val solution: Int = solve("res/day3/input1.txt")
|
||||
println(solution)
|
||||
}
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
package day3
|
||||
|
||||
import org.scalatest.funsuite.AnyFunSuite
|
||||
|
||||
class Puzzle1Test extends AnyFunSuite {
|
||||
test("Puzzle1.solve") {
|
||||
assert(Puzzle1.solve("tests_res/day3/input1.txt") == 4361)
|
||||
}
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
package day3
|
||||
|
||||
import org.scalatest.funsuite.AnyFunSuite
|
||||
|
||||
class Puzzle2Test extends AnyFunSuite {
|
||||
test("Puzzle2.solve") {
|
||||
assert(Puzzle2.solve("tests_res/day3/input1.txt") == 467835)
|
||||
}
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
467..114..
|
||||
...*......
|
||||
..35..633.
|
||||
......#...
|
||||
617*......
|
||||
.....+.58.
|
||||
..592.....
|
||||
......755.
|
||||
...$.*....
|
||||
.664.598..
|
Loading…
Reference in New Issue
Block a user