Avatar

Quant

Quant@programming.dev
Joined
1 posts β€’ 26 comments
Direct message

Uiua

Decided to try and use Uiua for each day this year. At least I’m not the only one to get this idea ^^

Run with example input here

PartOne ← (
  &rs ∞ &fo "input-1.txt"
  ⊜(βŠœβ‹•β‰ @ .)β‰ @\n.
  ≑⍆⍉
  ⌡/-
  /+
)

PartTwo ← (
  &rs ∞ &fo "input-1.txt"
  ⊜(βŠœβ‹•β‰ @ .)β‰ @\n.
  βŠ’βŸœβŠ£β‰
  0
  ⍒(+βŠ™(:βŠ™(Γ—β§»βŠšβ—‘βŒ•)β†˜1⟜⊒)|β‹…(β‰ 0⧻))
  βŠ™(β—Œβ—Œ) # just cleaning up the stack
)

&p "Day 1:"
&pf "Part 1: "
&p PartOne
&pf "Part 2: "
&p PartTwo

permalink
report
reply

Uiua

Took me a bit longer to get this one but still quite simple overall.
Spent quite some time on getting to know the try and assert operators better.

Run with example input here

# Get the indices matching the ascending/
# descending criteria
CheckAsc ← β‰‘Β°β–‘βš(⍣(⊸⍀.≍⍆.)⍣(⊸⍀.β‰β‡Œβ†.)0)
# Get the indices matching the distance criteria
CheckDist ← β‰‘Β°β–‘βš(⍣(⊸⍀.β‰ 1∈:0)0Γ—βŠ“β‰₯≀1,3⌡⧈-)
Split     ← βŠ™(β–½β‰ 1)β–½,,

PartOne ← (
  &rs ∞ &fo "input-2.txt"
  ⊜(β–‘βŠœβ‹•β‰ @ .)β‰ @\n.
  CheckAsc.
  β–½
  CheckDist
  ⧻⊚
)

PartTwo ← (
  &rs ∞ &fo "input-2.txt"
  ⊜(β–‘βŠœβ‹•β‰ @ .)β‰ @\n.
  CheckAsc.
  Split
  CheckDist.
  Split
  βŠ™(βŠ‚)
  ⧻
  :
  ⍚(≑(β–½:°⊟)⍜€⊞⊟:β‰ 1⊞=.⇑⧻.)
  ≑(⧻⊚CheckDistβ–½CheckAsc.Β°β–‘)
  +β§»β—΄βŠš
)

&p "Day 2:"
&pf "Part 1: "
&p PartOne
&pf "Part 2: "
&p PartTwo
permalink
report
reply

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
permalink
report
reply

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
permalink
report
reply

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
permalink
report
reply

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
permalink
report
reply

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
permalink
report
reply

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)

permalink
report
parent
reply

Uiua, forgot to put that in the post

permalink
report
parent
reply

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

permalink
report
parent
reply