summaryrefslogtreecommitdiff
path: root/day02/day02.java
blob: eb36ae9ca7b4466de33d6b8ad48a3b3de593a430 (plain)
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
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class day02 {
  public static void main(String args[]) throws IOException {
    RandomAccessFile input = new RandomAccessFile("input", "r");
    System.out.println("Day 02 Part 1: " + part1(input));
    input.seek(0);
    System.out.println("Day 02 Part 2: " + part2(input));
    input.close();
  }

  public static String part1(RandomAccessFile input) throws IOException {
    int tot = 0;
    String line;

    while ((line = input.readLine()) != null) {
      tot += gamePossible(line);
    }

    return Integer.toString(tot);
  }

  private static int gamePossible(String line) {
    Pattern idpat = Pattern.compile("Game (\\d+):");
    Pattern gamepat = Pattern.compile("(\\d+) (red|green|blue)(, (\\d+) (red|green|blue))*(;|\\Z)");
    Pattern drawpat = Pattern.compile("(\\d+) (red|green|blue)");

    Matcher idmat = idpat.matcher(line);
    idmat.find();
    int id = Integer.parseInt(idmat.group(1));

    boolean valid = true;

    Matcher gamemat = gamepat.matcher(line);
    while (gamemat.find()) {
      Matcher drawmat = drawpat.matcher(line.substring(gamemat.start(), gamemat.end()));
      while (drawmat.find()) {
        int num = Integer.parseInt(drawmat.group(1));
        switch (drawmat.group(2)) {
          case "red":
            if (num > 12) {
              valid = false;
            }
            break;
          case "green":
            if (num > 13) {
              valid = false;
            }
            break;
          case "blue":
            if (num > 14) {
              valid = false;
            }
        }
      }
    }

    return valid ? id : 0;
  }

  public static String part2(RandomAccessFile input) throws IOException {
    int tot = 0;
    String line;

    while ((line = input.readLine()) != null) {
      tot += gamePower(line);
    }

    return Integer.toString(tot);
  }

  private static int gamePower(String line) {
    Pattern idpat = Pattern.compile("Game (\\d+):");
    Pattern gamepat = Pattern.compile("(\\d+) (red|green|blue)(, (\\d+) (red|green|blue))*(;|\\Z)");
    Pattern drawpat = Pattern.compile("(\\d+) (red|green|blue)");

    Matcher idmat = idpat.matcher(line);
    idmat.find();
    int id = Integer.parseInt(idmat.group(1));

    int red = 0, green = 0, blue = 0;

    Matcher gamemat = gamepat.matcher(line);
    while (gamemat.find()) {
      Matcher drawmat = drawpat.matcher(line.substring(gamemat.start(), gamemat.end()));
      while (drawmat.find()) {
        int num = Integer.parseInt(drawmat.group(1));
        switch (drawmat.group(2)) {
          case "red":
            if (num > red) {
              red = num;
            }
            break;
          case "green":
            if (num > green) {
              green = num;
            }
            break;
          case "blue":
            if (num > blue) {
              blue = num;
            }
        }
      }
    }

    return red * blue * green;
  }
}