tterrag1098

Untitled

Dec 2nd, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. package com.tterrag.advent2018.days;
  2.  
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5.  
  6. import com.tterrag.advent2018.util.Day;
  7.  
  8. import lombok.Value;
  9.  
  10. public class Day03 extends Day {
  11.  
  12. public static void main(String[] args) {
  13. new Day03().run();
  14. }
  15.  
  16. @Value
  17. private static class Claim {
  18. int id;
  19. int x, y, h, w;
  20.  
  21. void fill(long[][] area) {
  22. for (int i = x; i < x + w && i < area.length; i++) {
  23. for (int j = y; j < y + h && j < area[i].length; j++) {
  24. area[i][j]++;
  25. }
  26. }
  27. }
  28.  
  29. private static Pattern PATTERN = Pattern.compile("#(\\d+)\\s@\\s(\\d+),(\\d+):\\s(\\d+)x(\\d+)");
  30.  
  31. static Claim parse(String s) {
  32. Matcher m = PATTERN.matcher(s);
  33. if (!m.matches()) {
  34. throw new IllegalArgumentException();
  35. }
  36. 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)));
  37. }
  38. }
  39.  
  40. private long[][] claims = new long[2000][2000];
  41.  
  42. @Override
  43. protected Result doParts() {
  44. parse(Claim::parse).forEach(c -> {
  45. c.fill(claims);
  46. });
  47.  
  48. int part1 = 0;
  49. for (int i = 0; i < claims.length; i++) {
  50. for (int j = 0; j < claims[i].length; j++) {
  51. if (claims[i][j] > 1) {
  52. part1++;
  53. }
  54. }
  55. }
  56.  
  57. return new Result(part1, "");
  58. }
  59. }
Add Comment
Please, Sign In to add comment