Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.tterrag.advent2018.days;
- 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();
- }
- @Value
- private static class Claim {
- int id;
- int x, y, h, w;
- 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]++;
- }
- }
- }
- 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[][] claims = new long[2000][2000];
- @Override
- protected Result doParts() {
- parse(Claim::parse).forEach(c -> {
- c.fill(claims);
- });
- int part1 = 0;
- for (int i = 0; i < claims.length; i++) {
- for (int j = 0; j < claims[i].length; j++) {
- if (claims[i][j] > 1) {
- part1++;
- }
- }
- }
- return new Result(part1, "");
- }
- }
Add Comment
Please, Sign In to add comment