Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void problem1(List<String> input) {
- long sum = 0;
- for (String line : input) {
- String[] values = line.replaceAll("-|\\[|\\]", " ").split(" ");
- int sector = Integer.valueOf(values[values.length - 2]);
- String checksum = values[values.length - 1];
- Map<Character, Chars> totals = Maps.newLinkedHashMap();
- for (int i = 0; i < values.length - 2; i++) {
- String value = values[i];
- for (char ch : value.toCharArray()) {
- Chars chars = totals.get(ch);
- if (chars == null) {
- chars = new Chars(ch, 0);
- totals.put(ch, chars);
- }
- chars.times++;
- }
- }
- List<Chars> chars = Lists.newArrayList(totals.values());
- chars.sort(null);
- boolean valid = true;
- for (int i = 0; i < checksum.length(); i++) {
- Chars ch = chars.get(i);
- if (!checksum.contains(String.valueOf(ch.ch))) {
- valid = false;
- break;
- }
- }
- if (valid) sum += sector;
- }
- System.out.println(sum);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement