summaryrefslogtreecommitdiff
path: root/day06/day06.java
blob: 49655018988c3f8b5fde43f8c268822fec7957dc (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
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

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

  public static String part1(RandomAccessFile input) throws IOException {
    ArrayList<Integer[]> races = new ArrayList<>();
    Pattern digits = Pattern.compile("\\d+");
    Matcher times = digits.matcher(input.readLine());
    Matcher distances = digits.matcher(input.readLine());

    while (times.find()) {
      distances.find();
      races.add(new Integer[]{
        Integer.parseInt(times.group()),
        Integer.parseInt(distances.group())
      });
    }

    int margin = 1;
    for (Integer[] race: races) {
      int wins = 0;
      for (int hold = 1; hold < race[0]; hold++) {
        int dist = (race[0] - hold) * hold;
        if (dist > race[1]) {
          wins += 1;
        }
      }
      margin *= wins;
    }

    return Integer.toString(margin);
  }

  public static String part2(RandomAccessFile input) throws IOException {
    long time = Long.parseLong(input.readLine().replaceAll(" ", "").substring(5));
    long distance = Long.parseLong(input.readLine().replaceAll(" ", "").substring(9));

    int wins = 0;
    for (long hold = 1; hold < time; hold++) {
      long dist = (time - hold) * hold;
      if (dist > distance) {
        wins += 1;
      }
    }

    return Integer.toString(wins);
  }
}