-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
79 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |