day 1 puzzle 1

This commit is contained in:
Louis Heredero 2023-12-01 09:07:15 +01:00
parent 0f65316dc1
commit 0ce0be9a6d
5 changed files with 1046 additions and 0 deletions

View File

@ -3,10 +3,14 @@
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/res" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/tests_res" type="java-test-resource" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="scala-sdk-2.13.12" level="application" />
<orderEntry type="library" name="scalatest.scalatest_2.13" level="project" />
</component>
</module>

1000
res/day1/input1.txt Normal file

File diff suppressed because it is too large Load Diff

29
src/day1/Puzzle1.scala Normal file
View File

@ -0,0 +1,29 @@
package day1
import scala.io.{BufferedSource, Source}
object Puzzle1 {
def loadInput(path: String): Int = {
var sum: Int = 0
val source: BufferedSource = Source.fromFile(path)
for (line: String <- source.getLines()) {
var a: Int = -1
var b: Int = -1
for (c: Char <- line) {
if ('0' <= c && c <= '9') {
if (a == -1) {
a = c - '0'
}
b = c - '0'
}
}
sum += a*10 + b
}
source.close()
return sum
}
def main(args: Array[String]): Unit = {
val sum: Int = loadInput("res/day1/input1.txt")
println(s"sum = $sum")
}
}

View File

@ -0,0 +1,9 @@
package day1
import org.scalatest.funsuite.AnyFunSuite
class Puzzle1Tester extends AnyFunSuite {
test("Puzzle1.loadInput") {
assert(Puzzle1.loadInput("tests_res/day1/input1.txt") == 142)
}
}

View File

@ -0,0 +1,4 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet