Unable to load image

rDrama Advent of Code Day 10: Pipe ( | ) edition

Summary for those just joining us:

Advent of Code is an annual Christmas themed coding challenge that runs from December 1st until christmas. Each day the coding problems get progressively harder. We have a leaderboard and pretty good turnout, so feel free to hop in at any time and show your stuff!

Whether you have a single line monstrosity or a beautiful phone book sized stack of OOP code, you can export it in a nice little image for sharing at https://carbon.vercel.app

What did you think about today's problem?

https://adventofcode.com/2023

Our Code is 2416137-393b284c (No need to share your profile, you have the option to join anonymously if you don't want us to see your github)

15
Jump in the discussion.

No email address required.

i unfortunately missed this ine because i was getting drunk with my friends... oopsies. anyways still super ineberated and havent attwmpted it, i rlly ahte how this is scored because u have to be a nowlifer to get on the leaderboard :/

!codecels join the AoC hate train :marseytrain:

Jump in the discussion.

No email address required.

wasted an ungodly amount of time reading stack traces with printed ascii grids and debugging because I zoned out and implemented "north, south, east, west" in one place as "up, down, left, right"

shan't doing part two, as I will be taking my own life, thank you for your understanding

:#marseybudddwyer:

Jump in the discussion.

No email address required.

I thought I had a solution to part 2 by just checking every cell and traversing until I hit an edge, but I didn't read the part that squeezing between pipes also counts and now I want to kill my elf

Jump in the discussion.

No email address required.

Yep I have my traversal part 2 done and losing the will to continue

Jump in the discussion.

No email address required.

:marseysweating: :marseysweating: :marseysweating:

Ended up using pick's Theorem.

Jump in the discussion.

No email address required.

Can you explain how? I saw a bunch of reddit people saying the same thing and I don't understand how that works since you'd need to know the area of the interior to calculate the result. Unless I just don't understand the theorem which is also likely.

Jump in the discussion.

No email address required.

Use a Depth First Search, one item at a time, to get the entire pipe in order. Then use the shoelace formula to get the Area. From picks theorem,

A= i +b/2

where A is the area, i is the number of interior points (the answer to the puzzle) and b is the number of boundary points (the length of the pipe).

The 2nd illustration on the Wikipedia page is very useful. https://en.wikipedia.org/wiki/Pick%27s_theorem

Jump in the discussion.

No email address required.

prpbably spomething to do with the difference of the coordinates

Jump in the discussion.

No email address required.

That doesn't help even a little bit, but thanks I guess

Jump in the discussion.

No email address required.

>finish puzzle

>look outside

>surprised the sun is up

Time flies when you're having fun :marseywholesome:

m←↑⊃⎕NGET'10.in'1 ⋄ s←⍸'S'=m
u←'|F7S' '-J7S' '|JLS' '-FLS' ⋄ v←u⌷⍨⊂3 4 1 2
j←(¯1 0)(0 1)(1 0)(0 ¯1) ⋄ k←(1 2)(2 3)(3 2)(2 1)
t←{⊂⍵}⌺3 3⊢m ⋄ w←(m∘∊¨v)∧¨u{∨/¨⍺∘∊¨(⊂⍵)∘⊃¨t}¨k
x←{l←1⊃⍵ ⋄ b←1↓2↑⍵ ⋄ ,∘⍵¨⊂¨b~⍨(⊂l)∘.+j/⍨l∘⌷¨w}
f←{⊃p⌷⍨1⍳⍨∧/¨s=⊃¨p←{⊃¨x¨⍵}⍣{s∊⊃¨⍺}⊣x s}
⎕←⌊2÷⍨≢p←f s

n←((¯1 0)(0 ¯1)) ((¯1 0)(0 1)) ((0 1)(1 0)) ((0 ¯1)(1 0)) ((0 ¯1)(0 1)) ((¯1 0)(1 0))
m←'.'@(c/⍨~p∊⍨c←,⍳⍴m)⊢('F7JL-|'⌷⍨n⍳⊂(⊂∘⍋⌷⊣)s-⊃¨(1∘↑,⍥⊂¯1∘↑)¯1↓1↓p)@s⊢m
⎕←+/∊('.'=m)∧{2|≢¨'L7|FJ'⎕R'|'¨'F7|LJ'⎕R''¨~∘'.-'¨,\⍵}⍣1⊢m
Jump in the discussion.

No email address required.

My part 1 was broken and couldn't figure it out, so i copy pasted like 10 solutions from the reddit solution thread that all gave me a wrong solution. And eventually I found the issue myself.

At the end the issue was that my F, moved exactly like the L. Otherwise it is just a simple dfs(and hoping that the longest contiguous part is the loop. But it would be easy to check if you reached S again. Also surrounded my entire pipe maze with one '.' in every direction, to not have to do bounds checking.

https://i.rdrama.net/images/17022025516693356.webp

Part 2, whenever I find time later

Jump in the discussion.

No email address required.

https://i.rdrama.net/images/17022166884442434.webp

Okay here is part 2. The only issue right now is that I have manually replace the 'S' with the appropriate symbol for part 2.

Actually was not that hard, but struggled a bit getting the logic of the raycasting right.

Basically to find out if a point is inside the loop polygon, you need traverse the row up to that point and count how many pipes were traversed.

| counts as one pipe break

L followed by a 7 counts as one break (ofc consider any amount of - inbetween)

F followed by J counts as one break

F followed by 7 and L followed by J don't count.

The point is then in the polygon if breaks % 2 == 1.

Jump in the discussion.

No email address required.

I hope writing my correct solution inside the debug block for my incorrect one makes my disdain for geometrystrags clear

https://i.rdrama.net/images/17022006498326552.webp

Jump in the discussion.

No email address required.

>hashmap

hmm

i really gotta learn about those

I keep hearing about them

yet I continue to ignore them

Jump in the discussion.

No email address required.

I think it's just a dict for people who've done a DSA class

Jump in the discussion.

No email address required.

Part 1 was straight forward path finding, just annoying to keep track of indexes. I need to take a break and think about part 2 https://i.rdrama.net/images/1702191758939138.webp

Jump in the discussion.

No email address required.

Kinda crazy to keep track of a max value when you can just find the length of the loop by traversing it once and divide by two

Jump in the discussion.

No email address required.

Thanks, that's a good trick. This is my first time doing coding puzzles so I'm suprised I'm keeping up tbh

Jump in the discussion.

No email address required.

I like seeing strange approaches to problems. It adds one more thing to the toolkit that might be needed later

Jump in the discussion.

No email address required.

It's simpler than path finding in that there is only ever one direction you can go, originally I thought I'd have to pull out A* and was relieved that it wasn't that kind of problem.

Jump in the discussion.

No email address required.

I think I went this way because you don't know the shape of S, but yeah, I could probably combine the neighbour finding and path building steps if I was smart about it

Jump in the discussion.

No email address required.

Part 2 (algo from SO) and cleaned up because doing it with flat arrays was stupid

https://i.rdrama.net/images/17022119685309224.webp

Jump in the discussion.

No email address required.

I had to do a Point In Polygon algorithm for computer vision class once and frankly I don't like it and just looked it up again. Rederiving algorithms is for suckers when half the pythoncels are just importing and using it directly.

Probably should clean it up and write a more general function for up, down, left, right movement but I don't care it's sunday and nice out I'm going outside.

https://i.rdrama.net/images/17021914193678105.webp

Jump in the discussion.

No email address required.

Good morning, I hate grids

:#marseygoodnight:

https://i.rdrama.net/images/17021886852735913.webp

Jump in the discussion.

No email address required.

I wasted maybe 8 hours on this today before starting from scratch and solving it in 10 minutes. My main mistakes:

1. Not appreciating that there's only one path to take out of each pipe.

2. Trying to do BFS for part 2 to fill the middle, writing some horrendous code for the tunnel parts.

I got up to 170 lines of code, realised "something's not right here" and cheated a little by reading a help thread on the subreddit. Once I understood the idea that you're just looking for the area inside a shape then it was fine. I used shapely which definitely felt like cheating but I just don't care.

Jump in the discussion.

No email address required.

I probably won't continue if the difficulty keeps ramping up. I have too much work to spend that much time on it every day, plus Nim doesn't have a great library ecosystem to solve these problems.

Part 1 was alright:

https://i.rdrama.net/images/17022220716401334.webp

Part 2 was an abomination, half nim, half python, half Gimp.

1. Found the path in Nim and create a new input file with all non-path tiles zeroed out.

2. Used a python script to draw an image upscaled 3 times (140x3, 140x3), where each 3x3 pixels corresponds to a pipe pattern or a 3x3 red dot for non-pipes.

3. In Gimp bucket fill the outer wall, bucket fill the inner wall , select all pixels in inner wall, remove holes in selection (captures the non-pipes pixels), subtract a selection of the inner wall.

4. Read number of pixels in selection and divide by 9.

https://i.rdrama.net/images/17022259244243233.webp

Prettier colours:

https://i.rdrama.net/images/17022309479630516.webp

Jump in the discussion.

No email address required.

https://i.rdrama.net/images/1702237146626357.webp

  • Operator overloading: Like most other languages, but with slightly better syntax. In addition, there are 19 different operator characters you can use to create arbitrary operators.

  • Stropping: Keywords like proc can be used as identifier names by enclosing them in backticks.

  • set vs std/sets: set is for ordinal types with <=2^16 possible values, because "sets are implemented as high performance bit vectors."

Jump in the discussion.

No email address required.

part 2 was written by the devil.

:#marseypentagram:

Jump in the discussion.

No email address required.

:marseyretard3:

I feel like Yandev with this terrible code, but what I did was trace along the path while marking every orthogonal unoccupied square on the grid with either an L or R depending on whether it's on my relative left or right while I travel along the loop. Then I make the Ls and Rs spread out along orthogonal squares using BFS until there's no more unoccupied squares. From there, I can tell where L or R is the enclosed area because the other one will be touching the edge of the grid, and I can just count them to get the answer.

https://i.rdrama.net/images/17022475056415212.webp

Jump in the discussion.

No email address required.

Part 2 was a pain in the arse today, but there are two ways I could imagine solving it:

1) Double the resolution of the input grid, so that the "squeezing" behaviour becomes visible and allows you to flood fill.

2) Consider the path as a polygon and each pixel as a point, then check which points are in the polygon. If you're on Python shapely is a fairly good library for this.

I went for the latter, which is a bit slow but far less memory intensive.

Jump in the discussion.

No email address required.

Part 1 was really easy, so I was kind of figuring part 2 was going to throw a curveball. I did not expect it to be that much of a b-word to figure out on day 10 though. I also eventually did a polygon type solution, but if I could go back in time, I think I would have done the flood fill you mentioned instead. I probably could have saved myself a couple frustrating hours.

Jump in the discussion.

No email address required.

frick, my part 2 works only with the first example, not the next example where it's doubly enclosed. gonna kms later, bye girls.

Jump in the discussion.

No email address required.

nvm I got it :marseyparty:

Jump in the discussion.

No email address required.

I did not enjoy trying to figure out what constitutes an edge or a vertex in Part 2. This was a clever way on the puzzle designer's part to turn something that should have been relatively simple into a total god darn nightmare.

Code for both parts:

https://i.rdrama.net/images/17022370492416968.webp

Jump in the discussion.

No email address required.

Your sponsor code doesn't work :marseycry:

Jump in the discussion.

No email address required.

Are you inputting it on the right page

Jump in the discussion.

No email address required.

i'm pretty sure?

Here, right?

https://i.rdrama.net/images/17024047031929288.webp

Jump in the discussion.

No email address required.

It literally says right under the box "not for private leaderboard codes"

Jump in the discussion.

No email address required.

I know I found it already, I concede I'm r-slurred

Jump in the discussion.

No email address required.

No wait wrong page, I've got it now. Ignore me, I'm a fricking r-slur

Jump in the discussion.

No email address required.

mann I tried the 2nd part but my caveman method of just marking from the outside-in didn't work & I'm partial about making a maze algorithm rn

https://i.rdrama.net/images/17022668281162214.webp

Jump in the discussion.

No email address required.

Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.