Avatar

Quant

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

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

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

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

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

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

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