Repository navigation
Expand file tree
/
Copy pathDay3.java
More file actions
96 lines (81 loc) · 2.19 KB
/
Copy pathDay3.java
File metadata and controls
96 lines (81 loc) · 2.19 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package adventForCode;
import java.io.FileNotFoundException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Scanner;
class MainDay3 {
public static void main(String[] args) throws FileNotFoundException {
Day3 day3 = new Day3();
day3.main();
}
}
class Day3 {
public void main() throws FileNotFoundException {
Scanner textFile = new Scanner(Paths.get("C:\\day3.txt").toFile());
String line;
ArrayList<Rucksack> rucksacks = new ArrayList<>();
int score = 0;
while (textFile.hasNextLine()) {
line = textFile.nextLine();
String s1a = line.substring(0, ((line.length() / 2)));
String s1b = line.substring((line.length() / 2));
rucksacks.add(new Rucksack(s1a, s1b));
}
for (Rucksack rucksack: rucksacks) {
score = score + rucksack.score;
}
}
}
class Alphabet {
int score;
char letter;
Alphabet(int score, String letter) {
this.score = score;
this.letter = letter.charAt(0);
}
public int getScore() {
return score;
}
public char getAlphabet() {
return letter;
}
}
class Rucksack {
String firstHalf;
String secondHalf;
char duplicateChar;
int score;
static ArrayList<Alphabet> alphabetArrayList = new ArrayList<>();
static {
for (int i = 1; i < 53; i++) {
alphabetArrayList.add(new Alphabet(i, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".substring(i - 1, i)));
}
}
Rucksack(String firstHalf, String secondHalf) {
this.firstHalf = firstHalf;
this.secondHalf = secondHalf;
determineDuplicateChar();
determineScore();
}
private void determineScore() {
if (duplicateChar == '!') {
this.score = 0;
}
for (Alphabet alphabet : alphabetArrayList) {
if (alphabet.getAlphabet() == duplicateChar) {
this.score = alphabet.getScore();
}
}
}
private void determineDuplicateChar() {
char duplicate = '!';
for (char c : firstHalf.toCharArray()) {
for (char b: secondHalf.toCharArray()) {
if (b == c){
duplicate = b;
};
}
this.duplicateChar = duplicate;
}
}
}