Skip to content

Commit

Permalink
Add more tests to 2024 day 9
Browse files Browse the repository at this point in the history
  • Loading branch information
sim642 committed Dec 9, 2024
1 parent 4ab694c commit 9f743f5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/main/scala/eu/sim642/adventofcode2024/Day9.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ object Day9 {
})._1
}

def filesystemToString(filesystem: Filesystem): String = {
val sb = new StringBuilder
filesystem.foreach({
case File(id, size) => sb ++= ('0' + id).toChar.toString * size // TODO: toDigit?
case Free(size) => sb ++= "." * size
})
sb.toString
}

trait Part {
def compact(filesystem: Filesystem): Filesystem

Expand Down
12 changes: 8 additions & 4 deletions src/test/scala/eu/sim642/adventofcode2024/Day9Test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@ class Day9Test extends AnyFunSuite {
val exampleInput2 = "2333133121414131402"

test("Part 1 examples") {
//assert(Part1.compactChecksum(parseFilesystem(exampleInput)) == ???)
//println(Part1.compact(parseFilesystem(exampleInput)))
//println(Part1.compact(parseFilesystem(exampleInput2)))
assert(filesystemToString(Part1.compact(parseFilesystem(exampleInput))) ==
"022111222") // trailing Free trimmed
assert(filesystemToString(Part1.compact(parseFilesystem(exampleInput2))) ==
"0099811188827773336446555566") // trailing Free trimmed
assert(Part1.compactChecksum(parseFilesystem(exampleInput2)) == 1928)

assert(Part1.compactChecksum(parseFilesystem(exampleInput)) == 69) // not in text
}

test("Part 1 input answer") {
assert(Part1.compactChecksum(parseFilesystem(input)) == 6310675819476L)
}

test("Part 2 examples") {
//println(Part2.compact(parseFilesystem(exampleInput2)))
assert(filesystemToString(Part2.compact(parseFilesystem(exampleInput2))) ==
"00992111777.44.333....5555.6666.....8888..")
assert(Part2.compactChecksum(parseFilesystem(exampleInput2)) == 2858)
}

Expand Down

0 comments on commit 9f743f5

Please sign in to comment.