-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday5.cpp
91 lines (84 loc) · 2.22 KB
/
day5.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
#include <array>
#include <numeric>
#include <sstream>
#include <vector>
#include <numeric>
#include "AOC_Solver.h"
uint64_t aoc::day5::part_1(std::vector<std::string>& input)
{
int max = 0,lnr=0;
std::vector<std::array<int,4>>lines{};
lines.resize(input.size());
for (auto& line : input){
std::stringstream stream(line);
std::array<int, 4>x{};
for (int k, j = 0; stream >> k;) {
x[j++] = k;
if (k > max)max = k;
if (stream.peek() == ',')
stream.ignore();
else if (stream.peek() == ' ')
stream.ignore(4);
}
lines[lnr++]=x;
}
++max;
std::vector<int>board{};
board.resize(max * max);
for (auto& l : lines) {
int x1 = l[0], y1 = l[1], x2 = l[2], y2 = l[3];
if (x1 == x2) {
int ly = y1 < y2 ? y1 : y2, uy = y1 < y2 ? y2 : y1;
for (int y = ly; y <= uy; y++)
board[x1 + y * max] += 1;
}
else if (y1 == y2) {
int lx = x1 < x2 ? x1 : x2, ux = x1 < x2 ? x2 : x1;
for (int x = lx; x <= ux; x++)
board[x + y1 * max] += 1;
}
}
return std::accumulate(board.begin(), board.end(), 0, [](int a, int b) {return a + (b > 1); });
}
uint64_t aoc::day5::part_2(std::vector<std::string>& input)
{
int max = 0, lnr = 0;
std::vector<std::array<int, 4>>lines{};
lines.resize(input.size());
for (auto& line:input){
std::stringstream stream(line);
std::array<int, 4>x{};
for (int k, j = 0; stream >> k;) {
x[j++] = k;
if (k > max)max = k;
if (stream.peek() == ',')
stream.ignore();
else if (stream.peek() == ' ')
stream.ignore(4);
}
lines[lnr++] = x;
}
++max;
std::vector<int>board{};
board.resize(max * max);
for (auto& l : lines) {
int x1 = l[0], y1 = l[1], x2 = l[2], y2 = l[3];
if (x1 == x2) {
int ly = y1 < y2 ? y1 : y2, uy = y1 < y2 ? y2 : y1;
for (int y = ly; y <= uy; y++)
board[x1 + y * max] += 1;
}
else if (y1 == y2) {
int lx = x1 < x2 ? x1 : x2, ux = x1 < x2 ? x2 : x1;
for (int x = lx; x <= ux; x++)
board[x + y1 * max] += 1;
}
else {
int step_x = x1<x2?1:-1,step_y = y1<y2?1:-1;
int lx = x1 < x2 ? x1 : x2,ux = x1 < x2 ? x2 : x1;
for (int i = 0; lx+i <= ux; i++)
board[(x1 + i*step_x) + (y1+i*step_y) * max] += 1;
}
}
return std::accumulate(board.begin(), board.end(), 0, [](int a, int b) {return a + (b > 1); });
}