summaryrefslogtreecommitdiff
path: root/day11/day11.java
blob: 2fd9f8a5d3f463431febf960cf6304ba95f9de92 (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
159
160
161
162
163
164
165
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;

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

  public static String part1(RandomAccessFile input) throws IOException {
    ArrayList<ArrayList<Pixel>> image = new ArrayList<>();

    String line;
    while ((line = input.readLine()) != null) {
      ArrayList<Pixel> linea = new ArrayList<>();
      for (char ch: line.toCharArray()) {
        if (ch == '.') {
          linea.add(Pixel.Space);
        } else if (ch == '#') {
          linea.add(Pixel.Galaxy);
        }
      }
      image.add(linea);
    }

    for (int i = image.size() - 1; i >= 0; i--) {
      if (allSpace(image.get(i))) {
        image.add(i+1, (ArrayList<Pixel>) image.get(i).clone());
      }
    }

    for (int i = image.get(0).size() - 1; i >= 0; i--) {
      ArrayList<Pixel> col = new ArrayList<>();

      for (ArrayList<Pixel> row: image) {
        col.add(row.get(i));
      }

      if (allSpace(col)) {
        for (ArrayList<Pixel> row: image) {
          row.add(i+1, row.get(i));
        }
      }
    }

    ArrayList<Integer[]> galaxies = new ArrayList<>();
    for (int r = 0; r < image.size(); r++) {
      for (int c = 0; c < image.get(0).size(); c++) {
        if (image.get(r).get(c) == Pixel.Galaxy) {
          galaxies.add(new Integer[] {r, c});
        }
      }
    }

    int sum = 0;

    for (int i = 0; i < galaxies.size() - 1; i++) {
      for (int j = i + 1; j < galaxies.size(); j++) {
        sum += dist(galaxies.get(i), galaxies.get(j));
      }
    }

    return Integer.toString(sum);
  }

  public static String part2(RandomAccessFile input) throws IOException {
    ArrayList<Integer[]> galaxies = new ArrayList<>();

    String line;
    int h = 0;
    int w = 0;
    while ((line = input.readLine()) != null) {
      w = 0;
      for (char ch: line.toCharArray()) {
        if (ch == '#') {
          galaxies.add(new Integer[] {h, w});
        }
        w += 1;
      }
      h += 1;
    }

    for (int r = h - 1; r >= 0; r--) {
      if (rowEmpty(galaxies, r)) {
        moveDown(galaxies, r, 999999);
      }
    }

    for (int c = w - 1; c >= 0; c--) {
      if (colEmpty(galaxies, c)) {
        moveRight(galaxies, c, 999999);
      }
    }

    long sum = 0;

    for (int i = 0; i < galaxies.size() - 1; i++) {
      for (int j = i + 1; j < galaxies.size(); j++) {
        sum += longDist(galaxies.get(i), galaxies.get(j));
      }
    }

    return Long.toString(sum);
  }

  static enum Pixel {
    Space,
    Galaxy
  }

  private static boolean allSpace(ArrayList<Pixel> line) {
    for (Pixel px: line) {
      if (px == Pixel.Galaxy) {
        return false;
      }
    }
    return true;
  }

  private static int dist(Integer[] i, Integer[] j) {
    return Math.abs(i[0] - j[0]) + Math.abs(i[1] - j[1]);
  }

  private static long longDist(Integer[] i, Integer[] j) {
    return Math.abs(i[0] - j[0]) + Math.abs(i[1] - j[1]);
  }

  private static boolean rowEmpty(ArrayList<Integer[]> galaxies, int row) {
    for (Integer[] galaxy: galaxies) {
      if (galaxy[0] == row) {
        return false;
      }
    }
    return true;
  }

  private static void moveDown(ArrayList<Integer[]> galaxies, int row, int dist) {
    for (Integer[] galaxy: galaxies) {
      if (galaxy[0] > row) {
        galaxy[0] += dist;
      }
    }
  }

  private static boolean colEmpty(ArrayList<Integer[]> galaxies, int col) {
    for (Integer[] galaxy: galaxies) {
      if (galaxy[1] == col) {
        return false;
      }
    }
    return true;
  }

  private static void moveRight(ArrayList<Integer[]> galaxies, int col, int dist) {
    for (Integer[] galaxy: galaxies) {
      if (galaxy[1] > col) {
        galaxy[1] += dist;
      }
    }
  }
}