tterrag1098

Untitled

Dec 2nd, 2018
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. @Value
  2. private static class Claim {
  3. int id;
  4. int x, y, h, w;
  5.  
  6. void fill(long[][] area) {
  7. for (int i = x; i < x + w && i < area.length; i++) {
  8. for (int j = y; j < y + h && j < area[i].length; j++) {
  9. area[i][j]++;
  10. }
  11. }
  12. }
  13.  
  14. private static Pattern PATTERN = Pattern.compile("#(\\d+)\\s@\\s(\\d+),(\\d+):\\s(\\d+)x(\\d+)");
  15.  
  16. static Claim parse(String s) {
  17. Matcher m = PATTERN.matcher(s);
  18. if (!m.matches()) {
  19. throw new IllegalArgumentException();
  20. }
  21. return new Claim(Integer.parseInt(m.group(1)), Integer.parseInt(m.group(2)), Integer.parseInt(m.group(3)), Integer.parseInt(m.group(4)), Integer.parseInt(m.group(5)));
  22. }
  23. }
  24.  
  25. private long[][] claims = new long[100][100];
  26.  
  27. @Override
  28. protected Result doParts() {
  29. parse(Claim::parse).forEach(c -> {
  30. c.fill(claims);
  31. });
  32.  
  33. int part1 = 0;
  34. for (int i = 0; i < claims.length; i++) {
  35. for (int j = 0; j < claims[i].length; j++) {
  36. if (claims[i][j] > 1) {
  37. part1++;
  38. }
  39. }
  40. }
  41.  
  42. return new Result(part1, "");
  43. }
Advertisement
Add Comment
Please, Sign In to add comment