aboutsummaryrefslogtreecommitdiff
path: root/src/2022/day18/aoc.cpp
blob: 03b5249ab9d65a429f9e66dc29d76a037a67d058 (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
#include "aoc.h"
#include <deque>

namespace aoc2022 {

static int maxx = 0;
static int maxy = 0;
static int maxz = 0;

bool is_valid(droplet d, cube& c) {
  bool bx = d.x >= 0 && d.x < maxx + 1;
  bool by = d.y >= 0 && d.y < maxy + 1;
  bool bz = d.z >= 0 && d.z < maxz + 1;
  return bx && by && bz && c.get(d.x, d.y, d.z) == 0;
}

// flood
void flood(cube& c) {
  std::deque<droplet> q;
  c.get(0, 0, 0) = -1;
  q.push_back(droplet{0, 0, 0});

  while (!q.empty()) {
    auto s = q.size();
    while (s-- > 0) {
      auto d = q.front();
      q.pop_front();
      droplet ds[] = {
          {d.x + 1, d.y, d.z}, {d.x - 1, d.y, d.z}, {d.x, d.y + 1, d.z},
          {d.x, d.y - 1, d.z}, {d.x, d.y, d.z + 1}, {d.x, d.y, d.z - 1},
      };
      for (auto& dx : ds) {
        if (is_valid(dx, c)) {
          c.get(dx.x, dx.y, dx.z) = -1;
          q.push_back(dx);
        }
      }
    }
  }
}

std::pair<int, int> day18(line_view file) {
  std::vector<droplet> ds;
  per_line(file, [&ds](line_view lv) {
    droplet d{lv};
    maxx = std::max(maxx, d.x);
    maxy = std::max(maxy, d.y);
    maxz = std::max(maxz, d.z);

    ds.emplace_back(d);
    return true;
  });

  cube c{maxx + 1, maxy + 1, maxz + 1};
  for (auto& d : ds) {
    c.get(d.x, d.y, d.z) = 1;
  }

  flood(c);
  int t0{0}, t1{0};
  std::vector<droplet> dx;
  c.traverse([&t0, &dx, &c](int x, int y, int z) {
    if (c.get(x, y, z) == 1) {
      t0 += 6;
      t0 -= c.get(x - 1, y, z) == 1 ? 1 : 0;
      t0 -= c.get(x + 1, y, z) == 1 ? 1 : 0;
      t0 -= c.get(x, y - 1, z) == 1 ? 1 : 0;
      t0 -= c.get(x, y + 1, z) == 1 ? 1 : 0;
      t0 -= c.get(x, y, z - 1) == 1 ? 1 : 0;
      t0 -= c.get(x, y, z + 1) == 1 ? 1 : 0;
    }
    if (c.get(x, y, z) == 0) {
      dx.emplace_back(x, y, z);
    }
  });

  for (auto& d : dx) {
    c.get(d.x, d.y, d.z) = 1;
  }

  c.traverse([&t1, &c](int x, int y, int z) {
    if (c.get(x, y, z) == 1) {
      t1 += 6;
      t1 -= c.get(x - 1, y, z) == 1 ? 1 : 0;
      t1 -= c.get(x + 1, y, z) == 1 ? 1 : 0;
      t1 -= c.get(x, y - 1, z) == 1 ? 1 : 0;
      t1 -= c.get(x, y + 1, z) == 1 ? 1 : 0;
      t1 -= c.get(x, y, z - 1) == 1 ? 1 : 0;
      t1 -= c.get(x, y, z + 1) == 1 ? 1 : 0;
    }
  });
  // printf("%d %d\n", t0, t1);

  return {t0, t1};
}
} // namespace aoc2022