Rust Finally Betrayed Me (Coding in a Random Language Every Day)
Ғылым және технология
Advent of Code 2023 is UPON US! What better way to spend the holiday season, learn to program, and test your skills against your friends. This year, I've decided to try something kind of insane. I'll be choosing a language off the wheel every day.... let's see what happens.
🏫 COURSES 🏫 Learn to code in C at lowlevel.academy
📰 NEWSLETTER 📰 Sign up for our newsletter at mailchi.mp/lowlevel/the-low-down
🙌 SUPPORT THE CHANNEL 🙌 Become a Low Level Associate and support the channel at / lowlevellearning
🔥🔥🔥 SOCIALS 🔥🔥🔥
Low Level Merch!: lowlevel.store/
Follow me on Twitter: / lowleveltweets
Follow me on Twitch: / lowlevellearning
Join me on Discord!: / discord
Пікірлер: 239
"It's gonna work but it's disgusting" -My exact thoughts while working on my own solution
@alexander53
6 ай бұрын
My exact thoughts working on v0 of anything I've ever written
@GaymerJenn
6 ай бұрын
Yep, same lol. I hate working with multidimensional arrays.
"Unnecessary parentheses around 'if' condition"
@LowLevelLearning
6 ай бұрын
I will not be silenced.
@ninetysixvoid
6 ай бұрын
didnt ask + dont care + ratio
@ecranfortessa
5 ай бұрын
@@ninetysixvoid Ratio failed, lulz.
@IonicMC
4 ай бұрын
@@LowLevelLearning Lmao
Even when you write Rust, your code style is still so similar to C.
@ctleans6326
6 ай бұрын
Clippy throws a fit every other line
Took me 2.5 hours for both the stars. 90% of time was trying to make my logic work in rust and rest 10% was me contemplating if I should just use python
@Reydriel
6 ай бұрын
Would Python make the algorithm any less of a pain to write? I feel like this question was very intentionally made to be very annoying with its edge cases lol
@siddharthbisht8522
6 ай бұрын
@@Reydriel for me personally algorithm was not that hard to make within the first 2 minutes I knew what I wanted to do with small modifications here and there along the way ( And in no way am I a good CP I am pretty sure my logic would be doo doo water for any decent CP ) but the fact that I have just started with the rust lang. But in the end it was fun.
@ficolas2
6 ай бұрын
More or less the same, 1:50h for me, failed the first time I submitted because of an off by one error. Most of the time was struggling with rust (learning it with this year AoC, I have read through like 1/3rd of the rust book before this) and struggling with neovim (also learning it with this year AoC, I have been using vim motions for months tho) I much prefer my solution to LLL's, however the second part is super slow, takes like half a second to run, because the time complexity is ass, plus I used regex to find the numbers. However conceptually is super simple.
@siddharthbisht8522
6 ай бұрын
@@ficolas2 I too want to learn vim (neovim) but just cant stand it. Tried for the first time last week but couldn't figure out how to move away from the terminal window and focus on the editor without using the mouse lol. Maybe if I can find someone who will instantly help me out for the first few weeks until I develop some basic knowledge then I will happily move away from my vscode haha
@ficolas2
6 ай бұрын
@@siddharthbisht8522 I would really recommend doing what I did. First use vimMotions in vscode (or ideavim in intelliJ), and get used to it, before moving to neovim. Otherwise it is so overwhelming. I watched the primeagen's videos on vim motions, and now I can't live without them. Sometimes I am writing something in a Google docs and attempt to do some vim shenanigans...
How it started: *rust copium* How it went: “This is disgusting” “the classic off by one error” (lol, lmao even, rust safety go brrrr) It’s beautiful, brings a tear to my eye. Rust culture aside, solid video homie
@warmwaffles
6 ай бұрын
It was fun watching the pain and the sudden realization it was right.
Chat please get AWK on the list next time
Can't wait to see the final episode ended off with Malbolge, where the solution takes until next christmas to find!
@miezekatze3536
6 ай бұрын
I don't think anyone is *that* good to finish it in a single year
@informagico6331
6 ай бұрын
I think we all agree to select Malbolge whenever the KZread chat obtains the privilege to vote
@bjerrum4921
6 ай бұрын
haha funny impossible to read language
@sinom
6 ай бұрын
You can by now algorithmically construct malbolge programs. They will be nowhere near optimal but they will work.
I think you need to add COBOL to the wheel.
@AlLiberali
6 ай бұрын
YES!
@CatFace8885
6 ай бұрын
omg yes!!!! That would be fun to watch
@imaginebunnys1017
6 ай бұрын
Definitely
Love this idea since day 1, i'm doing something similar where each weekday is a different (set) language, so Monday - PHP (never touched before and am scared for tomorrow) Tuesday - Pyhon Wednesday - Rust (just started learning) Thursday - C# (havent used in years) Friday - Java Saturday - C++ (barely used) Sunday - Typescript
@AlLiberali
6 ай бұрын
I'd argue that php is a better lang than snake. Worry not and embrace the $
@miezekatze3536
6 ай бұрын
I created a list where I chose random languages from each day and got "Haskell, nim, Fortran"... Haskell was by far the easiest to work with
As a rust dev, this season's advent of code really makes me want to just switch to C++ due to the strict nature of Rust trying to prevent you doing bad things with strings, even I know damn well the input strings are just pure ascii chars 😂. Well I can use a library for doing ascii things but currently I don't want that
@cysia3683
6 ай бұрын
I'm just calling .as_bytes() and work with byte slices lmao
@matthewsanetra
6 ай бұрын
Just Vec if you know it's ASCII. It should have string methods implemented for it
@ficolas2
6 ай бұрын
I'm trying out rust for this AoC, and so far, I have only really struggled with strings because of trying to access a hashmap with a String, that had as key type &str. Since the strings just have normal characters, I just did .chars(), and accessed them normally. Rust so far has been a weird experience, and I kinda like that. My experience with is is, read through like 1/3rd of the rust book, and started the AoC this year
@ElZafro_
6 ай бұрын
I'm using C++ and the parsing is just awful. I've tried using views, string_views and even """"modern"""" C++ but i'm missing rust so much on this one
@NabekenProG87
6 ай бұрын
I converted everything to bytes: "".as_bytes().to_vec(); A lot easier to handle
Didnt expect anyone to pronounce chars as "Chairs"
@fders938
6 ай бұрын
Started off pronouncing it like the "char" in "chart", then midway switched to pronouncing it like "care".
Haskell was put on the wheel! It's a very powerful language and is the most popular of functional programming languages. It has its roots in academia but it's used in the industry often. Its strength is in symbol manipulation making it perfect for this challenge. Benchmarks on Haskell programs have shown it to be very fast too.
I spent a long time on this, and the whole time thinking "I must be so stupid"... Then I watch this and find I did almost the same logic in my solutions, one with and one without Regex. Thanks for making me not feel stupid, that a programmer magnitudes much better and more experienced than me didn't have a solution that to me is unintelligeable. :)
I approached this one as an axis aligned bounding box collision detection problem - just treat every symbol as a 3x3 box centered at the symbol, find what collides with it.
@rezwhap
6 ай бұрын
Nice. In part 1, how did you prevent double-counting if a number is adjacent to two symbols?
@worgenzwithm14z
5 ай бұрын
Do a uniqueness check on the x and y coordinate of the first character of the number Use a set
Cool series! I feel like I hit many similar emotions while solving today's challenge. Especially "I don't like this solution there should be a better way to do this" ... damn I felt that
Really glad it wasn't just me! My algorithm was similar; I read the array left to right, top to bottom and when it found a digit started reading that into a buffer until there wasn't any more digits while keeping track of x and y for those digits, then checked if there was any symbols around it (previous line, before and after, next line). To avoid index issues in the array I added a line of dots to the beginning and end and a dot at the start and end of each line. That worked fine, but I found the second star much harder. I did the same thing but only recorded if there was a * around the number, then walked in a circle around the * to look for a second number, and assumed there was no duplicate two numbers in the array.
My solution was starting by defining a function where you would give it x / y coords where there is a single digit, and it would return the full part number where that coords belongs to, with that function working the rest was pretty straightforward
Wanted to see how to do it cleverly since I was unhappy with my solution. Accidently you boosted my confidence ;) But now I know how to use enumerate(). Thanks for that.
I'm doing aoc in Uiua. I hated every second of this task and gave up on the second star of the day. But I gotta say, looking at you doing it in rust, I'm starting to see why some people love array programming. My solution is a total of 486 characters (including whitespace), and for every component, I can reason its correctness.
Love this series
Surprisingly out if all the codes so far (I’ve gone up to 4). This was by far my cleanest and most efficient whilst implementing. Really struggled day 1 part 2 because if the stupid joined numbers. This 1 took me like 30 mins for both part 1 and 2. Walk array recursively find numbers sum
I want to see a challenge completed in assembly.
@y4ni608
6 ай бұрын
it wouldnt even be hard just annoying af
Wrote this day in Rust as well. Surprisingly not painful. It helped that I made the smallest thing a separate function. The very first thing I did was make a function that extracted the part code from a line, from wherever you started on that line if you started on a digit.
@rezwhap
6 ай бұрын
This is the way. We must decompose the problem! 🙂
These 3 days were "How to make nasty things to parse tutorial"
@KingJellyfishII
6 ай бұрын
they were actually quite nice and well-defined compared to a lot of problems I've encountered in real life...
@francescotomba1350
6 ай бұрын
@@KingJellyfishII feel you
I'm currently working on Day 3 in rust, and I'm happy I decided against the sort of approach you took here. I'm using regexes to locate numbers and symbols, and I'm doing a 2 pass approach: Each line has it's own vector of indices adjacent to a symbol on that line. Pass 1 locates each symbol and inserts the appropriate indices into the appropriate vectors. Pass 2 checks each number on each line, checking whether any of the adjacency indices are within the number's start/end range. Currently trying to figure out where my bug is, because I'm convinced I have exactly 1 bug preventing me from getting the right answer currently but code blindness has set in and I'm going mad because I haven't even tracked down which number it fails to count.
I looped over chars ,and each time i encountered a digit, i searched around the digit for a symbol & marked the cur_num as valid if yes. Part2 was similar. For each *, searched around, then found the start of any number & kept an array of start-positions to prevent duplicates.
@LowLevelLearning
6 ай бұрын
OH MY GOD THATS SO MUCH BETTER
@KingJellyfishII
6 ай бұрын
This is also pretty much how I solved it
@PoProstuLatanie
6 ай бұрын
I did the same for part 1, for part 2 i serached for stars and looked around them for numbers
@MrCumberlander1
6 ай бұрын
Yeah that's what I ended up doing. Except that instead of looping over chars I just used a find on the line. So I had to do less work manually myself.
Mulling in my head a good way to solve the problem algorithmically. I suspect if you were to redefine the numbers as a set of rectangles (co-ords for top left and bottom right) mapped to their equivalent value, and the symbols also to a set of rectangles that are plus one to each side on the symbol in the centre, and then do a intersection of symbols rectangles to numbers rectangles, then you can sum where you get a hit. I feel like this could be brought down to a few easier to manage passes transforming the data gradually.
I did it in Rust, i collected the lines into a list with an "all-dots" line at the beginning and end, walked that list with a window of 3, and zipped the 3 slices from the windows with a window of 7, if the center most char is a symbol, i basically check all possible combinations of chars for numbers, one of the checks being using another window of 3 over window(7) slice so in total, i call 'slice::window' 5 times my solution only works because the input does not have any symbol at the start or end of lines
some guy out there is doing AoC in the game "turing complete", basically assembly but worse
@thezouave7636
6 ай бұрын
Holy how, I need a link
@AlLiberali
6 ай бұрын
Legend
I ended up rewriting my solution like 3 times. Today was an annoying one, but after 4 hours I got part 1 working. Luckily part 2 was relatively easy even though I had to rewrite my entire algorithm to handle it.
So you kept saying "If Haskell gets on this list, I'm f'd"... but... like... you KNEW this would 100% guarantee they choose Haskell. Right?
I LOVE THIS SERIEESS
I also used Rust, also had off by one error, lol. But I kinda like my solution better, I feel like you made it little bit more complex than it needed to be.
#1 rule of livestreams You never tell chat what you don't want them to do
I like Rust but I easily create logic bugs because I force idomatic code too hard. When the problem doesn't fit the closest idom, it hurts.
Would love it if you added a timer so we could now more or less how much time it takes you to do these
Oh yeah, I remember running into the same issue. But I think I solved it by adding a column of dots to the right of my inputs 🤣
For part 1, I just looked for a symbol and generated a list of adjacent coordinates and iterated through to see if first they are valid coordinates and if they contain a number. If it is then find left and right of number, convert to int, and replace with periods to avoid duplicates. Part 2 was trivial with that solution. All in it took about 45 minutes. Was very proud compared to day 2. Language Go.
@pie6029
6 ай бұрын
I did the complete opposite thing. For part 1, I looked at the surroundings of each number to figure out if a symbol is nearby. For part 2 I had a Hashmap with the key being the coordinate of the Symbol and the value being the 2 nearby numbers, or 0 if none are there. (Rust)
Haskell is my favorite language. It's not hard, it's just different. Also, parsing input in Haskell is a breeze, you're gonna like it!
I also used rust, but I used pest as my parser, since it gives me the spans of the numbers and symbols
You have to add tcl, postscript, perl and forth to that list
Just watched the first haskell stream on Twitch, that was pure pain.
I did this challenge, and I didn’t store any structures whatsoever and I felt my solution was so naive. But then I had to do part 2 differently. Sounds like if you do part1 the correct way part2 is simple extension, maybe?
These videos are awesome
watching this as much entertaining as hurting at a same time hahahah, I do same way though, all of us I guess
Not a good year to try advent of code in Rust with Colemak-DH with Miryoku keymap on split keyboard when every single one of these things is new to me. lol. I definitely have an urge to just use python.
I'm solving this year's challanges in Go and boy was today difficult. And it's only day 3. 🤣
my struggle for this problem was trying so subtract a int from a usize, i kept doing usize as isize but didnt seem nice to do
@SVVV97
6 ай бұрын
Why did you have a isize to begin with? I don't quite see where you'd get the signed value from
@notkoalified
6 ай бұрын
@@Romenamath This, or alternatively saturating_sub
This is my algorithm for the first half of the problem (also in Rust btw): ooooooo o69420o ooooooo All the o's in the above diagram are all the positions you gotta check for a symbol (i.e all its neighbours). If you observe, for all the digits you just need to check the cell above and cell below, except for the first and last digit. To fix this, just assume the middle line's first 'o' and last 'o' are also digits. Now, for all "digits", you just need to check the cell above and below. However, that would mean you would need to check for those two 'o's separately. So, you CAN do that, or you can be lazy like me and instead for each "digit", you check for cell above, cell below and yourself for being a symbol. Since digits are guaranteed to not be a symbol, this is just equivalent to checking for the middle 'o's as the check for yourself will always return false for digits. Sure, computation wasted, but effort saved.
I'm doing same thing, pick random language for day, but my list is smaller than yours. Day 3, javascript 😢, I'm literally 15 times slower than with Lua yesterday
"It's gonna work but it's disgusting" - Whenever I get a date now that I'm over 30.
I kept thinking I was done on this one only to run into another small issue.
Next poll should include Fortran 🔥
that was really enjoyable to watch
Dude, 7 layers of nesting is enough for an elephant to nest into
Yeah, I'd initially made that off-by-one error as well.
How you got the first star without handling '/' as a valid character?
I felt like Day3 was significantly harder than most day 3 questions on AOC
I solved both parts by building up a list of numbers and a list of symbols, then examining those lists to calculate the answers
Please add speedrun timer!
I would love to see Lua❤
How about dart? Is it included already?
So, as someone who hasn't used Rust and hasn't used PHP since PHP 5, I have to conclude PHP is currently better than Rust? :-P
This problem was not hard, but boy was it annoying to code😢
Should you not be building these as unit testable modules though and using TDD? ;)
whats your operation system and browser?
first year participating in this challenge do they get harder/more annoying every day? kinda seems that way so far
Title is the definition of a workman blaming his tools :D
My friend coded a fkin matrix logic based blazingly fast simd solution for this in python+numpy like it's nothing while i struggled with my for loops, kill me
I was able to get the first part fairly quickly but after hours on the second part I stepped away. The code was absolutely disgusting.
Fortran should be an alternate lang -definitely Cobol
This makes my solution look clean wtf.
I don't know why, but I misinterpreted the question as numbers touching parts (other numbers touching symbols) also being parts themselves. My unimplemented solution was gonna be something like: 1. 2-pass connected components (need to figure out 8-way neighbour helper function for non-dots) 2. For each blob in raster order, if a symbol exists, sum parseable digits. The intended reading is obviously way easier...
How do these challenges prevent cheating? Love the videos keep em coming!
@nirajandata
6 ай бұрын
these type of challenges are for fun. I don't think people cheat here as they have nothing to gain by cheating
@PoProstuLatanie
6 ай бұрын
Also input for each person is slightly different so if you don't have the code but only somebody else's answer it's not going to work
@U20E0
6 ай бұрын
@@Romenamath why does it even tell you if it's higher or lower? That isn't particularly useful for debugging in most cases. Is that there literally only to make brute-force easier and thus bait cheaters?
Ooh I know I'm in for a treat
It's the season, so this guy chose the Christmas tree of death. 7:09
First ,your videos are great 👍.
13:06 I think it's because you cant format and write code properly, because you using vim, instead of normal IDE Also I don't understand why Vim can't add whitespaces when you go to a new line?
Remember, "Haskell makes you a better programmer" ;-)
Yes! Please now let the wheel fall on Haskell next time!
Forth. That's the language for the wheel. ;)
i’m sorry to say but i think your getting haskell 😂 also i made the same mistake when I did it myself lol
title: rust betrayed me content: off by 1 error
I just wanna see a code challenge done in IBM COBOL. Sorry
Hell yeah, it's Haskell time
While not a language per se I want to see SIMD on the wheel.
We need Rust 2.0 with added features that will make off by one errors harder to make.
Rust is still there next to Haskell
@LowLevelLearning
6 ай бұрын
public static void chair star rawr main
@diosrightcalfmuscle4090
6 ай бұрын
@@LowLevelLearning Dons't forget the uwu type declaration
Why he hates Haskell do much? 😂
You gotta do VOICEs like Prime
Just hoping for Fortran or Forth to come on that list. 🤞
@torarinvik4920
6 ай бұрын
Forth +1, but I wanna see him feel the pain of Haskell first 👹
@SVVV97
6 ай бұрын
Why stop at haskell: ATS! ATS! ;D
I messed up both parts at first. I had a pretty nice solution for part 1, but I had to completely rewrite it for part 2. (I also use Rust BTW)
Tbh i think you pick the wrong structure for this problem, i just made an integer matrix and in half a hour was done :(
I would choose assembly
Can someone confirm pls - was that was 9 levels of indentation?
i skipped to the end and holy sh!t there is a lot of loops inside of loops
@helbertgascon
6 ай бұрын
Sounds like my life 😂
"Length of the digit" was confusing me... surely that's always 1 😉
Prolog on the wheel or riot
Writing this in Rust was... fun.
@LowLevelLearning
6 ай бұрын
no
@marekvrbka
6 ай бұрын
@@LowLevelLearning I mean, it made me use a 2d coordinates library (wrapping_coords2d). It also made me realize that a proper coordinate library is missing in Rust, so I guess I will write one to be at peace.
Intercal next time!!!