Skip to content

Commit

Permalink
2023 - Day 18 - part 1 and 2
Browse files Browse the repository at this point in the history
  • Loading branch information
fmmr committed Dec 18, 2023
1 parent 62e6f22 commit f6296c1
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 9 deletions.
13 changes: 13 additions & 0 deletions src/main/kotlin/no/rodland/advent/Direction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,17 @@ enum class Direction(val c: Char, val num: Long) {
SOUTH -> WEST
WEST -> NORTH
}

operator fun times(num: Int): List<Direction> = (0..<num).map { _ -> this }
operator fun times(num: Long): List<Direction> = (0..<num).map { _ -> this }

companion object{
fun fromUDLR(c: Char): Direction = when (c) {
'U' -> NORTH
'D' -> SOUTH
'L' -> WEST
'R' -> EAST
else -> throw IllegalArgumentException("Unknown direction: $c")
}
}
}
51 changes: 46 additions & 5 deletions src/main/kotlin/no/rodland/advent_2023/Day18.kt
Original file line number Diff line number Diff line change
@@ -1,27 +1,68 @@
package no.rodland.advent_2023

import no.rodland.advent.Day
import no.rodland.advent.Direction
import no.rodland.advent.Pos

// template generated: 18/12/2023
// Fredrik Rødland 2023

class Day18(val input: List<String>) : Day<Long, Long, List<String>> {
// heavily inspired by https://www.youtube.com/watch?v=bGWK76_e-LM
// Shoelace formula: https://en.wikipedia.org/wiki/Shoelace_formula
// Pick's theorem: https://en.wikipedia.org/wiki/Pick%27s_theorem

class Day18(val input: List<String>) : Day<Long, Long, List<Day18.Command>> {

private val parsed = input.parse()

override fun partOne(): Long {
return 2
val points = parsed.map { it -> it.direction to it.num }
return shoelacePick(points)
// return buildMapAndTraverseAndCount()
}

override fun partTwo(): Long {
return 2
val points = parsed.map { it.colour }.map { it.takeLast(1).toDir() to it.dropLast(1).toInt(16) }
return shoelacePick(points)
}

private fun shoelacePick(points: List<Pair<Direction, Int>>): Long {
var boundaryPoints = 0
val posList = points
.fold(listOf(Pos(0, 0))) { acc, (dir, num) ->
boundaryPoints += num
val pos = acc.last().next(dir, num)
acc + pos
}

// https://en.wikipedia.org/wiki/Shoelace_formula
// {\displaystyle A={\frac {1}{2}}\sum _{i=1}^{n}x_{i}(y_{i+1}-y_{i-1})}
val shoelace = posList.mapIndexed { i, p -> p.x.toLong() * (posList[(i + 1) % posList.size].y - posList[(posList.size + i) % posList.size].y) }.sum()

// https://en.wikipedia.org/wiki/Pick%27s_theorem
// {\displaystyle A=i+{\frac {b}{2}}-1.}
// => i = A - b/2 + 1
val interiorPoints = shoelace - boundaryPoints / 2 + 1
return interiorPoints + boundaryPoints
}

override fun List<String>.parse(): List<String> {
private fun String.toDir(): Direction = when (this.first()) {
'0' -> Direction.EAST
'1' -> Direction.SOUTH
'2' -> Direction.WEST
'3' -> Direction.NORTH
else -> error("unknown dir: $this")
}

override fun List<String>.parse(): List<Command> {
return map { line ->
line
val split = line.split(" ", "(", ")", "#").filterNot { it.isBlank() }
Command(Direction.fromUDLR(split[0].first()), split[1].toInt(), split[2])
}
}

data class Command(val direction: Direction, val num: Int, val colour: String)

override val day = "18".toInt()
}

10 changes: 6 additions & 4 deletions src/test/kotlin/no/rodland/advent_2023/Day18Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ internal class Day18Test {
private val data18 = "2023/input_18.txt".readFile()
private val test18 = "2023/input_18_test.txt".readFile()

private val resultTestOne = 2L
private val resultTestTwo = 2L
private val resultOne = 2L
private val resultTwo = 2L
private val resultTestOne = 62L
private val resultTestTwo = 952408144115L
private val resultOne = 40714L
private val resultTwo = 129849166997110L

val test = defaultTestSuiteParseOnInit(
Day18(data18),
Expand All @@ -29,6 +29,8 @@ internal class Day18Test {
resultTwo,
{ Day18(data18) },
{ Day18(test18) },
numTestPart1 = 1000,
numTestPart2 = 1000,
)

@Nested
Expand Down
14 changes: 14 additions & 0 deletions src/test/resources/2023/input_18_test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
R 6 (#70c710)
D 5 (#0dc571)
L 2 (#5713f0)
D 2 (#d2c081)
R 2 (#59c680)
D 2 (#411b91)
L 5 (#8ceee2)
U 2 (#caa173)
L 1 (#1b58a2)
U 2 (#caa171)
R 2 (#7807d2)
U 3 (#a77fa3)
L 2 (#015232)
U 2 (#7a21e3)

0 comments on commit f6296c1

Please sign in to comment.