-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday10solution.java
More file actions
77 lines (68 loc) · 2.36 KB
/
Copy pathday10solution.java
File metadata and controls
77 lines (68 loc) · 2.36 KB
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
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class day10 {
public static void main(String[] args) throws FileNotFoundException {
List<String[]> input = getinput("input10");
System.out.println(part1(input));
part2(input);
}
public static void part2(List<String[]> ls){
int[][] crt = new int[6][40];
int reg = 1, row = 0, col = 0;
for(String[] str : ls){
if(col >= 40){
col=0;
row++;
}
if(col <= reg + 1 && col >= reg - 1)// reg - 1 <= col <= reg + 1, check if sprite loc in range
crt[row][col] = 1;//draw
col++;
if(!str[0].equals("noop")) {
if(col >= 40){
col=0;
row++;
}
if(col <= reg +1 && col >= reg -1)// reg - 1 <= col <= reg + 1, check if sprite loc in range
crt[row][col] = 1;//draw
col++;
}
if(str.length == 2)
reg+= Integer.parseInt(str[1]);
}
for (int i=0; i < crt.length; i++){//print screen
for (int j=0; j < crt[0].length; j++){
System.out.print(crt[i][j] == 1 ? "#":".");
}
System.out.println();
}
}
public static int part1(List<String[]> ls){
int sum = 0;
int reg = 1;
int cycle = 0;
for(String[] str : ls){
cycle++;
if(cycle <= 220 && ((cycle-20) % 40) == 0 )
sum += cycle * reg;
if(!str[0].equals("noop")) {
cycle++;
if (cycle <= 220 && ((cycle - 20) % 40) == 0)
sum += cycle * reg;
}
if(str.length == 2)
reg+= Integer.parseInt(str[1]);
}
return sum;
}
public static @NotNull List<String[]> getinput(String filename) throws FileNotFoundException {
var reader = new Scanner(new File("C:\\Users\\USER\\IdeaProjects\\AdventOfCode_Gil_2022\\src\\input10.txt"));
var list = new ArrayList<String[]>();
while (reader.hasNextLine())
list.add(reader.nextLine().split(" "));
return list;
}
}