-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPartOne.java
62 lines (49 loc) · 1.77 KB
/
PartOne.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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
* --- Day 4: Scratchcards ---
*
*
* https://adventofcode.com/2023/day/4
**/
public class PartOne {
public static void main(String[] args) throws Exception {
int total = 0;
try {
Scanner scanner = new Scanner(new File("./src/input.txt"));
while (scanner.hasNextLine()) {
String input = scanner.nextLine();
String[] cardSplit = input.split(":");
input = cardSplit[1];
String[] ownAndwinningCards = input.split(" \\| ");
String[] ownCards = ownAndwinningCards[0].split(" ");
String[] winningCards = ownAndwinningCards[1].split(" ");
int count = 0;
for (int i = 0; i < ownCards.length; i++) {
for (int j = 0; j < winningCards.length; j++) {
if (ownCards[i].isEmpty() || winningCards[j].isEmpty()) {
continue;
}
int ownCard = Integer.parseInt(ownCards[i]);
int winningCard = Integer.parseInt(winningCards[j]);
if (ownCard == winningCard) {
if (count == 0) {
count = 1;
} else {
count += count;
}
}
}
}
if (count != 0) {
total += count;
}
}
scanner.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
System.out.println(total);
}
}