Advent Of Code
An unofficial home for the advent of code community on programming.dev!
Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.
AoC 2024
Solution Threads
M | T | W | T | F | S | S |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 |
Rules/Guidelines
- Follow the programming.dev instance rules
- Keep all content related to advent of code in some way
- If what youre posting relates to a day, put in brackets the year and then day number in front of the post title (e.g. [2024 Day 10])
- When an event is running, keep solutions in the solution megathread to avoid the community getting spammed with posts
Relevant Communities
Relevant Links
Credits
Icon base by Lorc under CC BY 3.0 with modifications to add a gradient
console.log('Hello World')
Scala3
Not much to say about this, very straightforward implementation that was still fast enough
case class Pos3(x: Int, y: Int, z: Int)
case class Brick(blocks: List[Pos3]):
def dropBy(z: Int) = Brick(blocks.map(b => b.copy(z = b.z - z)))
def isSupportedBy(other: Brick) = ???
def parseBrick(a: String): Brick = a match
case s"$x1,$y1,$z1~$x2,$y2,$z2" => Brick((for x <- x1.toInt to x2.toInt; y <- y1.toInt to y2.toInt; z <- z1.toInt to z2.toInt yield Pos3(x, y, z)).toList)
def dropOn(bricks: List[Brick], brick: Brick): (List[Brick], List[Brick]) =
val occupied = bricks.flatMap(d => d.blocks.map(_ -> d)).toMap
@tailrec def go(d: Int): (Int, List[Brick]) =
val dropped = brick.dropBy(d).blocks.toSet
if dropped.intersect(occupied.keySet).isEmpty && !dropped.exists(_.z <= 0) then
go(d + 1)
else
(d - 1, occupied.filter((p, b) => dropped.contains(p)).map(_._2).toSet.toList)
val (d, supp) = go(0)
(brick.dropBy(d) :: bricks, supp)
def buildSupportGraph(bricks: List[Brick]): Graph[Brick, DiEdge[Brick]] =
val (bs, edges) = bricks.foldLeft((List[Brick](), List[DiEdge[Brick]]()))((l, b) =>
val (bs, supp) = dropOn(l._1, b)
(bs, supp.map(_ ~> bs.head) ++ l._2)
)
Graph() ++ (bs, edges)
def parseSupportGraph(a: List[String]): Graph[Brick, DiEdge[Brick]] =
buildSupportGraph(a.map(parseBrick).sortBy(_.blocks.map(_.z).min))
def wouldDrop(g: Graph[Brick, DiEdge[Brick]], b: g.NodeT): Long =
@tailrec def go(shaking: List[g.NodeT], falling: Set[g.NodeT]): List[g.NodeT] =
shaking match
case h :: t =>
if h.diPredecessors.forall(falling.contains(_)) then
go(h.diSuccessors.toList ++ t, falling + h)
else
go(t, falling)
case _ => falling.toList
go(b.diSuccessors.toList, Set(b)).size
def task1(a: List[String]): Long = parseSupportGraph(a).nodes.filter(n => n.diSuccessors.forall(_.inDegree > 1)).size
def task2(a: List[String]): Long =
val graph = parseSupportGraph(a)
graph.nodes.toList.map(wouldDrop(graph, _) - 1).sum
Nim
I sorted the bricks by their lower Z coordinate, then tried to move each of them downward, doing collision checks against all the others along the way. Once a level with collisions was found, I recorded each colliding brick as a supporter of the falling brick.
For part 1, I made another table of which other bricks each brick was supporting. Any bricks that weren't the sole support for any other bricks were counted as safe to disintegrate.
For part 2, I sorted the bricks again after applying gravity. For each brick, I included it in a set of bricks that would fall if it were removed, then checked the others further down the list to see if they had any non-falling supporters. Those that didn't would be added to the falling set.
Initially I was getting an answer for part 2 that was too high. I turned out that I was counting bricks that were on the ground as being unsupported, so some of them were getting included in the falling sets for their neighbors. Adding a z-level check fixed this.
Both of these have room for optimization, but non-debug builds run 0.5s and 1.0s respectively, so I didn't feel the need to write an octree implementation or anything.
Zig
https://github.com/Treeniks/advent-of-code/blob/master/2023/day22/zig/src/main.zig
(or on codeberg if you don't like to use github: https://codeberg.org/Treeniks/advent-of-code/src/branch/master/2023/day22/zig/src/main.zig )
Every time I use Zig, I love the result, but I hate writing it. The language is just a little too inflexible for quick and dirty solutions to quickly try out an idea or debug print something useful, but once you're done and have a result, it feels quite complete.
C
Part 1 was fun, essentially a matter of mapping a grid and implementing a function to scan above and below bricks.
Was worried part 2 would either make the grid approach impossible (large numbers) or have combinatory complexity that would necessitate some super efficient dependency table that I don't know about. Luckily that wasn't the case! Phew.