Skip to content

Commit 0cfda31

Browse files
committed
day 10 part 1 solution
1 parent 81e7e14 commit 0cfda31

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

10/1.cpp

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include <string>
4+
#include <vector>
5+
6+
7+
int main( int argc, char * argv[] )
8+
{
9+
if( argc < 2 )
10+
return 0;
11+
12+
std::ifstream ifs( argv[ 1 ] );
13+
14+
uint64_t sum( 0 );
15+
16+
while( !ifs.eof() )
17+
{
18+
std::string s;
19+
ifs >> s;
20+
21+
std::vector< char > v;
22+
for( char const c : s )
23+
{
24+
bool broken( false );
25+
switch( c )
26+
{
27+
case '(':
28+
v.push_back( ')' ); break;
29+
case '[':
30+
v.push_back( ']' ); break;
31+
case '{':
32+
v.push_back( '}' ); break;
33+
case '<':
34+
v.push_back( '>' ); break;
35+
36+
default:
37+
if( c != v.back() )
38+
broken = true;
39+
else
40+
v.pop_back();
41+
break;
42+
}
43+
44+
if( broken )
45+
{
46+
switch( c )
47+
{
48+
case ')': sum += 3; break;
49+
case ']': sum += 57; break;
50+
case '}': sum += 1197; break;
51+
case '>': sum += 25137; break;
52+
default: break;
53+
}
54+
break;
55+
}
56+
}
57+
}
58+
59+
std::cout << "result = " << sum << std::endl;
60+
61+
return 0;
62+
}

0 commit comments

Comments
 (0)