Quant
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
Ok, so part one wasn’t too hard, and since uiua also takes negative values for accessing arrays, I didn’t even have to care about converting my modulus results (though I did later for part two).
I’m a bit conflicted about the way I detected the quadrants the robots are in, or rather the way the creation of the mask-array happens. I basically made a 11x7 field of 0’s, then picked out each quadrant and added 1-4 respectively. Uiua’s group (⊕
) function then takes care of putting all the robots in separate arrays for each quadrant. Simple.
For part two, I didn’t even think long before I came here to see other’s approaches. The idea to look for the first occurrence where no robots’ positions overlapped was my starting point for what follows.
Example input stuff
Run with example input here
$ p=0,4 v=3,-3
$ p=6,3 v=-1,-3
$ p=10,3 v=-1,2
$ p=2,0 v=2,-1
$ p=0,0 v=1,3
$ p=3,0 v=-2,-2
$ p=7,6 v=-1,-3
$ p=3,0 v=-1,-2
$ p=9,3 v=2,3
$ p=7,3 v=-1,2
$ p=2,4 v=2,-3
$ p=9,5 v=-3,-3
.
PartOne ← (
# &rs ∞ &fo "input-14.txt"
⊜(↯2_2⋕regex"-?\\d+")≠@\n.
≡(⍜⌵(◿11_7)+°⊟⍜⊡₁×₁₀₀)
↯⟜(▽×°⊟)7_11 0
⍜↙₃(⍜≡↙₅+₁⍜≡↘₆+₂)
⍜↘₄(⍜≡↙₅+₃⍜≡↘₆+₄)
/×≡◇⧻⊕□-₁⊸(⊡:)⍉
)
PartTwo ← (
# &rs ∞ &fo "input-14.txt"
⊜(↯2_2⋕regex"-?\\d+")≠@\n.
0 # number of seconds to start at
0_0
⍢(◡(≡(⍜⌵(◿11_7)+°⊟⍜⊡₁×):)◌
◿[11_7]≡+[11_7]
⊙+₁
| ≠⊙(⧻◴)⧻.)
⊙◌◌
-₁
)
&p "Day 14:"
&pf "Part 1: "
&p PartOne
&pf "Part 2: "
&p PartTwo
Now on to the more layered approach of how I got my solution.
In my case, there’s two occasions of non-overlapping positions before the christmas tree appears.
I had some fun trying to get those frames and kept messing up with going back and forth between 7x11 vs 103x101 fields, often forgetting to adjust the modulus and other parts, so that was great.
In the end, I uploaded my input to the online uiua pad to make visualizing possible frames easier since uiua is able to output media if the arrays match a defined format.
Try it out yourself with your input
- Open the uiua pad with code here
- Replace the
0
in the first line with your solution for part two - If necessary, change the name of the file containing your input
- Drag a file containing your input onto the pad to upload it and run the code
- An image should be displayed
I used this code to find the occurrence of non-overlapping positions (running this locally):
&rs ∞ &fo "input-14.txt"
⊜(↯2_2⋕regex"-?\\d+")≠@\n.
0 # number of seconds to start at
0_0
⍢(◡(≡(⍜⌵(◿101_103)+°⊟⍜⊡₁×):)◌
◿[101_103]≡+[101_103]
⊙+₁
| ≠⊙(⧻◴)⧻.)
⊙◌◌
-₁
Whenever a new case was found, I put the result into the code in the online pad to check the generated image, and finally got this at the third try:
Counting the number of corners was a very useful hint for part 2. I had the most trouble with detecting the double corners, i.e. like in the example where the two B fields touch diagonally:
AAAAAA
AAABBA
AAABBA
ABBAAA
ABBAAA
AAAAAA
Still, I would’ve taken a lot longer and probably made really-bad-performance-code without reading this :D
Uiua
After finally deciding to put aside Day 9 Part 2 for now, this was really easy actually. The longest was figuring out how many extra dimensions I had to give some arrays and where to remove those again (and how). Then part 2 came along and all I had to do was remove a single character (not removing duplicates when landing on the same field by going different ways from the same starting point). Basically, everything in the parentheses of the Trails!
macro was my solution for part 1, just that the ^0
was ◴
(deduplicate). Once that was removed, the solution for part 2 was there as well.
Run with example input here
Note: in order to use the code here for the actual input, you have to replace =₈
with =₅₀
because I was too lazy to make it work with variable array sizes this time.
$ 89010123
$ 78121874
$ 87430965
$ 96549874
$ 45678903
$ 32019012
$ 01329801
$ 10456732
.
Adj ← ¤[0_¯1 0_1 ¯1_0 1_0]
Trails! ← (
⊚=0.
⊙¤
≡(□¤)
1
⍥(⊙(≡(□^0/⊂≡(+¤)⊙¤°□)⊙Adj
≡(□▽¬≡/++⊃=₋₁=₈.°□))
+1⟜⊸⍚(▽=⊙(:⟜⊡))
)9
⊙◌◌
⧻/◇⊂
)
PartOne ← (
# &rs ∞ &fo "input-10.txt"
⊜∵⋕≠@\n.
Trails!◴
)
PartTwo ← (
# &rs ∞ &fo "input-10.txt"
⊜∵⋕≠@\n.
Trails!∘
)
&p "Day 10:"
&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