-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day13.java
83 lines (75 loc) · 2.22 KB
/
Day13.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Day13 {
public static void fold_horizontal(int n,int m,int pos,boolean[][] board) {
for (int i=0;i<n;++i)
for (int j=pos+1;j<m;++j)
board[i] [2*pos - j ] |= board[i][j];
}
public static void fold_vertical(int n,int m,int pos,boolean[][] board) {
for (int j=0;j<m;++j)
for (int i=pos+1;i<n;++i)
board[ 2*pos - i ] [j] |= board[i][j];
}
public static int function(String fname,int numOfOperrations,boolean print) throws IOException {
String ln;
BufferedReader brdr=new BufferedReader(new FileReader(fname));
int n=0;
int m=0;
while (!(ln=brdr.readLine()).equals("")) {
int[] yx= {Integer.parseInt(ln.split(",")[0]),Integer.parseInt(ln.split(",")[1])};
n=Math.max(yx[1]+1, n);
m=Math.max(yx[0]+1, m);
}
boolean[][] board=new boolean[n][m];
brdr.close();
brdr=new BufferedReader(new FileReader(fname));
while (!(ln=brdr.readLine()).equals("")) {
int[] yx= {Integer.parseInt(ln.split(",")[0]),Integer.parseInt(ln.split(",")[1])};
board[yx[1]][yx[0]]=true;
}
for (int iter=0;iter<numOfOperrations;++iter) {
ln=brdr.readLine();
if (ln==null)
break;
String[] lsplit= ln.split(" ")[2].split("=");
int pos=Integer.parseInt(lsplit[1]);
if (lsplit[0].charAt(0)=='x') {
//System.out.println(n+" ["+m+"] "+pos);
fold_horizontal(n,m,pos,board);
m= pos;
}else {
//System.out.println("["+n+"] "+m+" "+pos);
fold_vertical(n,m,pos,board);
n=pos;
}
}
brdr.close();
if (print) {
for (int i=0;i<n;++i) {
for (int j=0;j<m;++j)
if (board[i][j])
System.out.print("#");
else
System.out.print(".");
System.out.println();
}
}
System.out.println();
int ans=0;
for (int i=0;i<n;++i)
for (int j=0;j<m;++j)
ans += ( board[i][j]) ? 1 : 0;
return ans;
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String SAMPLE="input/day13_sample.txt";
String REAL="input/day13.txt";
System.out.println(function(SAMPLE,1,false));
System.out.println(function(SAMPLE,10000,true));
System.out.println(function(REAL,1,false));
System.out.println(function(REAL,1000,true));
}
}