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

namespace aoc2022 {

struct build_result {
  int products[4] = {0, 0, 0, 0}; // ore,clay,obsi,geod
  int robots[4] = {1, 0, 0, 0};   // ore,clay,obsi,geod
  friend bool operator<(build_result r1, build_result r2) { return r1.products[3] < r2.products[3]; }
};

void print_result(int i, int m, const build_result& r) {
  printf("%d %02d| ore[%d] clay[%d] obsi[%d] geod[%d]\n", i, m, r.products[0], r.products[1], r.products[2],
         r.products[3]);
  printf("%d %02d| robots: %d %d %d %d\n", i, m, r.robots[0], r.robots[1], r.robots[2], r.robots[3]);
}

bool can_build(int i, const build_result& r, const blueprint& b) {
  switch (i) {
  case 0:
    return r.products[0] >= b.c_ore_r;
  case 1:
    return r.products[0] >= b.c_clay_r;
  case 2:
    return r.products[0] >= b.c_obsi_r[0] && r.products[1] >= b.c_obsi_r[1];
  case 3:
    return r.products[0] >= b.c_geod_r[0] && r.products[2] >= b.c_geod_r[1];
  default:
    break;
  }
  return false;
}

build_result build_robot(int i, build_result r, const blueprint& b) {
  switch (i) {
  case 0:
    r.products[0] -= b.c_ore_r;
    r.robots[0] += 1;
    break;
  case 1:
    r.products[0] -= b.c_clay_r;
    r.robots[1] += 1;
    break;
  case 2:
    r.products[0] -= b.c_obsi_r[0];
    r.products[1] -= b.c_obsi_r[1];
    r.robots[2] += 1;
    break;
  case 3:
    r.products[0] -= b.c_geod_r[0];
    r.products[2] -= b.c_geod_r[1];
    r.robots[3] += 1;
    break;
  default:
    break;
  }
  return r;
}

void build_product(build_result& r, int x) {
  for (int i = 0; i < 4; i++) {
    r.products[i] += r.robots[i] - ((int)i == x);
  }
}

int to_build(const blueprint& b, const build_result& r) {
  int i = 3;
  return i;
}

void build(int m, const blueprint& b, build_result r, build_result& max) {
  if (m > 0) {
    // print_result(b.idx, 24 - m, r);
    int i = to_build(b, r);
    if (i != INT32_MAX) {
      auto r0 = build_robot(i, r, b);
      build_product(r0, i);
      build(m - 1, b, r0, max);
    } else {
      build_product(r, 5);
      build(m - 1, b, r, max);
    }
  } else {
    if (max < r) {
      max = r;
    }
  }
}

std::pair<int, int> day19(line_view file) {
  std::vector<blueprint> bs;
  per_line(file, [&bs](line_view lv) {
    bs.emplace_back(lv);
    return true;
  });

  std::vector<build_result> rs;
  for (auto& b : bs) {
    build_result r;
    build_result m;
    // b.print();
    build(24, b, r, m);
    print_result(b.idx, 24, m);
    rs.push_back(m);
  }

  return {0, 0};
}
} // namespace aoc2022