Skip to content

Instantly share code, notes, and snippets.

@woojiahao
Created March 7, 2018 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woojiahao/a38fb97271234d567f114a71132e15a7 to your computer and use it in GitHub Desktop.
Save woojiahao/a38fb97271234d567f114a71132e15a7 to your computer and use it in GitHub Desktop.
CodeWars Challenge: John and Ann sign up for CodeWars (Uncomplete)
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/*
I was unable to complete this as I had overlooked how to manage 2 lists at once, providing
checks on when to fill what array. I had kept a linear fashion of thinking that each had to be
incremented every iteration, which was invalid.
On the other hand, using this solution, we add values based on whether the other has it, rather
than ensuring both lists are updated simultaneously
*/
public class Johnann {
enum Person {
JOHN, ANN;
}
private static List<Long> generateLists (Person person, long n) {
List<Long> john = new ArrayList<Long>(Collections.singletonList(0L));
List<Long> ann = new ArrayList<Long>(Collections.singletonList(1L));
for (int dayAnn = 1, dayJohn = 1; dayAnn < n || dayJohn < n;) {
if (john.size() > ann.get(dayAnn - 1)) {
ann.add(dayAnn - john.get(Math.toIntExact(ann.get(dayAnn - 1))));
dayAnn++;
}
if (ann.size() > john.get(dayJohn - 1)) {
john.add(dayJohn - ann.get(Math.toIntExact(john.get(dayJohn - 1))));
dayJohn++;
}
}
return (person == Person.JOHN ? john : ann);
}
public static List<Long> john(long n) {
return generateLists(Person.JOHN, n - 1);
}
public static List<Long> ann(long n) {
return generateLists(Person.ANN, n);
}
public static long sumJohn(long n) {
return generateLists(Person.JOHN, n - 1).stream().mapToLong(l -> l).sum();
}
public static long sumAnn(long n) {
return generateLists(Person.ANN, n).stream().mapToLong(l -> l).sum();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment