-
Notifications
You must be signed in to change notification settings - Fork 0
/
part1.cpp
57 lines (46 loc) · 1.19 KB
/
part1.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
#include "Day18.hpp"
using namespace std;
static string get_next_row(string &row);
static char get_tile(char left, char right);
static bool is_trap(char left, char right);
Result Day18::solve_p1(){
int safe_tiles = 0;
for(char ch: data){
if( ch == '.' ){
++safe_tiles;
}
}
string row = data;
for(unsigned ii = 1; ii < 40; ++ii){
row = get_next_row(row);
for(char ch: row){
if( ch == '.' ){
++safe_tiles;
}
}
}
return {true, to_string(safe_tiles)};
}
static string get_next_row(string &row){
// result = "" + get_tile(..) didn't work (compiler just discarded the tile)
string result = "";
result += get_tile('.', row[1]);
for(unsigned ii = 0; ii < row.length() - 2; ++ii){
result += get_tile(row[ii], row[ii+2]);
}
result += get_tile(row[row.length() - 2], '.');
return result;
}
static char get_tile(char left, char right){
if( is_trap(left, right) ){
return '^';
}
return '.';
}
static inline bool is_trap(char left, char right){
if ( left == '^' ){
return right == '.';
} else {
return right == '^';
}
}