day 5 puzzle 1
This commit is contained in:
parent
9f27d02b93
commit
6d95994276
16
README.md
16
README.md
@ -5,11 +5,11 @@ This repo contains my attempt at this year's Advent of Code (2023)
|
||||
I will try and do some problems (probably not all of them) in Scala
|
||||
|
||||
## Progress:
|
||||
#### Stars: 8 / 50
|
||||
| Mon | Tue | Wed | Thu | Fri | Sat | Sun |
|
||||
|:-----------------:|:---:|:---:|:---:|:-----------------:|:-----------------:|:-----------------:|
|
||||
| | | | | 1<br>:star::star: | 2<br>:star::star: | 3<br>:star::star: |
|
||||
| 4<br>:star::star: | 5 | 6 | 7 | 8 | 9 | 10 |
|
||||
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
|
||||
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
|
||||
| 25 | | | | | | |
|
||||
#### Stars: 9 / 50
|
||||
| Mon | Tue | Wed | Thu | Fri | Sat | Sun |
|
||||
|:-----------------:|:-----------:|:---:|:---:|:-----------------:|:-----------------:|:-----------------:|
|
||||
| | | | | 1<br>:star::star: | 2<br>:star::star: | 3<br>:star::star: |
|
||||
| 4<br>:star::star: | 5<br>:star: | 6 | 7 | 8 | 9 | 10 |
|
||||
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
|
||||
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
|
||||
| 25 | | | | | | |
|
83
src/day5/Puzzle1.scala
Normal file
83
src/day5/Puzzle1.scala
Normal file
@ -0,0 +1,83 @@
|
||||
package day5
|
||||
|
||||
import scala.io.{BufferedSource, Source}
|
||||
|
||||
object Puzzle1 {
|
||||
case class Range(startSrc: Long, startDst: Long, length: Long)
|
||||
var forwardingTables: Array[Array[Range]] = Array.empty
|
||||
var seeds: Array[Long] = Array.empty
|
||||
def loadInput(path: String): Unit = {
|
||||
val source: BufferedSource = Source.fromFile(path)
|
||||
|
||||
val almanac: String = source.getLines().mkString("\n")
|
||||
val tables: Array[String] = almanac.split("\n\n")
|
||||
|
||||
forwardingTables = new Array(tables.length - 1)
|
||||
seeds = tables(0).substring(7).split(" ").map(s => s.toLong)
|
||||
|
||||
for (i: Int <- 1 until tables.length) {
|
||||
val lines: Array[String] = tables(i).split(":\n")(1).split("\n")
|
||||
val table: Array[Range] = new Array(lines.length)
|
||||
for ((line: String, j: Int) <- lines.zipWithIndex) {
|
||||
val values: Array[Long] = line.split(" ").map(v => v.toLong)
|
||||
table(j) = Range(values(1), values(0), values(2))
|
||||
}
|
||||
forwardingTables(i-1) = table
|
||||
}
|
||||
|
||||
source.close()
|
||||
}
|
||||
|
||||
def sortTables(): Unit = {
|
||||
for (table: Array[Range] <- forwardingTables) {
|
||||
var changed: Boolean = false
|
||||
do {
|
||||
changed = false
|
||||
for (i: Int <- 0 until table.length-1) {
|
||||
if (table(i).startSrc >= table(i + 1).startSrc + table(i + 1).length) {
|
||||
changed = true
|
||||
val range: Range = table(i)
|
||||
table(i) = table(i + 1)
|
||||
table(i + 1) = range
|
||||
}
|
||||
}
|
||||
} while (changed)
|
||||
}
|
||||
}
|
||||
def map(seed: Long): Long = {
|
||||
var value: Long = seed
|
||||
for (i: Int <- forwardingTables.indices) {
|
||||
value = forward(value, i)
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
def forward(value: Long, tableI: Int): Long = {
|
||||
for (range: Range <- forwardingTables(tableI)) {
|
||||
if (value >= range.startSrc) {
|
||||
if (value < range.startSrc + range.length) {
|
||||
return value - range.startSrc + range.startDst
|
||||
}
|
||||
} else {
|
||||
return value
|
||||
}
|
||||
}
|
||||
return value
|
||||
}
|
||||
def solve(path: String): Long = {
|
||||
loadInput(path)
|
||||
sortTables()
|
||||
var smallest: Long = -1
|
||||
for (seed: Long <- seeds) {
|
||||
val location: Long = map(seed)
|
||||
if (smallest == -1 || location < smallest) {
|
||||
smallest = location
|
||||
}
|
||||
}
|
||||
return smallest
|
||||
}
|
||||
def main(args: Array[String]): Unit = {
|
||||
val solution: Long = solve("res/day5/input1.txt")
|
||||
println(solution)
|
||||
}
|
||||
}
|
9
tests/day5/Puzzle1Test.scala
Normal file
9
tests/day5/Puzzle1Test.scala
Normal file
@ -0,0 +1,9 @@
|
||||
package day5
|
||||
|
||||
import org.scalatest.funsuite.AnyFunSuite
|
||||
|
||||
class Puzzle1Test extends AnyFunSuite {
|
||||
test("Puzzle1.solve") {
|
||||
assert(Puzzle1.solve("tests_res/day5/input1.txt") == 35)
|
||||
}
|
||||
}
|
33
tests_res/day5/input1.txt
Normal file
33
tests_res/day5/input1.txt
Normal file
@ -0,0 +1,33 @@
|
||||
seeds: 79 14 55 13
|
||||
|
||||
seed-to-soil map:
|
||||
50 98 2
|
||||
52 50 48
|
||||
|
||||
soil-to-fertilizer map:
|
||||
0 15 37
|
||||
37 52 2
|
||||
39 0 15
|
||||
|
||||
fertilizer-to-water map:
|
||||
49 53 8
|
||||
0 11 42
|
||||
42 0 7
|
||||
57 7 4
|
||||
|
||||
water-to-light map:
|
||||
88 18 7
|
||||
18 25 70
|
||||
|
||||
light-to-temperature map:
|
||||
45 77 23
|
||||
81 45 19
|
||||
68 64 13
|
||||
|
||||
temperature-to-humidity map:
|
||||
0 69 1
|
||||
1 0 69
|
||||
|
||||
humidity-to-location map:
|
||||
60 56 37
|
||||
56 93 4
|
Loading…
Reference in New Issue
Block a user