2 ms·
For the specifics, as someone else observed, walking nums to find the values we've discovered and identify their indices is very affordable, it's N operations,
by tialaramex 3y ago
For the specifics, as someone else observed, walking nums to find the values we've discovered and identify their indices is very affordable, it's N operations, and if we can't afford N we're in trouble since we definitely need to do at least N comparisons.
To your general point, sure, one thing that's nice (but takes a colossal effort) about Advent of Code is that it has specific inputs for players, and they have specific answers, so although it's usual for participants to write code which could (you hope) solve the problem in the abstract, the actual test is whether you solved the input you actually had. Sometimes not all conceivable inputs are even tractable (and some people find this situation frustrating if it happens) only the inputs given to real players may be suitable.
For this exercise the provided Go solution gets to handwave a bunch of problems. For example, Go's "map" hash table allocates whenever it wants to and has amortized table growth on top. That can really add up for a problem like this.
If I attacked it in Rust I would make HashMap::with_capacity(nums.len()) which does a single allocation up front, I might not need all this space if I exit early, but it won't hurt. I don't know if there's even a way to write that in Go.