Skip to content

Commit 0f54911

Browse files
committed
solve problem
1 parent 59b0e7e commit 0f54911

File tree

5 files changed

+2940
-1
lines changed

5 files changed

+2940
-1
lines changed

.gitignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,8 @@ bin/
5151
.project
5252
.settings
5353
src/com/algorithm/training/dynamic_programming/quantize
54-
src/com/algorithm/training/array/my_calendar2
54+
src/com/algorithm/training/array/my_calendar2
55+
target
56+
algorithm.iml
57+
.gitignore
58+
.idea/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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

Comments
 (0)