Quant
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
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
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
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
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
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
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
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)