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