5

@lensflare I'm doing AOC with swift so far I like it, it's unfortunate they do the retarded "modern" `let name: <type>` thing and strings can be annoying to substring, otherwise it's good

Comments
  • 2
    I love the modern style. Once you get used to it, the other style becomes weird.

    The substring types are just for better performance and safety. If you want a string, simply put it into

    String(substring)

    But I agree, it can become annoying, especially with index based access.

    They have special types for that to handle utf-8 properly, because it has variable length characters.

    Glad that you give it a try!
  • 3
    @Lensflare I was meaning more the

    str[str.index(str.startIndex, offsetBy: 1)...]

    To me, that's obviously what I want with str[1...]

    I dislike the modern use of ":" and find it makes it unnecessary messy to type types, just omit it and it's fine like they did with ( and ) :D

    (I'm completely fine with the types being optional while writing)
  • 3
    Good idea to learn a language this way actually. Nice. Please do a full swift review afterwards.
  • 1
    @BordedDev

    > str[str.index(str.startIndex, offsetBy: 1)...]

    Yes, I already thought you mean that. Like I said, it’s not using integers to access strings by index (or ranges). It uses special string index types tailored to the specific kind of string. In most cases utf-8.

    But I agree that the syntax is a bit of a pain in the butt.

    > (I'm completely fine with the types being optional while writing)

    That’s called type inference.
  • 1
    @retoor So far it's pretty high in the list :D
  • 1
    @BordedDev oh, btw, here is a less ugly example which does the same:

    str.dropFirst(1)

    There is also

    dropLast(), prefix(), and suffix()

    And with that you can slice and substring your strings basically as if you were using integers
  • 1
    @BordedDev and the four functions that I mentioned also work with any collection, not just strings.
  • 2
    @Lensflare The name of it wasn't at top of mind ;P

    And yeah, I used those in the later ones. Though it would have been nice if the function just did,

    func str.substr(from: Int, to: Int) {

    return str[str.index(str.startIndex, offsetBy: from)...]

    }

    I don't really care what type of data size is being used for "chars" I just want the stuff between x and y
  • 1
    @BordedDev

    Maybe this is a bit less awkward:

    let from = 3
    let to = 5

    str.dropFirst(from).prefix(to - from)

    or…

    str.prefix(to).dropFirst(from)
  • 1
    So I saw some code that was generated for AoC by an ai agent in an IDE. It created a nested for loop for x and y within a specific range in a double dimensional array. One of the comments on the code complained about the nested for loops as being a problem. There was also a switch statement on strings inside, but whatever. I cringed on the nested for loops when I saw them, but I don't know if I am just stupid, but is there a better way to do operations on a double dimensional array? You have to iterate at some point.
  • 2
    @YourMom Keep the array 1 dimensional and do the offset maths?
  • 1
    @BordedDev in C++ the math is done in the index to the arrays. I have done this before as you say. Back in DOS days we could access the 320x200 256 color screen as a 64000 byte array via a pointer. I would do the math to calculate the index to write the data. Now there is so much abstraction between.

    Not sure its any faster though. Because the double loop was making decisions inside the inner loop.
  • 2
    @YourMom Not sure, I think optimising nested loops like that (probably subconsciously trying to get away from O(n * 2)) is just trying to find what the branch predictor of your CPU likes
  • 2
    @BordedDev I had two large arrays that mapped points on lines. I wanted to find intersections. They were x/y plots of points. So I made a single map the number of all points. Then I went over each array once. If the point existed more than once it would add 1 to the value of the map at that point. Then I could see all intersections by looking if the value was greater than 1. It was the best approach in hind site, but it avoided matching points in massive nested loops.

    I learned a bit about how to change how you view the data to process things better.
  • 1
    @YourMom Ah yeah, I messed up notation earlier, nested loops would have been O(N^M) whereas your solution is O(N+M) if I'm remembering that notation correctly. I thought you meant that you had nested arrays where the index meant the X and then the Y position on the nested one
  • 1
    @BordedDev It was two dimensional arrays on the the first one. But on the one I worked on it was not. I was trying to avoid that so I made 1 dimension.
  • 1
    @YourMom but I was comparing a 10K element array against another 10K element array. So it was naively a nested loop. I turned it into a 2 10K element parses vs 10K * 10K parse looking for intersections.
Add Comment