-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay14.cpp
More file actions
90 lines (73 loc) · 2.39 KB
/
Day14.cpp
File metadata and controls
90 lines (73 loc) · 2.39 KB
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
#include "Day14.h"
string Day14::HexToBinary(const std::string& hex)
{
string bin;
for_each(hex.begin(), hex.end(), [&](const char c) {bin.append(_hexLookup.at(toupper(c))); });
return bin;
}
void Day14::AssignGroupToCell(vector<Cell>& cells, Cell& cell, int currentGroup)
{
// If this cell is not visible or has already been assigned a group, leave the method
if (!cell.Visible || cell.GroupChecked)
return;
// If this cell is visible, mark it with the current group number
cell.Group = currentGroup;
// Mark the cell as checked
cell.GroupChecked = true;
// Check all neighbors (excluding diagonals)
// Left neighbor
if (cell.Index % _width != 0) // check if left neighbor exists
AssignGroupToCell(cells, cells[cell.Index - 1], currentGroup);
// Upper neighbor
if (cell.Index >= _width) // check if upper neighbor exists
AssignGroupToCell(cells, cells[cell.Index - _width], currentGroup);
// Right neighbor
if ((cell.Index % _width) != 1) // check if right neighbor exists
AssignGroupToCell(cells, cells[cell.Index + 1], currentGroup);
// Lower neighbor
if (cell.Index < _width * (_width - 1)) // check if lower neighbor exists
AssignGroupToCell(cells, cells[cell.Index + _width], currentGroup);
}
void Day14::Part1()
{
//const string input = "flqrgnkx";
const string input = "ljoxqyyw";
int used = 0;
for (int i = 0; i < 128; ++i)
{
string hash = Day10().KnotHash(input + '-' + to_string(i));
string binary = HexToBinary(hash);
used += count_if(binary.begin(), binary.end(), [](const char c) {return c == '1'; });
}
cout << "Day 14 Part 1 answer: " << used << endl;
}
void Day14::Part2()
{
vector <Cell> cells{};
cells.reserve(_width * _width);
//const string input = "flqrgnkx";
const string input = "ljoxqyyw";
// Fill the cells
size_t idxCtr = 0;
for (size_t i = 0; i < _width; ++i)
{
string hash = Day10().KnotHash(input + '-' + to_string(i));
string binary = HexToBinary(hash);
for (const char c : binary)
{
cells.push_back(Cell(c == '1', idxCtr));
++idxCtr;
}
}
// Assign groups
int currentGroup = 1;
for (Cell& cell : cells)
{
// Ignore extra calls for cells that are not visible or already checked even if they would cancel their method immediately at start of it
if (!cell.Visible || cell.GroupChecked)
continue;
AssignGroupToCell(cells, cell, currentGroup);
++currentGroup;
}
cout << "Day 14 Part 2 answer: " << currentGroup << endl;
}