Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.tterrag.advent2018.days;
- import java.util.List;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import com.tterrag.advent2018.util.Day;
- import lombok.Value;
- public class Day03 extends Day {
- public static void main(String[] args) {
- new Day03().run();
- }
- // @Override
- // protected List<String> linesList() {
- // return List.of("#1 @ 2,2: 4x2", "#2 @ 6,2: 2x2", "#3 @ 5,5: 2x2");
- // }
- @Value
- private static class Claim {
- int id;
- int x, y, w, h;
- void fill(long[][] area) {
- for (int i = x; i < x + w && i < area.length; i++) {
- for (int j = y; j < y + h && j < area[i].length; j++) {
- area[i][j]++;
- }
- }
- }
- boolean collides(Claim other) {
- return inside(other.getX(), other.getY())
- || inside(other.getX() + other.getW() - 1, other.getY())
- || inside(other.getX() + other.getW() - 1, other.getY() + other.getH() - 1)
- || inside(other.getX(), other.getY() + other.getH() - 1);
- }
- boolean inside(int x, int y) {
- return x >= this.x && x < this.x + w && y >= this.y && y < this.y + h;
- }
- private static Pattern PATTERN = Pattern.compile("#(\\d+)\\s@\\s(\\d+),(\\d+):\\s(\\d+)x(\\d+)");
- static Claim parse(String s) {
- Matcher m = PATTERN.matcher(s);
- if (!m.matches()) {
- throw new IllegalArgumentException();
- }
- 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)));
- }
- }
- private long[][] grid = new long[2000][2000];
- @Override
- protected Result doParts() {
- List<Claim> claims = parseList(Claim::parse);
- claims.forEach(c -> c.fill(grid));
- int part1 = 0;
- for (int i = 0; i < grid.length; i++) {
- for (int j = 0; j < grid[i].length; j++) {
- if (grid[i][j] > 1) {
- part1++;
- }
- }
- }
- for (int i = 0; i < claims.size(); i++) {
- Claim first = claims.get(i);
- boolean collides = true;
- for (int j = 0; j < claims.size() && collides; j++) {
- if (i != j && first.collides(claims.get(j))) {
- collides = false;
- }
- }
- if (collides) {
- return new Result(part1, first.getId());
- }
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment