this post was submitted on 09 Dec 2024
25 points (96.3% liked)

Advent Of Code

1008 readers
2 users here now

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

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 2 years ago
MODERATORS
25
submitted 1 month ago* (last edited 1 month ago) by CameronDev to c/advent_of_code
 

Day 9: Disk Fragmenter

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 4 points 1 month ago* (last edited 1 month ago)

Uiua

Just a port of my Dart solution from earlier really, and it shows, as it takes about 30 seconds on the live data.

(edit: I just noticed the little alien in the code (β‹…β‹…βˆ˜|β‹…βˆ˜|∘) which is literally flipping the stack (β•―Β°β–‘Β°)β•―οΈ΅ ┻━┻!)

How to read this

Try it live!

Data   ← "2333133121414131402"
FS     ← β†™βŒŠΓ·2⧻.▽≑⋕:β™­β‰βŠŸβŠƒ(⇑|β†―:Β―1)⧻.Data  # Build up a map of the FS.
MoveB  ← ⍜(⊑|β‹…)βŠƒ(β‹…β‹…βˆ˜|β‹…βˆ˜|∘) ⊑¯1.:βŠ’βŠšβŒ•Β―1. # Find a space, move block into it.
MoveBs ← ⍒(⍒(β†˜Β―1|=Β―1⊣)β†˜Β―1MoveB|>0β§»βŠšβŒ•Β―1)

TryMove ← ⨬(β—Œ|βˆ§βœβŠβ‡Œβ‰)/Γ—/>.
MoveFile ← (
  βŠƒ(βŠšβŒ•β†―:Β―1⧻|∘)βŠšβŒ•βŠ™.βŠ™.         # get posns from, start posn to.
  ⨬(β—Œβ—Œ|TryMove ⊟+βŠ™β—ŒΒ°βŠ,⊒)>0⧻. # check posn to is good, swap.
)
Check ← /+/Γ—βŠŸβ‡‘β§».β†₯0
&p Check MoveBs FS
&p Check ∧MoveFileβ‡Œ+1⇑/β†₯.FS

(edit: improved. Part1 is instant, part2 is about 17sec, but the alien has left)

Data   ← "2333133121414131402"
FS     ← ▽≑⋕:↙⧻:β™­β‰βŠŸβŠƒ(⇑|β†―:Β―1)⧻..Data # Build up a map of the FS.
Ixs    ← βŠƒ(⊚¬|β‡ŒβŠš)β‰₯0                 # Get indices of space, and of blocks reversed.
SwapBs ← β–½βŠΈβ‰‘/>β‰βŠŸβˆ©β†™βŸœ:β†§βˆ©β§»,,           # Join them where space < block.

Files ← β‡Œβ‰‘(β–‘βŠš)⊞=⇑+1/β†₯.
Move  ← ∧(βœβŠβ‡Œ)β‰βŠŸ+⇑⧻,⊒ # (tos, froms, fs)
MoveFile ← (
  βŠšβŒ•βŠ™,β†―:Β―1⧻.                # List of possible starts
  ⨬(β—Œβ—Œ|⨬(β—Œβ—Œ|Move)>∩⊒,,)>0⧻. # Only valid, leftwards starts 
)
Check ← /+/Γ—βŠŸβ‡‘β§».β†₯0
&p Check βˆ§βœβŠβ‡ŒSwapBs⊸Ixs FS
&p Check βˆ§β—‡MoveFile Files .FS