Avatar

Quant

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

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

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

Put this one off for a bit and I’ll put off part two for even longer because I don’t want to deal with any pyramid-shapes of boxes at the moment.
The code for part one feels too long but it works and runs <2s so I’m happy with it for now ^^

Run with example input here

Code

I decided to split the movement instructions lines further for aesthetic reasons when opening it in the online uiua pad since newlines are thrown out anyways.

$ ##########
$ #..O..O.O#
$ #......O.#
$ #.OO..O.O#
$ #..O@..O.#
$ #O#..O...#
$ #O..O..O.#
$ #.OO.O.OO#
$ #....O...#
$ ##########
$ 
$ <vv>^<v^>v>^vv^v>v<>v^v<v<^vv<<<^><
$ <><>>v<vvv<>^v^>^<<<><<v<<<v^vv^v>^
$ vvv<<^>^v^^><<>>><>^<<><^vv^^<>vvv<
$ >><^^v>^>vv<>v<<<<v<^v>^<^^>>>^<v<v
$ ><>vv>v^v^<>><>>>><^^>vv>v<^^^>>v^v
$ ^<^^>v^^>v^<^v>v<>>v^v^<v>v^^<^^vv<
$ <<v<^>>^^^^>>>v^<>vvv^><v<<<>^^^vv^
$ <vvv>^>v<^^^^v<>^>vvvv><>>v^<<^^^^^
$ ^><^><>>><>^^<<^^v>>><^<v>^<vv>>v>>
$ >^v><>^v><<<<v>>v<v<v>vvv>^<><<>^><
$ ^>><>^v<><^vvv<^^<><v<<<<<><^v<<<><
$ <<^^<v<^^^><^>>^<v^><<<^>>^v<v^v<v^
$ >^>>^v>vv>^<<^v<>><<><<v<<v><>v<^vv
$ <<<>^^v^>^^>>><<^v>>v^v><^^>>^<>vv^
$ <><^^>^^^<><vvvvv^v<v<<>^v<v>v<<^><
$ <><<><<<^^<<<^<<>><<><^^^>^^<>^>v<>
$ ^^>vv<^v^v<vv>^<><v<^v>^^^>>>^^vvv^
$ >vvv<>>>^<^>>>>>^<<^v>^vvv<>^<><<v>
$ v^^>>><<^^<>>^v^<v^vv<>v^<<>^<^v^v>
$ <^<<<><<^<v><v<>vv>>v><v^<vv<>v^<<^

Pos ← :Β°βŠŸβ™­βŠšβŠΈβŒ•@@
Move ← (
  βŠΈβŠ—@.
  ⍣(+₁⍀.β—‘(β‰ βŠ™β§»)
    βœβ†™β£(⬚@.↻₋₁⍀.=βŠƒ(⧻|βŠ—@#).)∘
  )β‹…βˆ˜
)

PartOne ← (
  # &rs ∞ &fo "input-15.txt"
  βŠœβ–‘Β¬β¦·"\n\n".
  βˆ©Β°β–‘Β°βŠŸ
  : βŠ™(β–½β‰ @\n.) βŠœβˆ˜β‰ @\n.
  β†˜β‚„βŠ›βŠ‚"^>v<"
  ⍒(βŠƒβ†˜β‚βŠ’
    βŠ™(⟜(⨬(⍉|∘)β—Ώβ‚‚)
      ⟜(⨬(⍜(β†˜βŠ™βŠ‘)Move Pos
        | ⍜(β‡Œβ†™βŠ™βŠ‘)Move +₁Pos
        )=₀◿₃)
      ⨬(⍉|∘)β—Ώβ‚‚
    )
  | β‰ 0⧻)
  β—Œ
  /+≑/+Γ—[100_1]βŠšβŒ•@O
)

PartTwo ← (
  ""
)

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

Hell yeah!

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

permalink
report
parent
reply

Damn that’s a lot time saved. I love how unassuming the addition looks for how great an effect it has

permalink
report
parent
reply

Welp, got frustrated again with part one because there kept being something wrong with my totally-not-ugly loop and so came here again. I did have to change IsInt (and thus also Cost to account for different handling) for part two though because I kept getting wrong results for my input.
I’m guessing it’s because uiua didn’t see the difference between rounded and non-rounded number anymore.

Here’s the updated, slightly messier version of the two functions that worked out for me in the end :D

IsInt ← β‰Β°βŠŸβ‰βœ(βŠ™(⍉≑↙₂))(/+Γ—)βŠ™β‰β…
Cost  ← /+Γ—3_1Γ—βŸœIsInt⊸AB

Could have been done better but I’m lacking the patience for that now

permalink
report
parent
reply

Nice :D
How’s the speed now?

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

I found multidimensional markers for partition to work really well for finding the fields: Areas ← βŠœβ–‘:⇑△.+1βœβ™­βŠ› It just groups the other array’s contents according to adjacent markers, horizontally and vertically. Took me quite a bit to figure out what’s actually happening in the example in the documentation ^^’

permalink
report
parent
reply

Uiua

I spent a while thinking about how to best do a flood fill in Uiua when I saw that ⊜ (partition) works beautifully with multidimensional markers: β€œGroups are formed from markers that are adjacent along any axis.”, meaning I just had to convert all letters into numbers and I’d get all indices belonging to a field into an array.
For part 2, I cheated a bit by coming here and reading that you only need to count the edges. To my surprise, the second part is actually a bit faster than part 1. Takes less than 0.2 seconds each though :D

Run with example input here

$ RRRRIICCFF
$ RRRRIICCCF
$ VVRRRCCFFF
$ VVRCCCJFFF
$ VVVVCJJCFE
$ VVIVCCJJEE
$ VVIIICJJEE
$ MIIIIIJJEE
$ MIIISIJEEE
$ MMMISSJEEE
.
N     ← +[0_Β―1 0_1 Β―1_0 1_0]
Areas ← βŠœβ–‘:⇑△.+1βœβ™­βŠ›
Peri  ← -/+≑(/+∊NΒ€)⟜€⟜(Γ—4⧻)
Sides ← (
  βŠ™(-Β€)β†―:β–½βŠ™0Γ—Β°βŠŸ.+2⌡⊸-+1βŠƒβŠ£βŠ’βŠΈβœβ‰β‰‘β†
  ⧻⊚⊸∊1_3⧈(/+/+)2_2.⍜⊑=β‚€+1:
  +βŠ™(Γ—2/+/+⧈(∊[[1_0 0_1][0_1 1_0]])2_2β—Œ)
)
Cost! ← /+≑◇(Γ—^0⟜⧻)

PartOne ← (
  # &rs ∞ &fo "input-12.txt"
  βŠœβˆ˜β‰ @\n.
  Cost!Peri Areas
)

PartTwo ← (
  # &rs ∞ &fo "input-12.txt"
  βŠœβˆ˜β‰ @\n.
  Cost!Sides Areas
)

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