tterrag1098

Untitled

Dec 2nd, 2018
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. package com.tterrag.advent2018.days;
  2.  
  3. import java.util.List;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. import com.tterrag.advent2018.util.Day;
  8.  
  9. import lombok.Value;
  10.  
  11. public class Day03 extends Day {
  12.  
  13. public static void main(String[] args) {
  14. new Day03().run();
  15. }
  16.  
  17. // @Override
  18. // protected List<String> linesList() {
  19. // return List.of("#1 @ 2,2: 4x2", "#2 @ 6,2: 2x2", "#3 @ 5,5: 2x2");
  20. // }
  21.  
  22. @Value
  23. private static class Claim {
  24. int id;
  25. int x, y, w, h;
  26.  
  27. void fill(long[][] area) {
  28. for (int i = x; i < x + w && i < area.length; i++) {
  29. for (int j = y; j < y + h && j < area[i].length; j++) {
  30. area[i][j]++;
  31. }
  32. }
  33. }
  34.  
  35. boolean collides(Claim other) {
  36. return inside(other.getX(), other.getY())
  37. || inside(other.getX() + other.getW() - 1, other.getY())
  38. || inside(other.getX() + other.getW() - 1, other.getY() + other.getH() - 1)
  39. || inside(other.getX(), other.getY() + other.getH() - 1);
  40. }
  41.  
  42. boolean inside(int x, int y) {
  43. return x >= this.x && x < this.x + w && y >= this.y && y < this.y + h;
  44. }
  45.  
  46. private static Pattern PATTERN = Pattern.compile("#(\\d+)\\s@\\s(\\d+),(\\d+):\\s(\\d+)x(\\d+)");
  47.  
  48. static Claim parse(String s) {
  49. Matcher m = PATTERN.matcher(s);
  50. if (!m.matches()) {
  51. throw new IllegalArgumentException();
  52. }
  53. 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)));
  54. }
  55. }
  56.  
  57. private long[][] grid = new long[2000][2000];
  58.  
  59. @Override
  60. protected Result doParts() {
  61. List<Claim> claims = parseList(Claim::parse);
  62.  
  63. claims.forEach(c -> c.fill(grid));
  64.  
  65. int part1 = 0;
  66. for (int i = 0; i < grid.length; i++) {
  67. for (int j = 0; j < grid[i].length; j++) {
  68. if (grid[i][j] > 1) {
  69. part1++;
  70. }
  71. }
  72. }
  73.  
  74. for (int i = 0; i < claims.size(); i++) {
  75. Claim first = claims.get(i);
  76. boolean collides = true;
  77. for (int j = 0; j < claims.size() && collides; j++) {
  78. if (i != j && first.collides(claims.get(j))) {
  79. collides = false;
  80. }
  81. }
  82. if (collides) {
  83. return new Result(part1, first.getId());
  84. }
  85. }
  86.  
  87. return null;
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment