-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
114 lines (102 loc) · 3.69 KB
/
main.cpp
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
#include "file.h"
#include "utilities.h"
#include <iostream>
#include <vector>
#include <string>
#include <numeric>
#ifdef TESTING
#include <gtest/gtest.h>
#endif
namespace aoc2023_day03 {
bool isCharacter(char c) {
if (c >= '0' && c <= '9') {
return false;
}
if (c == '.') {
return false;
}
return true;
}
int part_1(std::string_view path) {
std::vector<std::string> input = file::readFileAsArrayString(path);
int sol = 0;
for (int i = 0; i < input.size(); ++i) {
int j = 0;
while (j < input[0].size()) {
int nr = 0;
bool x = true;
while (j < input[0].size() && input[i][j] >= '0' && input[i][j] <= '9') {
nr = nr * 10 + input[i][j] - '0';
std::vector<utils::point> dir = utils::getListOfNeighboursAllDirections(i, j, input.size(), input[0].size());
for (const auto& d : dir) {
if (isCharacter(input[i + d.x][j + d.y])) x = false;
}
j++;
}
j++;
if (!x) sol += nr;
}
}
return sol;
}
int part_2(std::string_view path) {
struct state{
int neighbours;
int score;
};
std::vector<std::string> input = file::readFileAsArrayString(path);
std::vector<std::vector<state>> neighbours(input.size(), std::vector<state>(input[0].size(), {0, 1}));
for (int i = 0; i < input.size(); ++i) {
int j = 0;
while (j < input[0].size()) {
int nr = 0;
int start = j;
while (j < input[0].size() && input[i][j] >= '0' && input[i][j] <= '9') {
nr = nr * 10 + input[i][j] - '0';
j++;
}
std::vector<std::vector<bool>> updated(input.size(), std::vector<bool>(input[0].size(), false));
for (int k = start; k < j; ++k) {
std::vector<utils::point> dir = utils::getListOfNeighboursAllDirections(i, k, input.size(), input[0].size());
for (const auto& d : dir) {
if (isCharacter(input[i + d.x][k + d.y]) && !updated[i + d.x][k + d.y]) {
neighbours[i + d.x][k + d.y].neighbours++;
neighbours[i + d.x][k + d.y].score *= nr;
updated[i + d.x][k + d.y] = true;
}
}
}
j++;
}
}
return std::accumulate(neighbours.begin(), neighbours.end(), 0, [](const auto& s, const auto& l) {
return std::accumulate(l.begin(), l.end(), s, [](const auto& s, const auto& n) {
if (n.neighbours == 2) {
return s + n.score;
}
return s;
});
});
}
}
#ifdef TESTING
TEST(Tests2023Day03, part_1_test) {
ASSERT_EQ(aoc2023_day03::part_1("../2023/day03/input_test.in"), 4361);
}
TEST(Tests2023Day03, part_1_real_test) {
ASSERT_EQ(aoc2023_day03::part_1("../2023/day03/input.in"), 556057);
}
TEST(Tests2023Day03, part_2_test) {
ASSERT_EQ(aoc2023_day03::part_2("../2023/day03/input_test.in"), 467835);
}
TEST(Tests2023Day03, part_2_real_test) {
ASSERT_EQ(aoc2023_day03::part_2("../2023/day03/input.in"), 82824352);
}
#endif
#ifndef TESTING
int main() {
std::cout << "Part 1: " << aoc2023_day03::part_1("../2023/day03/input.in") << std::endl;
std::cout << "Part 2: " << aoc2023_day03::part_2("../2023/day03/input.in") << std::endl;
return 0;
}
#endif