summaryrefslogtreecommitdiff
path: root/years/2015.cpp
blob: b894d55c02aa1651e6e5c6396133bf6a6979678b (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
166
167
168
169
#include "../aoc.h"
#include <algorithm>
#include <iostream>
#include <set>
#include <sstream>
#include <vector>

std::vector<std::string> splitstring(std::string input, char splitter) {
  std::stringstream splitterstream(input);
  std::string segment;
  std::vector<std::string> splatstring;

  while (std::getline(splitterstream, segment, splitter)) {
    splatstring.push_back(segment);
  }

  return splatstring;
}

template <typename T>
bool vectorcontains(const std::vector<std::tuple<T, T>> &v,
                    const std::tuple<T, T> &x) {
  return std::find(v.begin(), v.end(), x) != v.end();
}

int y2015_q01_p01(std::string instructions) {
  int floor = 0;

  for (const char &c : instructions) {
    floor += c == '(' ? 1 : -1;
  }

  return floor;
}

int y2015_q01_p02(std::string instructions) {
  int floor = 0;

  for (std::size_t i = 0; i < instructions.size(); i++) {
    floor += instructions[i] == '(' ? 1 : -1;
    if (floor == -1)
      return i + 1;
  }

  return -1;
}

int y2015_q02_p01_instance(int l, int w, int h) {
  return std::min(l * w, std::min(l * h, h * w)) + 2 * l * w + 2 * l * h +
         2 * w * h;
}

int y2015_q02_p01() {
  int sum = 0;

  std::string input;
  getline(std::cin, input);

  while (input != "-1") {
    std::vector<std::string> split = splitstring(input, 'x');
    sum += y2015_q02_p01_instance(std::stoi(split[0]), std::stoi(split[1]),
                                  std::stoi(split[2]));

    getline(std::cin, input);
  }

  return sum;
}

int y2015_q02_p02_instance(int l, int w, int h) {
  return std::min(2 * (l + w), std::min(2 * (l + h), 2 * (w + h))) + l * w * h;
}

int y2015_q02_p02() {
  int sum = 0;

  std::string input;
  getline(std::cin, input);

  while (input != "-1") {
    std::vector<std::string> split = splitstring(input, 'x');
    sum += y2015_q02_p02_instance(std::stoi(split[0]), std::stoi(split[1]),
                                  std::stoi(split[2]));

    getline(std::cin, input);
  }

  return sum;
}

int y2015_q03_p01() {
  std::string input;
  getline(std::cin, input);

  std::set<std::tuple<int, int>> board;
  board.insert({0, 0});
  int x, y;

  for (const char &c : input) {
    switch (c) {
    case '>':
      x++;
      break;
    case '<':
      x--;
      break;
    case '^':
      y++;
      break;
    case 'v':
      y--;
      break;
    }
    board.insert({x, y});
  }

  return static_cast<int>(board.size());
}

int y2015_q03_p02() {
  std::string input;
  std::cout << "Warning! This input might need to be hardcoded depending on "
               "your terminal's input length limit.";
  getline(std::cin, input);

  std::set<std::tuple<int, int>> board;
  board.insert({0, 0});
  bool bot = false;
  int x = 0, y = 0, x2 = 0, y2 = 0;

  for (const char &c : input) {
    if (bot) {
      switch (c) {
      case '>':
        x2++;
        break;
      case '<':
        x2--;
        break;
      case '^':
        y2++;
        break;
      case 'v':
        y2--;
        break;
      }
      board.insert({x2, y2});
    } else {
      switch (c) {
      case '>':
        x++;
        break;
      case '<':
        x--;
        break;
      case '^':
        y++;
        break;
      case 'v':
        y--;
        break;
      }
      board.insert({x, y});
    }
    bot = !bot;
  }

  return static_cast<int>(board.size());
}