summaryrefslogtreecommitdiff
path: root/day08/day08.java
blob: 34f9ed138c5a62069e5fc366586e363b7f0b8ea1 (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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

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

  public static String part1(RandomAccessFile input) throws IOException {
    char[] instr = input.readLine().toCharArray();
    input.readLine();

    Pattern nodepat = Pattern.compile("[A-Z]{3}");

    HashMap<String, Node> nodes = new HashMap<>();

    String line;
    while ((line = input.readLine()) != null) {
      Matcher mat = nodepat.matcher(line);
      mat.find();
      String name = mat.group();
      mat.find();
      String left = mat.group();
      mat.find();
      String right = mat.group();

      Node node;
      if (nodes.containsKey(name)) {
        node = nodes.get(name);
      } else {
        node = new Node(name);
        nodes.put(name, node);
      }
      if (nodes.containsKey(left)) {
        node.left = nodes.get(left);
      } else {
        node.left = new Node(left);
        nodes.put(left, node.left);
      }
      if (nodes.containsKey(right)) {
        node.right = nodes.get(right);
      } else {
        node.right = new Node(right);
        nodes.put(right, node.right);
      }
    }

    Node at = nodes.get("AAA");
    int count = 0, ip = 0;
    while (!(at.name.equals("ZZZ"))) {
      if (instr[ip] == 'L') {
        at = at.left;
      } else {
        at = at.right;
      }
      count += 1;
      ip += 1;
      ip %= instr.length;
    }

    return Integer.toString(count);
  }

  public static String part2(RandomAccessFile input) throws IOException {
    char[] instr = input.readLine().toCharArray();
    input.readLine();

    Pattern nodepat = Pattern.compile("[A-Z]{3}");

    HashMap<String, Node> nodes = new HashMap<>();

    String line;
    while ((line = input.readLine()) != null) {
      Matcher mat = nodepat.matcher(line);
      mat.find();
      String name = mat.group();
      mat.find();
      String left = mat.group();
      mat.find();
      String right = mat.group();

      Node node;
      if (nodes.containsKey(name)) {
        node = nodes.get(name);
      } else {
        node = new Node(name);
        nodes.put(name, node);
      }
      if (nodes.containsKey(left)) {
        node.left = nodes.get(left);
      } else {
        node.left = new Node(left);
        nodes.put(left, node.left);
      }
      if (nodes.containsKey(right)) {
        node.right = nodes.get(right);
      } else {
        node.right = new Node(right);
        nodes.put(right, node.right);
      }
    }

    ArrayList<Node> ats = new ArrayList<>();
    for (String name: nodes.keySet()) {
      if (name.charAt(2) == 'A') {
        ats.add(nodes.get(name));
      }
    }

    int count = 0, ip = 0;
    boolean allatz = false;
    while (!allatz) {
      if (instr[ip] == 'L') {
        for (int i = 0; i < ats.size(); i++) {
          ats.set(i, ats.get(i).left);
        }
      } else {
        for (int i = 0; i < ats.size(); i++) {
          ats.set(i, ats.get(i).right);
        }
      }
      count += 1;
      ip += 1;
      ip %= instr.length;
      allatz = true;
      for (Node at: ats) {
        if (!(at.name.charAt(2) == 'Z')) {
          allatz = false;
        }
      }
    }

    return Integer.toString(count);
  }
}

class Node {
  String name;
  Node left, right;

  public Node(String name) {
    this.name = name;
    this.left = null;
    this.right = null;
  }

  public String toString() {
    return this.name + " = (" + this.left.name + ", " + this.right.name + ")";
  }
}