-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPart1.java
More file actions
138 lines (110 loc) · 3.86 KB
/
Copy pathPart1.java
File metadata and controls
138 lines (110 loc) · 3.86 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package y2016.d04;
import common.Files;
import org.jetbrains.annotations.NotNull;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Part1 {
private static class Letter implements Comparator<Letter>, Comparable<Letter> {
private Character character;
private int count;
public Letter(Character character, int count) {
this.character = character;
this.count = count;
}
public Character getCharacter() {
return character;
}
public int getCount() {
return count;
}
@Override
public int compareTo(@NotNull Part1.Letter o) {
return compare(this, o);
}
@Override
public int compare(Letter o1, Letter o2) {
int countDifference = o2.getCount() - o1.getCount();
if (countDifference != 0) {
return countDifference;
}
return o1.getCharacter().compareTo(o2.getCharacter());
}
}
private static class Room {
private String name;
private Integer sectorId;
private String checksum;
public Room(String raw) {
Pattern pattern = Pattern.compile("^([a-z-]+)-(\\d+)\\[([a-z]+)]$");
Matcher matcher = pattern.matcher(raw.trim());
if (matcher.find()) {
this.name = matcher.group(1);
this.sectorId = Integer.parseInt(matcher.group(2));
this.checksum = matcher.group(3);
}
}
public boolean isValid() {
HashMap<Character, Integer> letters = new HashMap<>();
String tempName = name.replace("-", "");
for (int i = 0; i < tempName.length(); i++) {
char c = tempName.charAt(i);
if (letters.containsKey(c)) {
letters.put(c, letters.get(c) + 1);
} else {
letters.put(c, 1);
}
}
TreeSet<Letter> toBeSorted = new TreeSet<Letter>(new Letter('a', 0));
for (Map.Entry<Character, Integer> entry: letters.entrySet()) {
toBeSorted.add(new Letter(entry.getKey(), entry.getValue()));
}
int i = 0;
for (Letter l: toBeSorted) {
if (this.checksum.charAt(i++) != l.getCharacter()) {
return false;
}
if (i >= 5) {
return true;
}
}
return false;
}
public Integer getSectorId(){
return sectorId;
}
public String toString() {
return this.name + " - " + this.sectorId + " - " + this.checksum;
}
public String decodeRoomName() {
StringBuilder out = new StringBuilder();
for (int i = 0; i < this.name.length(); i++) {
char c = this.name.charAt(i);
if (c == '-') {
out.append(' ');
continue;
}
Integer a = (int) c;
a -= (int) 'a';
a += this.getSectorId() % 26;
a = a % 26;
a += (int) 'a';
out.append((char)a.byteValue());
}
return out.toString();
}
}
public static void main(String[] args) throws FileNotFoundException {
ArrayList<String> lines = Files.readByLines("src/main/java/y2016/d04/in.txt");
Integer sum = 0;
for (String line: lines) {
Room room = new Room(line);
System.out.println(room.decodeRoomName() + " " + room.getSectorId());
if (room.isValid()) {
sum += room.getSectorId();
}
}
System.out.println(sum);
}
}