Quant

joined 3 weeks ago
[–] Quant@programming.dev 5 points 2 weeks ago

A solution in malbolge would be amazing and also kinda terrifying

[–] Quant@programming.dev 3 points 2 weeks ago

You can actually use (singular) emojis as variable names. According to the documentation, they won't be be used as glyphs so you don't even have to worry about breaking changes of that kind :D

[–] Quant@programming.dev 2 points 2 weeks ago

Hell yeah!

┻━┻︡ (Β°β–‘Β°)/ οΈ΅ ┻━┻

[–] Quant@programming.dev 2 points 2 weeks ago

No worries, it does seem a lot less difficult in hindsight now, my mind just blanked at what I expected to be a lot more code :))

That performance improvement is amazing, I'll definitely take a look at how that works in detail later. Just gotta recover from the mental stretch gymnastics trying to remember the state of the stack at different code positions

[–] Quant@programming.dev 5 points 2 weeks ago

Glad to hear that my attempts at de-spaghettifying worked to some degree at least :D

[–] Quant@programming.dev 3 points 2 weeks ago

Uiua, forgot to put that in the post

[–] Quant@programming.dev 3 points 2 weeks ago (2 children)

Thanks to your solution I learned more about how to use reduce :D

My solution did work for the example input but not for the actual one. When I went here and saw this tiny code block and you saying

This turned out to be reasonably easy

I was quite taken aback. And it's so much better performance-wise too :D (well, until part 2 comes along in my case. Whatever this black magic is you used there is too high for my fried brain atm)

[–] Quant@programming.dev 3 points 2 weeks ago (2 children)

Uiua

Credits to @mykl@lemmy.world for the approach of using reduce and also how to split the input by multiple characters.
I can happily say that I learned quite a bit today, even though the first part made me frustrated enough that I went searching for other approaches ^^

Part two just needed a simple modification. Changing how the input is parsed and passed to the adapted function took longer than changing the function itself actually.

Run with example input here

PartOne ← (
  &rs ∞ &fo "input-7.txt"
  βŠœβ–‘β‰ @\n.
  ≑◇(βŠœβ–‘β‰ @:.)
  β‰‘βœβŠ‘β‹•0
  β‰‘βœ(Β°β–‘βŠ‘1)(βŠœβ‹•β‰ @ .)
  ⟜(⊑0⍉)

  # own attempt, produces a too low number
  # ≑(:βˆ©Β°β–‘Β°βŠŸ
  #   ⍣(⍀.◑⍣(1⍀.(≀/Γ—)⍀.(β‰₯/+),,)0
  #     βŠ™Β€β‹―β‡‘βΏ:2-1⊸⧻
  #     ⊞(β₯(⟜⍜(βŠ™(↙2))(⨬+Γ—βŠ™Β°βŠŸβŠ‘0)
  #         β†˜1
  #       )⧻.
  #       ⍀.=0⧻.
  #     )
  #     βˆˆβ™­β—Œ
  #   )0)

  # reduce approach found on the programming.dev AoC community by mykl@lemmy.world
  ≑(β—‡(∈/(β—΄β™­[βŠƒ(+|Γ—)]))⊑0:Β°βŠ‚)
  Β°β–‘/+β–½
)

PartTwo ← (
  &rs ∞ &fo "input-7.txt"
  ⊜(β–‘βŠœβ‹•Β¬βˆˆ": ".)β‰ @\n.
  βŸœβ‰‘β—‡βŠ’
  ≑◇(∈/(β—΄β™­[β‰‘βŠƒβŠƒ(+|Γ—|β‹•$"__")]):Β°βŠ‚)
  Β°β–‘/+β–½
)

&p "Day 7:"
&pf "Part 1: "
&p PartOne
&pf "Part 2: "
&p PartTwo
[–] Quant@programming.dev 1 points 2 weeks ago

Uiua

Part one was simple enough. Part two nearly made me give up.
Part two has the most ugly and least performant code I've made in uiua so far but it gets the job done and that's all I care about for now.

Run with example input here

RotateClock ← (
  βŠ™βŠ™(β‰β‡Œ)
  βŠ™(β‡Œβœ(⊑0)(-βŠ™(⧻⊑0.)+1))
  ↻¯1
)

RotateCounter ← (
  βŠ™βŠ™(β‡Œβ‰)
  βŠ™(⍜(⊑0)(-βŠ™(⧻.)+1)β‡Œ)
  ↻1
)

NewPos ← (
  βŠ™βœ(βŠ™βŠ‘:)(-1+βŠ™(βŠ—@#)βŸœβ†˜βŠ™.)⟜°⊟
  ⍜(⊑1)β‹…
)

MarkPath ← (
  RotateClock
  ⍒( # replace characters up til next '#'
    βŠ™(βŠ™βœ(β†˜βŠ™βŠ‘:)(⍜(↙)(β–½:@^⧻)βŠ—@#.)⟜°⊟
      NewPos
    )
    RotateCounter
  | β‹…(β‰ 0⊑0))
  β—Œβ—Œ
)

PartOne ← (
  &rs ∞ &fo "input-6.txt"
  βŠœβˆ˜β‰ @\n.
  # maybe make compatible with
  # non-up facing inputs
  β™­βŠš=@^.
  [0 1 2 3]
  MarkPath
  &fwa "test.txt" json.
  /+/+=@^
)

PartTwo ← (
  &rs ∞ &fo "input-6.txt"
  βŠœβˆ˜β‰ @\n.
  # maybe make compatible with
  # non-up facing inputs
  β™­βŠš=@^.
  [0 1 2 3]
  β—‘MarkPath
  βŠ™::
  # rotate the field to match the intital state
  βŠ™βŠ™(
    βŠ™(⊚=@#)
    ⍒(β‡Œβ‰|Β¬β‰βŠš=@#)
    βŠ™β—Œ
  )
  βŠ™βŠ™(⊚=@^.)
  βŠ™βŠ™βŠ™Β€βˆ©Β€
  ⊞(βŠ™βŠ™(βœβŠ‘β‹…@#)
    RotateClock
    βŠ™NewPos
    €¯1_¯1_¯1
    ⍒(βŠ™β—‘(βŠ‚βŠ’)
      βŠ‚
      βŠ™(RotateCounter
        βŠ™NewPos
      )
    | =1+βŠ™(βˆˆβ†˜1β‡Œ)β—‘β‹…(β‰ 129⊑2)βŠ™(βŠ‚βŠ’))
    # 129 = length of input array. Hardcoded because
    # the condition block doesn't seem to get the
    # input array passed to it so the length can't
    # be read dynamically
    βŠ™(βŠ‚βŠ’)
    ∈
    βŠ™β—Œ
  )
  /+β™­
)

&p "Day 6:"
&pf "Part 1: "
&p PartOne
&pf "Part 2: "
&p PartTwo
[–] Quant@programming.dev 1 points 2 weeks ago

Uiua

This is the first one that caused me some headache because I didn't read the instructions carefully enough.
I kept trying to create a sorted list for when all available pages were used, which got me stuck in an endless loop.

Another fun part was figuring out to use memberof (∈) instead of find (βŒ•) in the last line of FindNext. So much time spent on debugging other areas of the code

Run with example input here

FindNext ← βŠ™(
  ⊑1⍉,
  βŠƒβ–½(β–½Β¬)⊸∈
  βŠ™βŠ™(⊑0⍉.)
  :βŠ™(⟜(β–½Β¬βˆˆ))
)

# find the order of pages for a given set of rules
FindOrder ← (
  β—΄β™­.
  []
  ⍒(βŠ‚FindNext|β‹…(>1⧻))
  βŠ™β—ŒβŠ‚
)

PartOne ← (
  &rs ∞ &fo "input-5.txt"
  βˆ©Β°β–‘Β°βŠŸβŠœβ–‘Β¬βŒ•"\n\n".
  βŠ™(⊜(β–‘βŠœβ‹•β‰ @,.)β‰ @\n.β†˜1)
  ⊜(βŠœβ‹•β‰ @|.)β‰ @\n.

  βŠ™.
  Β€
  ⊞(β—‘(Β°β–‘:)
    ⟜:βŠ™(Β°βŠŸβ‰)
    =2+∩∈
    β–½
    FindOrder
    βŠΈβ‰Β°β–‘:
    βŠ™β—Œ
  )
  ≑◇(⊑⌊÷2⧻.)β–½β™­
  /+
)

PartTwo ← (
  &rs ∞ &fo "input-5.txt"
  βˆ©Β°β–‘Β°βŠŸβŠœβ–‘Β¬βŒ•"\n\n".
  βŠ™(⊜(β–‘βŠœβ‹•β‰ @,.)β‰ @\n.β†˜1)
  ⊜(βŠœβ‹•β‰ @|.)β‰ @\n.
  βŠ™.
  ⍜€⊞(
    β—‘(Β°β–‘:)
    ⟜:βŠ™(Β°βŠŸβ‰)
    =2+∩∈
    β–½
    FindOrder
    βŠΈβ‰Β°β–‘:
    βŠŸβˆ©β–‘
  )
  βŠ™β—Œ
  βŠƒ(⊑0)(⊑1)⍉
  ≑◇(⊑⌊÷2⧻.)▽¬≑°░
  /+
)

&p "Day 5:"
&pf "Part 1: "
&p PartOne
&pf "Part 2: "
&p PartTwo
[–] Quant@programming.dev 2 points 2 weeks ago

Uiua

This one was nice. The second part seemed quite daunting at first but wasn't actually that hard in the end.

Run with example input here

Row    ← βŒ• "XMAS"
RevRow ← βŒ•"SAMX"
Sum    ← /+/+
Count  ← +∩SumβŠƒRow RevRow

PartOne ← (
  &rs ∞ &fo "input-4.txt"
  βŠœβˆ˜β‰ @\n.
  βŠ™+⟜∩CountβŸœβ‰ # horizontal and vertical search
  ⟜(/+⧈(Countβ‰β‰‘β¬š@ ↻⇑⧻.)4)
  /+⧈(Countβ‰β‰‘β¬š@ ↻¯⇑⧻.)4
  ++
)

Mask ← Β°βŠšΓ—2⇑5
# Create variations of X-MAS
Vars ← (
  ["M S"
   " A "
   "M S"]
  ≑♭[βˆ©βŸœβ‰]β‰‘β‡Œ.
  Mask
  ⊏0βŠžβ–½Β€
)

PartTwo ← (
  &rs ∞ &fo "input-4.txt"
  βŠœβˆ˜β‰ @\n.
  ⧈(/+β™­βŠžβ‰βŠ™Β€Varsβ–½Maskβ™­)3_3
  Sum
)

&p "Day 4:"
&pf "Part 1: "
&p PartOne
&pf "Part 2: "
&p PartTwo
[–] Quant@programming.dev 1 points 2 weeks ago

Uiua

Regex my beloved <3

Run with example input here

FindMul ← regex "mul\\((\\d+),(\\d+)\\)"

PartOne ← (
  &rs ∞ &fo "input-3.txt"
  FindMul
  /+≑(Γ—Β°βŠŸβ‹•βŠ1_2)
)

IdDont ← βŠ—β–‘"don't()"β™­

PartTwo ← (
  &rs ∞ &fo "input-3.txt"
  regex "mul\\(\\d+,\\d+\\)|do\\(\\)|don't\\(\\)"
  ⍒(IdDont.
    β†˜1βŠƒβ†˜β†™
    βŠ—β–‘"do()"β™­.
    βŠ‚β†˜1β†˜
  | IdDont.
    ≠⧻,
  )
  β–½β™­=0βŒ•β–‘"do()".
  ≑(Γ—Β°βŠŸβ‹•βŠ1_2β™­FindMul)β™­
  /+
)

&p "Day 3:"
&pf "Part 1: "
&p PartOne
&pf "Part 2: "
&p PartTwo
view more: β€Ή prev next β€Ί