-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14.cpp
174 lines (137 loc) · 5.02 KB
/
day14.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
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
170
171
172
173
/*
--- Day 14: Disk Defragmentation ---
Suddenly, a scheduled job activates the system's disk defragmenter. Were
the situation different, you might sit and watch it for a while, but today,
you just don't have that kind of time. It's soaking up valuable system
resources that are needed elsewhere, and so the only option is to help it
finish its task as soon as possible.
The disk in question consists of a 128x128 grid; each square of the grid is
either free or used. On this disk, the state of the grid is tracked by the
bits in a sequence of knot hashes.
A total of 128 knot hashes are calculated, each corresponding to a single
row in the grid; each hash contains 128 bits which correspond to individual
grid squares. Each bit of a hash indicates whether that square is free (0)
or used (1).
The hash inputs are a key string (your puzzle input), a dash, and a number
from 0 to 127 corresponding to the row. For example, if your key string
were flqrgnkx, then the first row would be given by the bits of the knot
hash of flqrgnkx-0, the second row from the bits of the knot hash of
flqrgnkx-1, and so on until the last row, flqrgnkx-127.
The output of a knot hash is traditionally represented by 32 hexadecimal
digits; each of these digits correspond to 4 bits, for a total of
4 * 32 = 128 bits. To convert to bits, turn each hexadecimal digit to its
equivalent binary value, high-bit first: 0 becomes 0000, 1 becomes 0001, e
becomes 1110, f becomes 1111, and so on; a hash that begins with a0c2017...
in hexadecimal would begin with 10100000110000100000000101110000... in
binary.
Continuing this process, the first 8 rows and columns for key flqrgnkx
appear as follows, using # to denote used squares, and . to denote free
ones:
##.#.#..-->
.#.#.#.#
....#.#.
#.#.##.#
.##.#...
##..#..#
.#...#..
##.#.##.-->
| |
V V
In this example, 8108 squares are used across the entire 128x128 grid.
Given your actual key string, how many squares are used?
--- Part Two ---
Now, all the defragmenter needs to know is the number of regions. A region
is a group of used squares that are all adjacent, not including diagonals.
Every used square is in exactly one region: lone used squares form their
own isolated regions, while several adjacent squares all count as a single
region.
In the example above, the following nine regions are visible, each marked
with a distinct digit:
11.2.3..-->
.1.2.3.4
....5.6.
7.8.55.9
.88.5...
88..5..8
.8...8..
88.8.88.-->
| |
V V
Of particular interest is the region marked 8; while it does not appear
contiguous in this small view, all of the squares marked 8 are connected
when considering the whole 128x128 grid. In total, in this example, 1242
regions are present.
How many regions are present given your key string?
*/
#include <iostream>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include "knothash.h"
#include "main.h"
using namespace std;
int mainfunc( istream& is, ostream& os, Part part ) {
string input;
getline( is, input );
assert( is );
set<pair<int,int> > array;
for ( unsigned row = 0; row < 128; row++ ) {
ostringstream rowKey;
rowKey << input;
rowKey << '-' << row;
vector<uint8_t> data;
for ( auto c : rowKey.str() ) {
data.push_back( c );
}
vector<uint8_t> hash = knotHash( data );
assert( hash.size() == 16 );
for ( unsigned i = 0; i < hash.size(); i ++ ) {
for ( unsigned bit = 0; bit < 8; bit ++ ) {
if ( ( 0x80 >> bit ) & hash[i] ) {
array.insert( { i*8 + bit, row } );
}
}
}
}
if ( part == Part::PART1 ) {
os << array.size() << endl;
} else {
unsigned numGroups = 0;
while( array.size() ) {
numGroups++;
set<pair<int,int> > neighbors;
auto b = array.begin();
neighbors.insert( *b );
array.erase( b );
while( neighbors.size() ) {
auto d = neighbors.begin();
int x = d->first;
int y = d->second;
neighbors.erase( d );
auto iter = array.find( { x+1, y } );
if ( iter != array.end() ) {
neighbors.insert( *iter );
array.erase( iter );
}
iter = array.find( { x, y+1 } );
if ( iter != array.end() ) {
neighbors.insert( *iter );
array.erase( iter );
}
iter = array.find( { x-1, y } );
if ( iter != array.end() ) {
neighbors.insert( *iter );
array.erase( iter );
}
iter = array.find( { x, y-1 } );
if ( iter != array.end() ) {
neighbors.insert( *iter );
array.erase( iter );
}
}
}
os << numGroups << endl;
}
return 0;
}