Ad
  • Custom User Avatar

    Same here. Caching the results doesn't help and I don't intend to implement a BK-tree on my own for a 3 kyu kata.

    EDIT: Calculating the distance has been the bottleneck here. Try a more efficient implementation. It should be able to calculate the distance from abcdefghijklmno to pqrstuvwxyzabcd very quickly instead of taking ages.

  • Default User Avatar

    Damn... Yeah, I did that translation a while ago and didn't know the problem with arrays used with hashcodes yet. I'll take a look at all of this later.

  • Custom User Avatar

    Actually, my testcases all validated except the randomcases. So I actually had to make a distinction method and then it all ran fine.

    Looking at your testcase creation for the random ones; I see you use:
    Set<int[]> posAtt = new HashSet<int[]>();

    This is where it goes wrong. new int[]{1,3} != new int[]{1,3}

    arrays use the default hashcode implementation for checking equals.

    here are some possible solutions:
    use a SortedSet like TreeSet and give a comparator on creation like: Set<int[]> test = new TreeSet<>(Comparator.comparing(Arrays::toString));
    use a Set of lists instead of int[], they actually check the elements on equality.
    my personal favourite: use a set of Point. Its an incredibly simply int-tuple that does what you expect.

    If you don't want to destroy any of the current solutions, the first suggestion would work fine (altough it's very bad!), but just remember for next time to use an actual Object as keys in sets and maps :)

  • Default User Avatar
  • Default User Avatar

    ok.

    Errr on second thoughts, you don't have to handle this because you will never hit several times the same spot in the "real life" game. Accordingly, the provided attack array never give you duplicate coordinates. That's why most of the solution do not take that into account.

  • Default User Avatar