mykl

joined 2 years ago
MODERATOR OF
[–] mykl@lemmy.world 4 points 1 week ago* (last edited 1 week ago)

Obviously very spoilery code, if you dare read it closely. try it here

It feels very anticlimactic to be ending so early in the month, but see you all next year!

# AOC 2025 day 12 - Packing parcels
▽⊸≡◇˜∊@x⊜□⊸≠@\n&fras"AOC2025day12.txt"
/+<⊙×₉≡◇(⊓/×/+⊃↙↘2⊜⋕¬⊸∊": x")

[–] mykl@lemmy.world 7 points 1 week ago* (last edited 1 week ago)

huh?

⇌+@A×4+5˜⊂0+3⊂0[+1⊸˙×2]
[–] mykl@lemmy.world 2 points 1 week ago

Yes, it felt a little more like I was solving the puzzle-setter rather than the puzzle today.

[–] mykl@lemmy.world 1 points 1 week ago

There’s so many great features hidden behind ‘un’.

I’m sure that part2 could be simplified significantly but I was fed up by then.

[–] mykl@lemmy.world 2 points 1 week ago* (last edited 1 week ago) (4 children)

Uiua

If it's stupid but it works...

My dirty hackI broke the maze into SVR>FFT>DAC>OUT and SVR>DAC>FFT>OUT, realised that the latter was taking a suspiciously long time, so submitted just the answer from the former --> success!

link (You'll need to up the execution limit)

# AOC2025day11 - mazes.
# Uncomment for Part 1
D ← "aaa: you hhh\nyou: bbb ccc\nbbb: ddd eee\nccc: ddd eee fff\nddd: ggg\neee: out\nfff: out\nggg: out\nhhh: ccc fff iii\niii: out"
# Uncomment for Part 2
# D ← "svr: aaa bbb\naaa: fft\nfft: ccc\nbbb: tty\ntty: ccc\nccc: ddd eee\nddd: hub\nhub: fff\neee: dac\ndac: fff\nfff: ggg hhh\nggg: out\nhhh: out"
# D      ← &fras "randomAOC/AOC2025day11.txt"
Tabs   ← ⊜(⊙□°⊂⊜∘¬⊸∊": ")⊸≠@\nD
Nexts  ← ⍣(°□⊡˜⨂⊙Tabs|[])
Part₁  ← ⊙◌⧻path(˙≠°⊏Nexts|≍"out")"you"

Part₂ ← (
  ⊙◌⧻path(˙≠°⊏Nexts|≍"fft")"svr"
  ⊙◌⧻path(˙≠°⊏Nexts|≍"dac")"fft"
  ⊙◌⧻path(˙≠°⊏Nexts|≍"out")"dac"
  ××
)
# Only one will be right for the test data, depending on dataset.
⊃Part₁ Part₂
[–] mykl@lemmy.world 5 points 1 week ago

I'm getting PTSD from today now.

[–] mykl@lemmy.world 5 points 1 week ago* (last edited 1 week ago) (4 children)

Uiua

(added language tag)

Quiet here, isn't it?

Here's part1 to be going on with.

# AOC 2025 Day 10 - Wiring maze
D ← "[.##.] (3) (1,3) (2) (2,3) (0,2) (0,1) {3,5,4,7}\n[...#.] (0,2,3,4) (2,3) (0,4) (0,1,2) (1,2,3,4) {7,5,12,7,2}\n[.###.#] (0,1,2,3,4) (0,3,4) (0,1,2,4,5) (1,2) {10,11,11,5,10,5}"
# D      ← &fras"randomAOC/AOC2025day10.txt"
Digits ← ⊜⋕⊸∊+@0⇡10
Parse  ← ⊜(□⊜□⊸≠@\s)⊸≠@\n
Part₁ ← (
  ≡◇(
    =@#↘₁↘₋₁°□°⊂↘¯1  # target
    ⊙⬚0≡◇(°⊚Digits)  # presses
    ⧻⊢path(≡⌞≠|=0/+) # find shortest path
  )
  /+-1
)
Part₁ Parse D

I've given up on Part 2. I knew what I needed to do but didn't have the understanding of how to use the matrix elimination method to get beyond the first stages. But I did find this:

How to solve part 2 without librariesThis is a solver written totally from scratch in Dart, so easily readable unlike some other languages :-): [https://github.com/ayoubzulfiqar/advent-of-code/blob/main/2025/Dart/Day10/part_2.dart](GitHub link)

There's lots of parallelism (that's over the top for this problem), but the core solveSystem method is very clearly written, just long...

[–] mykl@lemmy.world 2 points 1 week ago

Squeezing all spaces out of the grid (and plotting at right-angles to the other visualisation here) gives this evil shape, which will doubtless haunt my dreams tonight.

[–] mykl@lemmy.world 5 points 2 weeks ago* (last edited 1 week ago) (2 children)

Uiua

Part 1 was easy, part 2 is ...less so...

a hint that might help youvisualising the data reveals some interesting patterns. Follow link to do so.

Any way, here's my Part 1 while I'm still thinking about this.

# AOC 2025 Day 09
# Experimental!
D ← &fras"AOC2025day09.txt"  # Drop your file here and edit name
/↥/×+1⌵⍉/-⍉₂⧅<2⋕°csv D       # Part 1 
∧⍜⊡⋅1⟜(˜↯0+1/↥)⍜⍉≡⍜⍆⊛⋕°csv D # Visualised data

Part 2

This is basically me thinking out loud, so it's untidy, brutal, repetitive and slow. (20s in the pad) link if you care

# Experimental!
# AOC 2025 Day 09
D     ← "7,1\n11,1\n11,7\n9,7\n9,5\n2,5\n2,3\n7,3"
D     ← &fras"AOC2025day09.txt" # Drop your file here and edit name
Parse ← ⋕°csv
Part₁ ← /↥/×+1⌵⍉/-⍉₂⧅<2

Squeeze ← ⍜⍉≡⍜⍆⊛          # Squeeze (add a sort to inspect)
Draw    ← ∧⍜⊡⋅1⟜(˜↯0+1/↥) # Draw

HLines ← (
  Squeeze Parse D
  ⍆⊕(□⍆)⊛⊸≡⊢
  ∧◇(⍜⊡˜⍜(⊏|˙=)⊙˜∘⊃(⊢⊢|↘⊙⇡°⊟+0_1⊣⍉))
)

VLines ← (
  Squeeze Parse D
  ⍆⊕(□⍆)⊛⊸≡⊣
  ∧◇(⍜⊡˜⍜(⊏|˙=)⊙˜∘⊃(⊣⊣|↘⊙⇡°⊟+0_1⊢⍉))
)
DrawBorder ← ⍜⍉VLines HLines ˜↯0↯2+1/↥♭Squeeze Parse D # Draws full border

# DrawBorder Squeeze # running this shows these as key boundary points -> [219 121] [219 123]
# ⊏≡⌟˜⨂[[219 121] [219 123]]⊸Squeeze # which map to -> [[94985 48652][94985 50114]]
# leftmost -> only look left, rightmost-> only look right
# SO, now fill the shape to make this easier.
Fill ← (
  ⊙⊙1           # fill colour.
  Good ← =0⊙◌⊡⊢ # Needs filling. Simple edges-check.
  # Take first of list. If needs fill, do so and then add
  # its four neighbours into queue. Repeat until queue is empty.
  ⍢(⨬(↘1|◴⊂+A₂¤°⊂ ⊃(⋅∘|∘|⋅⋅⋅∘)◡(⍜(⊡|⋅∘)⊢))◡Good
  | >0⧻)
  ⋅⊙⋅
)
DrawBorder     # Comment out everything below here to view the boundary.
Fill [219_120] # Now fill that boundary ([2_1] for test)
Squeeze Parse D
# ([0 1] for test)
[219 123] #  [219 121] gave the wrong answer, so it's this.
≡⌞⊟       # couple this with every other point
# Extract the covered window of the array for each pair of corners.
# (Very probably doing this the hard way:-()
# Only keep those that are all 1.
▽⤚≡⌟(=1/×♭≡⌞⊏⊙⊏˜∘∩(↘⊙⇡°⊟+0_1⍆)°⊟⍉)
# Pick out our squeezed indices
⨂Squeeze Parse D
# Find out what the unsqueezed values were
˜⊏Parse D
# Find areas, return max.
/↥≡/×+1⌵≡/-
[–] mykl@lemmy.world 1 points 2 weeks ago

Yeah, that's one thing the gurus keep hammering home: anything you can move out of loop constructs (inc rows, partition, etc as well as the obvious do, repeat) and handle pervasively is a big win.

[–] mykl@lemmy.world 3 points 2 weeks ago

I'm more like the sorcerer's apprentice watching my arrays get increasingly out of my control :-)

[–] mykl@lemmy.world 1 points 2 weeks ago

spoilerStupider than that: I'd just got halfway through writing a complicated divide and conquer algorithm to calculate nearest pairs when I realised that I could just brute-force it. :-)
But yes, precomputing was definitely the way to go.

21
submitted 1 year ago* (last edited 1 year ago) by mykl@lemmy.world to c/advent_of_code@programming.dev
 

I wanted to get an idea of how the blocks were landing and here's some thoughts on what I came up with:

  • they were building a simple maze (duh I guess).
  • the final (blocking) block is highlighted as a tiny red dot for half a second or so (edit: now flashing!).
  • my generated path jumped about seemingly at random even when blocks landed elsewhere so I don't animate the dropping of the first 1000 blocks as it's more noise than data.
  • the ~500 blocks before the final one don't affect my path at all, so it's quite a boring end.
  • Lemmy doesn't like long animations, so I skip 10 blocks at a time.

If you want to toast your CPU for a few seconds, here's some terrible Uiua code.

Data  ← ≡◇(⊜⋕⊸≠@,)°/$"_\n_" &fras "AOC2024day18.txt"
End   ← 70_70
Count ← 1024

D₄      ← [1_0 ¯1_0 0_1 0_¯1]
Valid   ← ▽¬⊸∊:▽⊸(≡/××⊃(≤⊢End|≥0))+D₄¤
BestLen ← ⍣(-1⧻⊢path(Valid|≍End)0_0↙:Data|∞)
Chop!   ← ◌⍢(⨬(⊙◌+1|⊙⊙◌:):⟜^0⌊÷2+,,|>)

BadBlock ← -1Chop!(=∞BestLen)Count ⧻Data
Skip     ← 1000
Step     ← 10
Times    ← ⍜(-Skip|⁅⍜(÷Step|⇡⌊))+1BadBlock

# paths - will ruthlessly spawn new threads until your CPU burns.
∵(×1_1_0)wait≡spawn(°⊚°□⊢path(Valid|≍End)0_0↙:Data)Times

¤∵(×0_0_1)⬚0+↯+1End0°⊚↙Skip Data           # old blocks
+:∵(×0_1_1)≡(⬚0+↯+1End0°⊚↘Skip↙)Times¤Data # add new blocks
≡+                                         # superimpose
⊂:⍜(⊡|⋅1_0_0) ⊡BadBlock Data⊣.             # Add frame for final block.
⍥(⊂:↙¯2.)10                                # Freeze frame.
≡(▽⟜≡▽4)                                   # Scale up.
&fwa "AOC2024day18.gif"gif 60
 

I've had a few comments on my Uiua solutions asking how you're even supposed to understand them, so I thought I'd write a little explainer.

Uiua is a new language that uses the array programming paradigm (other languages in this family are APL, J, K, R and BQN). This approach recognises that a great deal of programming is about the manipulation and interrogation of arrays of data and so provides tools to handle arrays of data as fundamental units. So rather than building nested for-loops to access data items, you manipulate the array as a whole, e.g. to add 1 to every element of a multi-dimensional array A, you would simply write +1A. This approach not only makes some aspects of programming easier, it also means that the compiler can generate extremely efficient code, and in principle make use of massively parallel processes for further speedups (I don't know to what extent Uiua supports this). Array programming languages are very useful for people who want fast processing of large amounts of multidimensional data, e.g. audio, video, scientific data, or financial data.

There are three factors that can make Uiua code hard to understand.

First, as we have already seen, Uiua is an array programming language, which not only mean that it uses a very different approach to problem solving, but that it also inherits that family of language's love of using glyphs for operator names rather than ascii names. Using the Uiua online editor and learning the documentation are the only ways to deal with this massive barrier :-(

Second, Uiua is stack based, so values are normally held on the stack rather than in named variables. This introduces some of the same challenges as writing in Factor, Forth etc, where you have to build up a mental model of what's on the stack at any time. There are variables available, but idiomatic code tends to avoid them as far as possible.

Third, function application is generally in mathematic order i.e. right-to-left. This can be complicated by some operators having different numbers of arguments which affect the binding order, but you learn to see through that…

Okay, so given all of that, how does one interpret some Uiua code? Let's work though my solution to day seven.

Data   ← ⊜(□⊜⋕⊸(¬∈": "))⊸≠@\n "190: 10 19\n3267: 81 40 27\n83: 17 5\n156: 15 6\n7290: 6 8 6 15\n161011: 16 10 13\n192: 17 8 14\n21037: 9 7 18 13\n292: 11 6 16 20"
Calib! ← ≡◇⊢▽⊸≡◇(∈♭/[^0]:°⊂) # Calibration targets which can be constructed from their values.
&p/+Calib!⊃(+|×)Data
&p/+Calib!⊃(+|×|+×ⁿ:10+1⌊ₙ₁₀,)Data

What the heck does it all mean?

Let’s find out! We'll go through it step by step.

Line 1

Data ← ⊜(□⊜⋕⊸(¬∈": "))⊸≠@\n "190: 10 19\n3267: 81 …. a string ….”

⊜(…)⊸≠@\n Can be read as partition () the string by building an array of sub-strings where each char is not \n (≠@\n) then perform the first function (i.e. the code in parentheses) on each sub-string in turn, concatenating each of the results into an array.

□⊜⋕⊸(¬∈": ") For each of these sub-strings, we immediately re-partition it by only keeping those characters that are not in string “: “, and then for each of these resulting (sub-sub-)strings, parse it as a number (). As each of the lines has a different number of entries, the outer partition would not be able to build a regular array, so we box () each line before passing it out to hide the contents away, allowing the outer array to be built successfully.

So at this point, Data has been defined as a constant looking something like [[190 10 19] [3267 81 40 27] …etc… ]

Line 2

Calib! ← ≡◇⊢▽⊸≡◇(∈♭/[^0]:°⊂)

Calib! is a ‘macro’, that is a function that takes a function as its first argument and then inserts it into its own body whenever it sees a ^0

▽⊸≡◇(…) Means only keep those lines of the array that meet a certain condition (▽⊸≡◇ reads as “keep by rows content” where 'content" means 'look inside the box').

∈♭/[^0]:°⊂ This is where the power of an array programming language really starts to show.

For each row of the input this is passed the numbers in that row as an array. First we remove the first entry (our target) and push it down the stack for later :°⊂.

Then we reduce (/) the rest of the array by repeatedly applying a function on the result so far and the next element. The function here is partially suppled by the macro substitution, so for part 1 this would be in full [⊃(+|×)] This means ’take the two arguments and fork (), or perform two functions on them, wrapping the results in an array [...].

Some magic happens here: Uiua supports ‘pervasive’ application of functions, so executing + 5 [[1 2] [3 4]] gives [[6 7] [8 9]]. For each succeeding entry in the list of numbers, we’re adding, and multiplying (and concatenating for part 2) it to every existing result, and storing these new results in a new dimension of the accumulated array.

Finally, we flatten () this monstrous array into a single list of numbers and check whether that target value is in this list (member ) . That true/false is passed out to the surrounding ‘keep’ functionality.

≡◇⊢ Now we have kept only the lines where the target can be calculated from the numbers. So all we have to do is pass back the ≡◇⊢ “rows contents first” i.e. the first number in each line.

Lines 3 and 4

&p/+Calib!⊃(+|×)Data
&p/+Calib!⊃(+|×|+×ⁿ:10+1⌊ₙ₁₀,)Data

Call the macro on the data array, with the two different sets of operators. /+ is reduce (/) by addition (i.e. sum the results).

+×ⁿ:10+1⌊ₙ₁₀, Takes a copy of the second number, get the floor of the base-10 log, adds 1, raises 10 to that power and multiplies the first number by that before adding the second number. This is an arithmetic concatenation, which works as a pervasive operator as discussed above. [N.B. I just couldn't get ⋕$"__" or similar string approaches to work in this context. If you know how to, please let me know!]

That's it. Problem seven dealt with in 72 tokens :-)

Next steps

If you want to learn more run the code, read the Uiua language tour, explore that website and documentation, or ask away here. I'm by no mean an expert, but I'm happy to help with the basics.

 

I created this group last year but soon found that there was a lot more active group on programming.dev with many more participants and even a private leaderboard, so you should probably head on over there!

Happy coding everyone!

 
 

Am I in danger?

 

Thanks Homer.

2
Day 1 solutions (adventofcode.com)
submitted 2 years ago* (last edited 2 years ago) by mykl@lemmy.world to c/adventofcode@lemmy.world
 

How was day one for everyone? I was surprised at how tricky it was to get the right answer for part two. Not the usual easy start I've seen in the past. I'd be interested to see any solutions here.

 

Hi All, I posted here recently that I was spending November revisiting my AOC 2022 solutions (written in Dart) and posting them for reading, running and editing online.

With my last solution being posted yesterday, the series is now complete, and might be interesting if anyone's looking for an idea of the level of difficulty involved in a typical year.

To ensure that this post isn't just about posts on other communities, I've added a little bonus content - a simple visualisation I created for my solution for day 14 (flowing sand).

 

As a warmup for this year's Advent of Code, I'm re-reading and posting my solutions to last year's challenges. You can read, run and edit today's solution by following the post link.

Today was the final challenge for the year, and as usual was quite simple and had only one part. This involved parsing and process numbers written in what I learned was called "balanced base five" notation.

Thanks for following along, now we just need to wait a few days to see what this year holds!

 

As a warmup for this year's Advent of Code, I'm re-reading and posting my solutions to last year's challenges. You can read, run and edit today's solution by following the post link.

Today had us running round a maze with moving obstacles. Treating time as a dimension allowed me to build a graph and find the shortest path. Part 2 just required doing this three times. This took me closer than I like to a full second runtime, but not close enough to make me re-think my solution.

view more: next ›