-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution10_1.h
87 lines (80 loc) · 1.91 KB
/
solution10_1.h
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
#ifndef AOC23_SOLUTION10_1_H
#define AOC23_SOLUTION10_1_H
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <ranges>
#include <algorithm>
#include <sstream>
#include <map>
#include <cstring>
enum direction {
N, E, S, W
};
direction step(direction current, char pipe) {
switch (current) {
case N:
switch (pipe) {
case '|': return N;
case '7': return W;
case 'F': return E;
}
case E:
switch (pipe) {
case '-': return E;
case 'J': return N;
case '7': return S;
}
case S:
switch (pipe) {
case '|': return S;
case 'L': return E;
case 'J': return W;
}
case W:
switch (pipe) {
case '-': return W;
case 'L': return N;
case 'F': return S;
}
}
}
void solution() {
std::ifstream file("inputs/10.txt");
if (!file.is_open()) {
std::cout << "Couldn't open file" << std::endl;
return;
}
char map[140][140];
int x, y;
std::string line;
for(int i = 0; getline( file, line );++i) {
for (int j = 0; j < line.size(); ++j) {
if (line[j] == 'S') {
x = j;
y = i;
}
}
std::strncpy(map[i], line.c_str(), 140);
}
direction current = N;
int steps = 1;
y--;
do {
current = step(current, map[y][x]);
switch (current) {
case N:
--y; break;
case E:
++x; break;
case S:
++y; break;
case W:
--x; break;
}
++steps;
} while (map[y][x] != 'S');
std::cout << "sum: " << steps/2 << std::endl;
}
#endif //AOC23_SOLUTION10_1_H