Uiua, forgot to put that in the post
Quant
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)
Uiua
Credits to @[email protected] 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 [email protected]
β‘(β(β/(β΄β[β(+|Γ)]))β‘0:Β°β)
Β°β‘/+β½
)
PartTwo β (
&rs β &fo "input-7.txt"
β(β‘ββΒ¬β": ".)β @\n.
ββ‘ββ’
β‘β(β/(β΄β[β‘ββ(+|Γ|β$"__")]):Β°β)
Β°β‘/+β½
)
&p "Day 7:"
&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
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
Glad to hear that my attempts at de-spaghettifying worked to some degree at least :D