|
| 1 | +package com.algorithm.contest.advent_of_code_2018.day03.puzzle1; |
| 2 | + |
| 3 | +import jdk.nashorn.internal.runtime.regexp.joni.Regex; |
| 4 | + |
| 5 | +import java.io.BufferedReader; |
| 6 | +import java.io.FileReader; |
| 7 | +import java.io.IOException; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.regex.Matcher; |
| 11 | +import java.util.regex.Pattern; |
| 12 | + |
| 13 | +public class Solution { |
| 14 | + public static void main(String[] args) { |
| 15 | + try { |
| 16 | + FileReader fileReader = new FileReader("src/com/algorithm/contest/advent_of_code_2018/day03/puzzle1/input"); |
| 17 | + BufferedReader bufferedReader = new BufferedReader(fileReader); |
| 18 | + String line; |
| 19 | + int maxWeight = 0, maxHeight = 0; |
| 20 | + Pattern pattern = Pattern.compile("^#\\d+\\s@\\s(\\d+),(\\d+):\\s(\\d+)x(\\d+)"); |
| 21 | + while((line = bufferedReader.readLine())!= null) { |
| 22 | + //#1 @ 56,249: 24x16 |
| 23 | + Matcher matcher = pattern.matcher(line); |
| 24 | + matcher.find(); |
| 25 | + maxWeight = Math.max(maxWeight, Integer.parseInt(matcher.group(2)) + Integer.parseInt(matcher.group(4))); |
| 26 | + maxHeight = Math.max(maxHeight, Integer.parseInt(matcher.group(1)) + Integer.parseInt(matcher.group(3))); |
| 27 | + } |
| 28 | + |
| 29 | + int[][] check = new int[maxHeight+1][maxWeight+1]; |
| 30 | + fileReader = new FileReader("src/com/algorithm/contest/advent_of_code_2018/day03/puzzle1/input"); |
| 31 | + bufferedReader = new BufferedReader(fileReader); |
| 32 | + while((line = bufferedReader.readLine())!= null) { |
| 33 | + Matcher matcher = pattern.matcher(line); |
| 34 | + matcher.find(); |
| 35 | + for(int i = Integer.parseInt(matcher.group(2)); i < Integer.parseInt(matcher.group(2)) + Integer.parseInt(matcher.group(4)); i++) { |
| 36 | + for(int j = Integer.parseInt(matcher.group(1)); j < Integer.parseInt(matcher.group(1)) + Integer.parseInt(matcher.group(3)); j++) { |
| 37 | + check[i][j]++; |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + int overlapCnt = 0; |
| 42 | + for(int i = 0; i < check.length; i++) { |
| 43 | + for(int j = 0; j < check[i].length; j++) { |
| 44 | + if(check[i][j] > 1) { |
| 45 | + overlapCnt++; |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + System.out.println(overlapCnt); |
| 50 | + } catch (IOException e) { |
| 51 | + e.printStackTrace(); |
| 52 | + } |
| 53 | + } |
| 54 | +} |
0 commit comments