Quant

joined 2 weeks ago
[โ€“] Quant@programming.dev 1 points 4 days ago* (last edited 4 days ago)

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

CodeI 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

[โ€“] Quant@programming.dev 2 points 1 week ago (1 children)

Damn that's a lot time saved. I love how unassuming the addition looks for how great an effect it has

[โ€“] Quant@programming.dev 1 points 1 week ago* (last edited 1 week ago)

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

  1. Open the uiua pad with code here
  2. Replace the 0 in the first line with your solution for part two
  3. If necessary, change the name of the file containing your input
  4. Drag a file containing your input onto the pad to upload it and run the code
  5. 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:

[โ€“] Quant@programming.dev 2 points 1 week ago (1 children)

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~

[โ€“] Quant@programming.dev 2 points 1 week ago (3 children)

Nice :D
How's the speed now?

[โ€“] Quant@programming.dev 1 points 1 week ago

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

[โ€“] Quant@programming.dev 2 points 1 week ago (5 children)

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 ^^'

[โ€“] Quant@programming.dev 2 points 1 week ago

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
[โ€“] Quant@programming.dev 1 points 1 week ago* (last edited 1 week ago)

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
[โ€“] Quant@programming.dev 2 points 2 weeks ago

That's also possible, though I've had cases where putting a ? before a function changed the output to what I expected instead of doing something else.
This only happened in the online pad and seems to have been fixed by reloading the tab but I've taken to call any such behavior a bug now :D
Especially in this case because โ‹•"1019" should at least not throw a "invalid float literal" error.

Though if you ever find another explanation I'd be happy to read it ^^

(I'm going to 'investigate' some more because I had this error a few times but I don't remember the circumstances and solution anymore)

[โ€“] Quant@programming.dev 3 points 2 weeks ago (2 children)

Great explanation :D
I understand what I called black magic before now. I did wonder what something other than a concatenation would do in that place but didn't consider that it might be just that because it looked so complicated.

I wasn't able to get it running with โ‹•$"__" either, though I'd assume it's a bug caused by how the numbers get passed around or something. The day uiua has a stable release will be glorious

[โ€“] Quant@programming.dev 4 points 2 weeks ago

Uiua

Adapting the part one solution for part two took me longer than part one did today, but I didn't want to change much anymore.

I even got scolded by the interpreter to split the evaluating line onto multiple ones because it got too long.
Can't say it's pretty but it does it's job ^^'

Run with example input here

PartOne โ† (
  &rs โˆž &fo "input-8.txt"
  โŸœ(โ–ฝยฌโˆˆ".\n".โ—ด)
  โŠœโˆ˜โ‰ @\n.
  :ยคโŸœ(:ยค-1โ–ณ)
  โ‰ก(โ–กโŠšโŒ•)
  โ—ด/โ—‡โŠ‚โš(โ‰ก(-:โŸœ-ยฐโŠŸ)โง…โ‰ 2)
  โงปโ–ฝยฌ:โŠ™(/+โ‰+)โŸœโŠ“><,0
)

PartTwo โ† (
  &rs โˆž &fo "input-8.txt"
  โŸœ(โ–ฝยฌโˆˆ".\n".โ—ดโŸœยค
    โ–ฝ:โŸœโ‰ก(>1โงปโŠšโŒ•)
  )
  โŠœโˆ˜โ‰ @\n.
  :ยคโŸœ(:ยค-1โ–ณ)
  โ‰ก(โ–กโŠšโŒ•)
  โŠธโš(
    โง…โ‰ 2โŠ™ยค
    โ‰ก(:ยคโŸœ-ยฐโŠŸ
      โข(โŠ™โŠ‚โŸœ-โŠ™โŠธโŠข
      | โ‹…(=0/++โŠ“><,0โŠข))
      โ–กโŠ™โ—Œโ—Œ
    )
  )
  โ—ด/โ—‡โŠ‚/โ—‡โŠ‚
  โงปโ–ฝยฌ:โŠ™(/+โ‰+)โŸœโŠ“><,0
)

&p "Day 8:"
&pf "Part 1: "
&p PartOne
&pf "Part 2: "
&p PartTwo
47
submitted 2 weeks ago* (last edited 2 weeks ago) by Quant@programming.dev to c/advent_of_code@programming.dev
 

I don't remember exactly what this difference caused but I do remember it being very annoying to debug.

Edit: the language used is Uiua

view more: next โ€บ