Skip to content

Commit

Permalink
✨ day 20 part 1; slowwww
Browse files Browse the repository at this point in the history
  • Loading branch information
twentylemon committed Dec 20, 2024
1 parent f243963 commit cac5f8a
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
Binary file added src/main/resources/year2024/day20.txt
Binary file not shown.
45 changes: 45 additions & 0 deletions src/main/scala/org/lemon/advent/year2024/Day20.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.lemon.advent.year2024

import org.lemon.advent.lib._
import org.lemon.advent.lib.`2d`._
import org.lemon.advent.lib.graph._

private object Day20:

def parse = Coord.gridToMap

def findCheats(grid: Map[Coord, Char]): Seq[(Coord, Coord)] =
def isTrack(c: Char) = c == '.' || c == 'S' || c == 'E'
def get(coord: Coord) =
val c = grid.getOrElse(coord, ' ')
if isTrack(c) then '.' else c

grid.iterator
.filter((_, c) => isTrack(c))
.flatMap((coord, _) =>
for
d <- Direction.values
if get(coord.shift(d, 1)) == '#'
if get(coord.shift(d, 2)) == '.'
yield (coord, coord.shift(d, 2))
)
.toSeq.sorted

def adjacency(grid: Map[Coord, Char], cheat: (Coord, Coord))(coord: Coord) =
val neigh = coord.adjacent.filter(c => grid.getOrElse(c, '#') != '#').map((_, 1))
if coord == cheat._1 then (cheat._2, 2) +: neigh else neigh

def part1(input: String) =
val grid = parse(input)
val start = grid.find(_._2 == 'S').get._1
val end = grid.find(_._2 == 'E').get._1
val noCheating = pathFind(adjacency(grid, ((-1, -1), (-1, -1))), start, end).get.distance

val cheats = findCheats(grid)
cheats
.map(cheat => pathFind(adjacency(grid, cheat), start, end).get.distance)
.map(noCheating - _)
.count(_ >= 100)

def part2(input: String) =
0
39 changes: 39 additions & 0 deletions src/test/scala/org/lemon/advent/year2024/Day20Test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.lemon.advent.year2024

import org.lemon.advent._
import org.lemon.advent.year2024.Day20._

class Day20Test extends UnitTest:

test("part 1 example") {
val in = """|###############
|#...#...#.....#
|#.#.#.#.#.###.#
|#S#...#.#.#...#
|#######.#.#.###
|#######.#.#...#
|#######.#.###.#
|###..E#...#...#
|###.#######.###
|#...###...#...#
|#.#####.#.###.#
|#.#...#.#.#...#
|#.#.#.#.#.#.###
|#...#...#...###
|###############""".stripMargin
part1(in) shouldBe 0
}

test("part 1") {
part1(read(file(2024)(20))) shouldBe 1289
}

test("part 2 example") {
val in = """|
|""".stripMargin
part2(in) shouldBe 0
}

test("part 2") {
part2(read(file(2024)(20))) shouldBe 0
}

0 comments on commit cac5f8a

Please sign in to comment.