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