Added older advents
This commit is contained in:
parent
8db2505049
commit
9cf858b860
78 changed files with 13807 additions and 0 deletions
1
advent_of_code_2015/day1/input.in
Normal file
1
advent_of_code_2015/day1/input.in
Normal file
File diff suppressed because one or more lines are too long
18
advent_of_code_2015/day1/main1.cc
Normal file
18
advent_of_code_2015/day1/main1.cc
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
string buffer;
|
||||
|
||||
while(getline(cin, buffer))
|
||||
{
|
||||
int up = count(buffer.begin(), buffer.end(), '(');
|
||||
int down = count(buffer.begin(), buffer.end(), ')');
|
||||
|
||||
cout << "Floor: " << up - down << "\n";
|
||||
}
|
||||
}
|
||||
27
advent_of_code_2015/day1/main2.cc
Normal file
27
advent_of_code_2015/day1/main2.cc
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
string buffer;
|
||||
|
||||
while(getline(cin, buffer))
|
||||
{
|
||||
int floor = 0;
|
||||
|
||||
for (size_t idx = 0; idx != buffer.size(); ++idx)
|
||||
{
|
||||
floor += buffer[idx] == '(' ? 1 : -1;
|
||||
if (floor == -1)
|
||||
{
|
||||
cout << "Entered basement in step " << idx + 1 << "\n";
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
1000
advent_of_code_2015/day2/input.in
Normal file
1000
advent_of_code_2015/day2/input.in
Normal file
File diff suppressed because it is too large
Load diff
63
advent_of_code_2015/day2/main1.cc
Normal file
63
advent_of_code_2015/day2/main1.cc
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct Present
|
||||
{
|
||||
size_t length;
|
||||
size_t width;
|
||||
size_t height;
|
||||
|
||||
size_t extra_wrap()
|
||||
{
|
||||
return min(length * width, min(length * height, width * height));
|
||||
}
|
||||
|
||||
size_t wrap_required()
|
||||
{
|
||||
return 2 * length * width
|
||||
+ 2 * width * height
|
||||
+ 2 * height * length
|
||||
+ extra_wrap();
|
||||
}
|
||||
|
||||
size_t bow_size()
|
||||
{
|
||||
return length * width * height;
|
||||
}
|
||||
|
||||
size_t ribbon_required()
|
||||
{
|
||||
size_t ribbon = min(length + width, min(length + height, width + height)) * 2;
|
||||
return ribbon + bow_size();
|
||||
}
|
||||
};
|
||||
|
||||
istream &operator>>(istream& in, Present &present)
|
||||
{
|
||||
in >> present.length;
|
||||
in.ignore(1); // ignore 'x'.
|
||||
in >> present.width;
|
||||
in.ignore(1); // ignore 'x'.
|
||||
in >> present.height;
|
||||
|
||||
return in;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
size_t total_wrap = 0;
|
||||
size_t total_ribbon = 0;
|
||||
Present present;
|
||||
|
||||
while(!cin.eof())
|
||||
{
|
||||
cin >> present;
|
||||
total_wrap += present.wrap_required();
|
||||
total_ribbon += present.ribbon_required();
|
||||
}
|
||||
|
||||
cout << "Total wrap required: " << total_wrap << "\n";
|
||||
cout << "Total ribbon required: " << total_ribbon << "\n";
|
||||
}
|
||||
1
advent_of_code_2015/day3/input.in
Normal file
1
advent_of_code_2015/day3/input.in
Normal file
File diff suppressed because one or more lines are too long
53
advent_of_code_2015/day3/main1.cc
Normal file
53
advent_of_code_2015/day3/main1.cc
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
#include <iostream>
|
||||
#include <set>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Point
|
||||
{
|
||||
public:
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
|
||||
bool operator<(Point const &lhs, Point const &rhs)
|
||||
{
|
||||
if (lhs.x == rhs.x)
|
||||
return lhs.y < rhs.y;
|
||||
return lhs.x < rhs.x;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
set<Point> delivered;
|
||||
Point position{0, 0};
|
||||
delivered.insert(position);
|
||||
|
||||
while (!cin.eof())
|
||||
{
|
||||
switch (cin.get())
|
||||
{
|
||||
case '>':
|
||||
++position.x;
|
||||
break;
|
||||
|
||||
case '<':
|
||||
--position.x;
|
||||
break;
|
||||
|
||||
case '^':
|
||||
--position.y;
|
||||
break;
|
||||
|
||||
case 'v':
|
||||
++position.y;
|
||||
break;
|
||||
}
|
||||
|
||||
delivered.insert(position);
|
||||
}
|
||||
|
||||
cout << "Delivered count: " << delivered.size() << "\n";
|
||||
}
|
||||
56
advent_of_code_2015/day3/main2.cc
Normal file
56
advent_of_code_2015/day3/main2.cc
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
#include <iostream>
|
||||
#include <set>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Point
|
||||
{
|
||||
public:
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
|
||||
bool operator<(Point const &lhs, Point const &rhs)
|
||||
{
|
||||
if (lhs.x == rhs.x)
|
||||
return lhs.y < rhs.y;
|
||||
return lhs.x < rhs.x;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
bool robo = false;
|
||||
set<Point> delivered;
|
||||
Point position{0, 0};
|
||||
Point robo_position{0, 0};
|
||||
delivered.insert(position);
|
||||
|
||||
while (!cin.eof())
|
||||
{
|
||||
switch (cin.get())
|
||||
{
|
||||
case '>':
|
||||
++(robo ? robo_position : position).x;
|
||||
break;
|
||||
|
||||
case '<':
|
||||
--(robo ? robo_position : position).x;
|
||||
break;
|
||||
|
||||
case '^':
|
||||
--(robo ? robo_position : position).y;
|
||||
break;
|
||||
|
||||
case 'v':
|
||||
++(robo ? robo_position : position).y;
|
||||
break;
|
||||
}
|
||||
|
||||
robo = !robo;
|
||||
delivered.insert(robo ? robo_position : position);
|
||||
}
|
||||
|
||||
cout << "Delivered count: " << delivered.size() << "\n";
|
||||
}
|
||||
24
advent_of_code_2015/day4/main1.cc
Normal file
24
advent_of_code_2015/day4/main1.cc
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "md5.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
string prefix = "bgvyzdsv";
|
||||
|
||||
for (size_t idx = 0; ; ++idx)
|
||||
{
|
||||
string test = prefix + to_string(idx);
|
||||
|
||||
string hash = md5(test);
|
||||
if (hash.find_first_not_of('0') == 6) //5 for solution 1
|
||||
{
|
||||
cout << test << "\n";
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
362
advent_of_code_2015/day4/md5.cpp
Normal file
362
advent_of_code_2015/day4/md5.cpp
Normal file
|
|
@ -0,0 +1,362 @@
|
|||
/* MD5
|
||||
converted to C++ class by Frank Thilo (thilo@unix-ag.org)
|
||||
for bzflag (http://www.bzflag.org)
|
||||
|
||||
based on:
|
||||
|
||||
md5.h and md5.c
|
||||
reference implemantion of RFC 1321
|
||||
|
||||
Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
|
||||
rights reserved.
|
||||
|
||||
License to copy and use this software is granted provided that it
|
||||
is identified as the "RSA Data Security, Inc. MD5 Message-Digest
|
||||
Algorithm" in all material mentioning or referencing this software
|
||||
or this function.
|
||||
|
||||
License is also granted to make and use derivative works provided
|
||||
that such works are identified as "derived from the RSA Data
|
||||
Security, Inc. MD5 Message-Digest Algorithm" in all material
|
||||
mentioning or referencing the derived work.
|
||||
|
||||
RSA Data Security, Inc. makes no representations concerning either
|
||||
the merchantability of this software or the suitability of this
|
||||
software for any particular purpose. It is provided "as is"
|
||||
without express or implied warranty of any kind.
|
||||
|
||||
These notices must be retained in any copies of any part of this
|
||||
documentation and/or software.
|
||||
|
||||
*/
|
||||
|
||||
/* interface header */
|
||||
#include "md5.hpp"
|
||||
|
||||
/* system implementation headers */
|
||||
#include <cstdio>
|
||||
|
||||
|
||||
// Constants for MD5Transform routine.
|
||||
#define S11 7
|
||||
#define S12 12
|
||||
#define S13 17
|
||||
#define S14 22
|
||||
#define S21 5
|
||||
#define S22 9
|
||||
#define S23 14
|
||||
#define S24 20
|
||||
#define S31 4
|
||||
#define S32 11
|
||||
#define S33 16
|
||||
#define S34 23
|
||||
#define S41 6
|
||||
#define S42 10
|
||||
#define S43 15
|
||||
#define S44 21
|
||||
|
||||
///////////////////////////////////////////////
|
||||
|
||||
// F, G, H and I are basic MD5 functions.
|
||||
inline MD5::uint4 MD5::F(uint4 x, uint4 y, uint4 z) {
|
||||
return x&y | ~x&z;
|
||||
}
|
||||
|
||||
inline MD5::uint4 MD5::G(uint4 x, uint4 y, uint4 z) {
|
||||
return x&z | y&~z;
|
||||
}
|
||||
|
||||
inline MD5::uint4 MD5::H(uint4 x, uint4 y, uint4 z) {
|
||||
return x^y^z;
|
||||
}
|
||||
|
||||
inline MD5::uint4 MD5::I(uint4 x, uint4 y, uint4 z) {
|
||||
return y ^ (x | ~z);
|
||||
}
|
||||
|
||||
// rotate_left rotates x left n bits.
|
||||
inline MD5::uint4 MD5::rotate_left(uint4 x, int n) {
|
||||
return (x << n) | (x >> (32-n));
|
||||
}
|
||||
|
||||
// FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
|
||||
// Rotation is separate from addition to prevent recomputation.
|
||||
inline void MD5::FF(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac) {
|
||||
a = rotate_left(a+ F(b,c,d) + x + ac, s) + b;
|
||||
}
|
||||
|
||||
inline void MD5::GG(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac) {
|
||||
a = rotate_left(a + G(b,c,d) + x + ac, s) + b;
|
||||
}
|
||||
|
||||
inline void MD5::HH(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac) {
|
||||
a = rotate_left(a + H(b,c,d) + x + ac, s) + b;
|
||||
}
|
||||
|
||||
inline void MD5::II(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac) {
|
||||
a = rotate_left(a + I(b,c,d) + x + ac, s) + b;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////
|
||||
|
||||
// default ctor, just initailize
|
||||
MD5::MD5()
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////
|
||||
|
||||
// nifty shortcut ctor, compute MD5 for string and finalize it right away
|
||||
MD5::MD5(const std::string &text)
|
||||
{
|
||||
init();
|
||||
update(text.c_str(), text.length());
|
||||
finalize();
|
||||
}
|
||||
|
||||
//////////////////////////////
|
||||
|
||||
void MD5::init()
|
||||
{
|
||||
finalized=false;
|
||||
|
||||
count[0] = 0;
|
||||
count[1] = 0;
|
||||
|
||||
// load magic initialization constants.
|
||||
state[0] = 0x67452301;
|
||||
state[1] = 0xefcdab89;
|
||||
state[2] = 0x98badcfe;
|
||||
state[3] = 0x10325476;
|
||||
}
|
||||
|
||||
//////////////////////////////
|
||||
|
||||
// decodes input (unsigned char) into output (uint4). Assumes len is a multiple of 4.
|
||||
void MD5::decode(uint4 output[], const uint1 input[], size_type len)
|
||||
{
|
||||
for (unsigned int i = 0, j = 0; j < len; i++, j += 4)
|
||||
output[i] = ((uint4)input[j]) | (((uint4)input[j+1]) << 8) |
|
||||
(((uint4)input[j+2]) << 16) | (((uint4)input[j+3]) << 24);
|
||||
}
|
||||
|
||||
//////////////////////////////
|
||||
|
||||
// encodes input (uint4) into output (unsigned char). Assumes len is
|
||||
// a multiple of 4.
|
||||
void MD5::encode(uint1 output[], const uint4 input[], size_type len)
|
||||
{
|
||||
for (size_type i = 0, j = 0; j < len; i++, j += 4) {
|
||||
output[j] = input[i] & 0xff;
|
||||
output[j+1] = (input[i] >> 8) & 0xff;
|
||||
output[j+2] = (input[i] >> 16) & 0xff;
|
||||
output[j+3] = (input[i] >> 24) & 0xff;
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////
|
||||
|
||||
// apply MD5 algo on a block
|
||||
void MD5::transform(const uint1 block[blocksize])
|
||||
{
|
||||
uint4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
|
||||
decode (x, block, blocksize);
|
||||
|
||||
/* Round 1 */
|
||||
FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
|
||||
FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
|
||||
FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
|
||||
FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
|
||||
FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
|
||||
FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
|
||||
FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
|
||||
FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
|
||||
FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
|
||||
FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
|
||||
FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
|
||||
FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
|
||||
FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
|
||||
FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
|
||||
FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
|
||||
FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
|
||||
|
||||
/* Round 2 */
|
||||
GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
|
||||
GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
|
||||
GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
|
||||
GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
|
||||
GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
|
||||
GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
|
||||
GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
|
||||
GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
|
||||
GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
|
||||
GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
|
||||
GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
|
||||
GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
|
||||
GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
|
||||
GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
|
||||
GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
|
||||
GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
|
||||
|
||||
/* Round 3 */
|
||||
HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
|
||||
HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
|
||||
HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
|
||||
HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
|
||||
HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
|
||||
HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
|
||||
HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
|
||||
HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
|
||||
HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
|
||||
HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
|
||||
HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
|
||||
HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
|
||||
HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
|
||||
HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
|
||||
HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
|
||||
HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
|
||||
|
||||
/* Round 4 */
|
||||
II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
|
||||
II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
|
||||
II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
|
||||
II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
|
||||
II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
|
||||
II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
|
||||
II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
|
||||
II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
|
||||
II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
|
||||
II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
|
||||
II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
|
||||
II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
|
||||
II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
|
||||
II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
|
||||
II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
|
||||
II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
|
||||
|
||||
state[0] += a;
|
||||
state[1] += b;
|
||||
state[2] += c;
|
||||
state[3] += d;
|
||||
|
||||
// Zeroize sensitive information.
|
||||
memset(x, 0, sizeof x);
|
||||
}
|
||||
|
||||
//////////////////////////////
|
||||
|
||||
// MD5 block update operation. Continues an MD5 message-digest
|
||||
// operation, processing another message block
|
||||
void MD5::update(const unsigned char input[], size_type length)
|
||||
{
|
||||
// compute number of bytes mod 64
|
||||
size_type index = count[0] / 8 % blocksize;
|
||||
|
||||
// Update number of bits
|
||||
if ((count[0] += (length << 3)) < (length << 3))
|
||||
count[1]++;
|
||||
count[1] += (length >> 29);
|
||||
|
||||
// number of bytes we need to fill in buffer
|
||||
size_type firstpart = 64 - index;
|
||||
|
||||
size_type i;
|
||||
|
||||
// transform as many times as possible.
|
||||
if (length >= firstpart)
|
||||
{
|
||||
// fill buffer first, transform
|
||||
memcpy(&buffer[index], input, firstpart);
|
||||
transform(buffer);
|
||||
|
||||
// transform chunks of blocksize (64 bytes)
|
||||
for (i = firstpart; i + blocksize <= length; i += blocksize)
|
||||
transform(&input[i]);
|
||||
|
||||
index = 0;
|
||||
}
|
||||
else
|
||||
i = 0;
|
||||
|
||||
// buffer remaining input
|
||||
memcpy(&buffer[index], &input[i], length-i);
|
||||
}
|
||||
|
||||
//////////////////////////////
|
||||
|
||||
// for convenience provide a verson with signed char
|
||||
void MD5::update(const char input[], size_type length)
|
||||
{
|
||||
update((const unsigned char*)input, length);
|
||||
}
|
||||
|
||||
//////////////////////////////
|
||||
|
||||
// MD5 finalization. Ends an MD5 message-digest operation, writing the
|
||||
// the message digest and zeroizing the context.
|
||||
MD5& MD5::finalize()
|
||||
{
|
||||
static unsigned char padding[64] = {
|
||||
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||
};
|
||||
|
||||
if (!finalized) {
|
||||
// Save number of bits
|
||||
unsigned char bits[8];
|
||||
encode(bits, count, 8);
|
||||
|
||||
// pad out to 56 mod 64.
|
||||
size_type index = count[0] / 8 % 64;
|
||||
size_type padLen = (index < 56) ? (56 - index) : (120 - index);
|
||||
update(padding, padLen);
|
||||
|
||||
// Append length (before padding)
|
||||
update(bits, 8);
|
||||
|
||||
// Store state in digest
|
||||
encode(digest, state, 16);
|
||||
|
||||
// Zeroize sensitive information.
|
||||
memset(buffer, 0, sizeof buffer);
|
||||
memset(count, 0, sizeof count);
|
||||
|
||||
finalized=true;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
//////////////////////////////
|
||||
|
||||
// return hex representation of digest as string
|
||||
std::string MD5::hexdigest() const
|
||||
{
|
||||
if (!finalized)
|
||||
return "";
|
||||
|
||||
char buf[33];
|
||||
for (int i=0; i<16; i++)
|
||||
sprintf(buf+i*2, "%02x", digest[i]);
|
||||
buf[32]=0;
|
||||
|
||||
return std::string(buf);
|
||||
}
|
||||
|
||||
//////////////////////////////
|
||||
|
||||
std::ostream& operator<<(std::ostream& out, MD5 md5)
|
||||
{
|
||||
return out << md5.hexdigest();
|
||||
}
|
||||
|
||||
//////////////////////////////
|
||||
|
||||
std::string md5(const std::string str)
|
||||
{
|
||||
MD5 md5 = MD5(str);
|
||||
|
||||
return md5.hexdigest();
|
||||
}
|
||||
93
advent_of_code_2015/day4/md5.hpp
Normal file
93
advent_of_code_2015/day4/md5.hpp
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
/* MD5
|
||||
converted to C++ class by Frank Thilo (thilo@unix-ag.org)
|
||||
for bzflag (http://www.bzflag.org)
|
||||
|
||||
based on:
|
||||
|
||||
md5.h and md5.c
|
||||
reference implementation of RFC 1321
|
||||
|
||||
Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
|
||||
rights reserved.
|
||||
|
||||
License to copy and use this software is granted provided that it
|
||||
is identified as the "RSA Data Security, Inc. MD5 Message-Digest
|
||||
Algorithm" in all material mentioning or referencing this software
|
||||
or this function.
|
||||
|
||||
License is also granted to make and use derivative works provided
|
||||
that such works are identified as "derived from the RSA Data
|
||||
Security, Inc. MD5 Message-Digest Algorithm" in all material
|
||||
mentioning or referencing the derived work.
|
||||
|
||||
RSA Data Security, Inc. makes no representations concerning either
|
||||
the merchantability of this software or the suitability of this
|
||||
software for any particular purpose. It is provided "as is"
|
||||
without express or implied warranty of any kind.
|
||||
|
||||
These notices must be retained in any copies of any part of this
|
||||
documentation and/or software.
|
||||
|
||||
*/
|
||||
|
||||
#ifndef BZF_MD5_H
|
||||
#define BZF_MD5_H
|
||||
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
// a small class for calculating MD5 hashes of strings or byte arrays
|
||||
// it is not meant to be fast or secure
|
||||
//
|
||||
// usage: 1) feed it blocks of uchars with update()
|
||||
// 2) finalize()
|
||||
// 3) get hexdigest() string
|
||||
// or
|
||||
// MD5(std::string).hexdigest()
|
||||
//
|
||||
// assumes that char is 8 bit and int is 32 bit
|
||||
class MD5
|
||||
{
|
||||
public:
|
||||
typedef unsigned int size_type; // must be 32bit
|
||||
|
||||
MD5();
|
||||
MD5(const std::string& text);
|
||||
void update(const unsigned char *buf, size_type length);
|
||||
void update(const char *buf, size_type length);
|
||||
MD5& finalize();
|
||||
std::string hexdigest() const;
|
||||
friend std::ostream& operator<<(std::ostream&, MD5 md5);
|
||||
|
||||
private:
|
||||
void init();
|
||||
typedef unsigned char uint1; // 8bit
|
||||
typedef unsigned int uint4; // 32bit
|
||||
enum {blocksize = 64}; // VC6 won't eat a const static int here
|
||||
|
||||
void transform(const uint1 block[blocksize]);
|
||||
static void decode(uint4 output[], const uint1 input[], size_type len);
|
||||
static void encode(uint1 output[], const uint4 input[], size_type len);
|
||||
|
||||
bool finalized;
|
||||
uint1 buffer[blocksize]; // bytes that didn't fit in last 64 byte chunk
|
||||
uint4 count[2]; // 64bit counter for number of bits (lo, hi)
|
||||
uint4 state[4]; // digest so far
|
||||
uint1 digest[16]; // the result
|
||||
|
||||
// low level logic operations
|
||||
static inline uint4 F(uint4 x, uint4 y, uint4 z);
|
||||
static inline uint4 G(uint4 x, uint4 y, uint4 z);
|
||||
static inline uint4 H(uint4 x, uint4 y, uint4 z);
|
||||
static inline uint4 I(uint4 x, uint4 y, uint4 z);
|
||||
static inline uint4 rotate_left(uint4 x, int n);
|
||||
static inline void FF(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
|
||||
static inline void GG(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
|
||||
static inline void HH(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
|
||||
static inline void II(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
|
||||
};
|
||||
|
||||
std::string md5(const std::string str);
|
||||
|
||||
#endif
|
||||
1000
advent_of_code_2015/day5/input.in
Normal file
1000
advent_of_code_2015/day5/input.in
Normal file
File diff suppressed because it is too large
Load diff
59
advent_of_code_2015/day5/main1.cc
Normal file
59
advent_of_code_2015/day5/main1.cc
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
bool has_forbidden(string const &str)
|
||||
{
|
||||
if (str.find("ab") != string::npos) return true;
|
||||
if (str.find("cd") != string::npos) return true;
|
||||
if (str.find("pq") != string::npos) return true;
|
||||
if (str.find("xy") != string::npos) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool has_repeating(string const &str)
|
||||
{
|
||||
for (size_t idx = 0; idx != str.size() - 1; ++idx)
|
||||
{
|
||||
if (str[idx] == str[idx + 1])
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool vowels_ok(string const &str)
|
||||
{
|
||||
size_t vowels = 0;
|
||||
|
||||
vowels += count(str.begin(), str.end(), 'a');
|
||||
vowels += count(str.begin(), str.end(), 'e');
|
||||
vowels += count(str.begin(), str.end(), 'i');
|
||||
vowels += count(str.begin(), str.end(), 'o');
|
||||
vowels += count(str.begin(), str.end(), 'u');
|
||||
|
||||
return vowels >= 3;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
string buffer;
|
||||
size_t count = 0;
|
||||
|
||||
while(getline(cin, buffer))
|
||||
{
|
||||
cout << "'" << buffer << "'\n";
|
||||
|
||||
if (vowels_ok(buffer)
|
||||
&& has_repeating(buffer)
|
||||
&& !has_forbidden(buffer))
|
||||
{
|
||||
++count;
|
||||
}
|
||||
}
|
||||
|
||||
cout << "Nice strings: " << count << "\n";
|
||||
}
|
||||
50
advent_of_code_2015/day5/main2.cc
Normal file
50
advent_of_code_2015/day5/main2.cc
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
bool has_repeating_pair(string const &text)
|
||||
{
|
||||
for (size_t idx = 0; idx < text.size() - 2; ++idx)
|
||||
{
|
||||
if (text.find(text.substr(idx, 2), idx + 2) != string::npos)
|
||||
{
|
||||
cout << "'" << text << "' has repeating pair: '";
|
||||
cout << text.substr(idx, 2) << "'\n";
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// cout << "'" << text << "' has no repeating pair\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
bool repeats_plus_one(string const &text)
|
||||
{
|
||||
for (size_t idx = 0; idx < text.size() - 2; ++idx)
|
||||
{
|
||||
if (text[idx] == text[idx + 2])
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
string buffer;
|
||||
size_t count = 0;
|
||||
|
||||
while (getline(cin, buffer))
|
||||
{
|
||||
if (has_repeating_pair(buffer)
|
||||
&& repeats_plus_one(buffer))
|
||||
{
|
||||
++count;
|
||||
}
|
||||
}
|
||||
|
||||
cout << "Nice strings: " << count << "\n";
|
||||
|
||||
}
|
||||
300
advent_of_code_2015/day6/input.in
Normal file
300
advent_of_code_2015/day6/input.in
Normal file
|
|
@ -0,0 +1,300 @@
|
|||
turn off 660,55 through 986,197
|
||||
turn off 341,304 through 638,850
|
||||
turn off 199,133 through 461,193
|
||||
toggle 322,558 through 977,958
|
||||
toggle 537,781 through 687,941
|
||||
turn on 226,196 through 599,390
|
||||
turn on 240,129 through 703,297
|
||||
turn on 317,329 through 451,798
|
||||
turn on 957,736 through 977,890
|
||||
turn on 263,530 through 559,664
|
||||
turn on 158,270 through 243,802
|
||||
toggle 223,39 through 454,511
|
||||
toggle 544,218 through 979,872
|
||||
turn on 313,306 through 363,621
|
||||
toggle 173,401 through 496,407
|
||||
toggle 333,60 through 748,159
|
||||
turn off 87,577 through 484,608
|
||||
turn on 809,648 through 826,999
|
||||
toggle 352,432 through 628,550
|
||||
turn off 197,408 through 579,569
|
||||
turn off 1,629 through 802,633
|
||||
turn off 61,44 through 567,111
|
||||
toggle 880,25 through 903,973
|
||||
turn on 347,123 through 864,746
|
||||
toggle 728,877 through 996,975
|
||||
turn on 121,895 through 349,906
|
||||
turn on 888,547 through 931,628
|
||||
toggle 398,782 through 834,882
|
||||
turn on 966,850 through 989,953
|
||||
turn off 891,543 through 914,991
|
||||
toggle 908,77 through 916,117
|
||||
turn on 576,900 through 943,934
|
||||
turn off 580,170 through 963,206
|
||||
turn on 184,638 through 192,944
|
||||
toggle 940,147 through 978,730
|
||||
turn off 854,56 through 965,591
|
||||
toggle 717,172 through 947,995
|
||||
toggle 426,987 through 705,998
|
||||
turn on 987,157 through 992,278
|
||||
toggle 995,774 through 997,784
|
||||
turn off 796,96 through 845,182
|
||||
turn off 451,87 through 711,655
|
||||
turn off 380,93 through 968,676
|
||||
turn on 263,468 through 343,534
|
||||
turn on 917,936 through 928,959
|
||||
toggle 478,7 through 573,148
|
||||
turn off 428,339 through 603,624
|
||||
turn off 400,880 through 914,953
|
||||
toggle 679,428 through 752,779
|
||||
turn off 697,981 through 709,986
|
||||
toggle 482,566 through 505,725
|
||||
turn off 956,368 through 993,516
|
||||
toggle 735,823 through 783,883
|
||||
turn off 48,487 through 892,496
|
||||
turn off 116,680 through 564,819
|
||||
turn on 633,865 through 729,930
|
||||
turn off 314,618 through 571,922
|
||||
toggle 138,166 through 936,266
|
||||
turn on 444,732 through 664,960
|
||||
turn off 109,337 through 972,497
|
||||
turn off 51,432 through 77,996
|
||||
turn off 259,297 through 366,744
|
||||
toggle 801,130 through 917,544
|
||||
toggle 767,982 through 847,996
|
||||
turn on 216,507 through 863,885
|
||||
turn off 61,441 through 465,731
|
||||
turn on 849,970 through 944,987
|
||||
toggle 845,76 through 852,951
|
||||
toggle 732,615 through 851,936
|
||||
toggle 251,128 through 454,778
|
||||
turn on 324,429 through 352,539
|
||||
toggle 52,450 through 932,863
|
||||
turn off 449,379 through 789,490
|
||||
turn on 317,319 through 936,449
|
||||
toggle 887,670 through 957,838
|
||||
toggle 671,613 through 856,664
|
||||
turn off 186,648 through 985,991
|
||||
turn off 471,689 through 731,717
|
||||
toggle 91,331 through 750,758
|
||||
toggle 201,73 through 956,524
|
||||
toggle 82,614 through 520,686
|
||||
toggle 84,287 through 467,734
|
||||
turn off 132,367 through 208,838
|
||||
toggle 558,684 through 663,920
|
||||
turn on 237,952 through 265,997
|
||||
turn on 694,713 through 714,754
|
||||
turn on 632,523 through 862,827
|
||||
turn on 918,780 through 948,916
|
||||
turn on 349,586 through 663,976
|
||||
toggle 231,29 through 257,589
|
||||
toggle 886,428 through 902,993
|
||||
turn on 106,353 through 236,374
|
||||
turn on 734,577 through 759,684
|
||||
turn off 347,843 through 696,912
|
||||
turn on 286,699 through 964,883
|
||||
turn on 605,875 through 960,987
|
||||
turn off 328,286 through 869,461
|
||||
turn off 472,569 through 980,848
|
||||
toggle 673,573 through 702,884
|
||||
turn off 398,284 through 738,332
|
||||
turn on 158,50 through 284,411
|
||||
turn off 390,284 through 585,663
|
||||
turn on 156,579 through 646,581
|
||||
turn on 875,493 through 989,980
|
||||
toggle 486,391 through 924,539
|
||||
turn on 236,722 through 272,964
|
||||
toggle 228,282 through 470,581
|
||||
toggle 584,389 through 750,761
|
||||
turn off 899,516 through 900,925
|
||||
turn on 105,229 through 822,846
|
||||
turn off 253,77 through 371,877
|
||||
turn on 826,987 through 906,992
|
||||
turn off 13,152 through 615,931
|
||||
turn on 835,320 through 942,399
|
||||
turn on 463,504 through 536,720
|
||||
toggle 746,942 through 786,998
|
||||
turn off 867,333 through 965,403
|
||||
turn on 591,477 through 743,692
|
||||
turn off 403,437 through 508,908
|
||||
turn on 26,723 through 368,814
|
||||
turn on 409,485 through 799,809
|
||||
turn on 115,630 through 704,705
|
||||
turn off 228,183 through 317,220
|
||||
toggle 300,649 through 382,842
|
||||
turn off 495,365 through 745,562
|
||||
turn on 698,346 through 744,873
|
||||
turn on 822,932 through 951,934
|
||||
toggle 805,30 through 925,421
|
||||
toggle 441,152 through 653,274
|
||||
toggle 160,81 through 257,587
|
||||
turn off 350,781 through 532,917
|
||||
toggle 40,583 through 348,636
|
||||
turn on 280,306 through 483,395
|
||||
toggle 392,936 through 880,955
|
||||
toggle 496,591 through 851,934
|
||||
turn off 780,887 through 946,994
|
||||
turn off 205,735 through 281,863
|
||||
toggle 100,876 through 937,915
|
||||
turn on 392,393 through 702,878
|
||||
turn on 956,374 through 976,636
|
||||
toggle 478,262 through 894,775
|
||||
turn off 279,65 through 451,677
|
||||
turn on 397,541 through 809,847
|
||||
turn on 444,291 through 451,586
|
||||
toggle 721,408 through 861,598
|
||||
turn on 275,365 through 609,382
|
||||
turn on 736,24 through 839,72
|
||||
turn off 86,492 through 582,712
|
||||
turn on 676,676 through 709,703
|
||||
turn off 105,710 through 374,817
|
||||
toggle 328,748 through 845,757
|
||||
toggle 335,79 through 394,326
|
||||
toggle 193,157 through 633,885
|
||||
turn on 227,48 through 769,743
|
||||
toggle 148,333 through 614,568
|
||||
toggle 22,30 through 436,263
|
||||
toggle 547,447 through 688,969
|
||||
toggle 576,621 through 987,740
|
||||
turn on 711,334 through 799,515
|
||||
turn on 541,448 through 654,951
|
||||
toggle 792,199 through 798,990
|
||||
turn on 89,956 through 609,960
|
||||
toggle 724,433 through 929,630
|
||||
toggle 144,895 through 201,916
|
||||
toggle 226,730 through 632,871
|
||||
turn off 760,819 through 828,974
|
||||
toggle 887,180 through 940,310
|
||||
toggle 222,327 through 805,590
|
||||
turn off 630,824 through 885,963
|
||||
turn on 940,740 through 954,946
|
||||
turn on 193,373 through 779,515
|
||||
toggle 304,955 through 469,975
|
||||
turn off 405,480 through 546,960
|
||||
turn on 662,123 through 690,669
|
||||
turn off 615,238 through 750,714
|
||||
turn on 423,220 through 930,353
|
||||
turn on 329,769 through 358,970
|
||||
toggle 590,151 through 704,722
|
||||
turn off 884,539 through 894,671
|
||||
toggle 449,241 through 984,549
|
||||
toggle 449,260 through 496,464
|
||||
turn off 306,448 through 602,924
|
||||
turn on 286,805 through 555,901
|
||||
toggle 722,177 through 922,298
|
||||
toggle 491,554 through 723,753
|
||||
turn on 80,849 through 174,996
|
||||
turn off 296,561 through 530,856
|
||||
toggle 653,10 through 972,284
|
||||
toggle 529,236 through 672,614
|
||||
toggle 791,598 through 989,695
|
||||
turn on 19,45 through 575,757
|
||||
toggle 111,55 through 880,871
|
||||
turn off 197,897 through 943,982
|
||||
turn on 912,336 through 977,605
|
||||
toggle 101,221 through 537,450
|
||||
turn on 101,104 through 969,447
|
||||
toggle 71,527 through 587,717
|
||||
toggle 336,445 through 593,889
|
||||
toggle 214,179 through 575,699
|
||||
turn on 86,313 through 96,674
|
||||
toggle 566,427 through 906,888
|
||||
turn off 641,597 through 850,845
|
||||
turn on 606,524 through 883,704
|
||||
turn on 835,775 through 867,887
|
||||
toggle 547,301 through 897,515
|
||||
toggle 289,930 through 413,979
|
||||
turn on 361,122 through 457,226
|
||||
turn on 162,187 through 374,746
|
||||
turn on 348,461 through 454,675
|
||||
turn off 966,532 through 985,537
|
||||
turn on 172,354 through 630,606
|
||||
turn off 501,880 through 680,993
|
||||
turn off 8,70 through 566,592
|
||||
toggle 433,73 through 690,651
|
||||
toggle 840,798 through 902,971
|
||||
toggle 822,204 through 893,760
|
||||
turn off 453,496 through 649,795
|
||||
turn off 969,549 through 990,942
|
||||
turn off 789,28 through 930,267
|
||||
toggle 880,98 through 932,434
|
||||
toggle 568,674 through 669,753
|
||||
turn on 686,228 through 903,271
|
||||
turn on 263,995 through 478,999
|
||||
toggle 534,675 through 687,955
|
||||
turn off 342,434 through 592,986
|
||||
toggle 404,768 through 677,867
|
||||
toggle 126,723 through 978,987
|
||||
toggle 749,675 through 978,959
|
||||
turn off 445,330 through 446,885
|
||||
turn off 463,205 through 924,815
|
||||
turn off 417,430 through 915,472
|
||||
turn on 544,990 through 912,999
|
||||
turn off 201,255 through 834,789
|
||||
turn off 261,142 through 537,862
|
||||
turn off 562,934 through 832,984
|
||||
turn off 459,978 through 691,980
|
||||
turn off 73,911 through 971,972
|
||||
turn on 560,448 through 723,810
|
||||
turn on 204,630 through 217,854
|
||||
turn off 91,259 through 611,607
|
||||
turn on 877,32 through 978,815
|
||||
turn off 950,438 through 974,746
|
||||
toggle 426,30 through 609,917
|
||||
toggle 696,37 through 859,201
|
||||
toggle 242,417 through 682,572
|
||||
turn off 388,401 through 979,528
|
||||
turn off 79,345 through 848,685
|
||||
turn off 98,91 through 800,434
|
||||
toggle 650,700 through 972,843
|
||||
turn off 530,450 through 538,926
|
||||
turn on 428,559 through 962,909
|
||||
turn on 78,138 through 92,940
|
||||
toggle 194,117 through 867,157
|
||||
toggle 785,355 through 860,617
|
||||
turn off 379,441 through 935,708
|
||||
turn off 605,133 through 644,911
|
||||
toggle 10,963 through 484,975
|
||||
turn off 359,988 through 525,991
|
||||
turn off 509,138 through 787,411
|
||||
toggle 556,467 through 562,773
|
||||
turn on 119,486 through 246,900
|
||||
turn on 445,561 through 794,673
|
||||
turn off 598,681 through 978,921
|
||||
turn off 974,230 through 995,641
|
||||
turn off 760,75 through 800,275
|
||||
toggle 441,215 through 528,680
|
||||
turn off 701,636 through 928,877
|
||||
turn on 165,753 through 202,780
|
||||
toggle 501,412 through 998,516
|
||||
toggle 161,105 through 657,395
|
||||
turn on 113,340 through 472,972
|
||||
toggle 384,994 through 663,999
|
||||
turn on 969,994 through 983,997
|
||||
turn on 519,600 through 750,615
|
||||
turn off 363,899 through 948,935
|
||||
turn on 271,845 through 454,882
|
||||
turn off 376,528 through 779,640
|
||||
toggle 767,98 through 854,853
|
||||
toggle 107,322 through 378,688
|
||||
turn off 235,899 through 818,932
|
||||
turn on 445,611 through 532,705
|
||||
toggle 629,387 through 814,577
|
||||
toggle 112,414 through 387,421
|
||||
toggle 319,184 through 382,203
|
||||
turn on 627,796 through 973,940
|
||||
toggle 602,45 through 763,151
|
||||
turn off 441,375 through 974,545
|
||||
toggle 871,952 through 989,998
|
||||
turn on 717,272 through 850,817
|
||||
toggle 475,711 through 921,882
|
||||
toggle 66,191 through 757,481
|
||||
turn off 50,197 through 733,656
|
||||
toggle 83,575 through 915,728
|
||||
turn on 777,812 through 837,912
|
||||
turn on 20,984 through 571,994
|
||||
turn off 446,432 through 458,648
|
||||
turn on 715,871 through 722,890
|
||||
toggle 424,675 through 740,862
|
||||
toggle 580,592 through 671,900
|
||||
toggle 296,687 through 906,775
|
||||
54
advent_of_code_2015/day6/main.ih
Normal file
54
advent_of_code_2015/day6/main.ih
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
enum Action
|
||||
{
|
||||
TURN_OFF,
|
||||
TURN_ON,
|
||||
TOGGLE
|
||||
};
|
||||
|
||||
struct Point
|
||||
{
|
||||
size_t x;
|
||||
size_t y;
|
||||
|
||||
Point()
|
||||
: x(0), y(0)
|
||||
{ }
|
||||
};
|
||||
|
||||
struct Instruction
|
||||
{
|
||||
Action action;
|
||||
Point from;
|
||||
Point through;
|
||||
};
|
||||
|
||||
istream &operator>>(istream &in, Point &point)
|
||||
{
|
||||
cin >> point.x;
|
||||
cin.ignore(1); // ignore ','
|
||||
cin >> point.y;
|
||||
|
||||
return in;
|
||||
}
|
||||
|
||||
// thrash tier parsing function
|
||||
istream &operator>>(istream &in, Instruction &instruction)
|
||||
{
|
||||
cin.ignore(5);
|
||||
string command;
|
||||
cin >> command;
|
||||
if (command == "le")
|
||||
instruction.action = TOGGLE;
|
||||
else instruction.action = command == "on" ? TURN_ON : TURN_OFF;
|
||||
cin >> instruction.from;
|
||||
cin.ignore(9); //ignore ' through '
|
||||
cin >> instruction.through;
|
||||
|
||||
return in;
|
||||
}
|
||||
56
advent_of_code_2015/day6/main1.cc
Normal file
56
advent_of_code_2015/day6/main1.cc
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
#include "main.ih"
|
||||
|
||||
int main()
|
||||
{
|
||||
vector<Instruction> instructions;
|
||||
|
||||
while(!cin.eof())
|
||||
{
|
||||
Instruction instr;
|
||||
cin >> instr;
|
||||
|
||||
instructions.push_back(instr);
|
||||
cout << "Instruction set " << instr.action << "\n";
|
||||
cout << " from: [" << instr.from.x << ", " << instr.from.y << "]\n";
|
||||
cout << " througn: [" << instr.through.x << ", " << instr.through.y << "]\n";
|
||||
}
|
||||
|
||||
vector<vector<bool>> lights;
|
||||
lights.resize(1000, vector<bool>(1000, false));
|
||||
|
||||
for (Instruction const &instr : instructions)
|
||||
{
|
||||
for (size_t idx = instr.from.x; idx <= instr.through.x; ++idx)
|
||||
{
|
||||
for (size_t idy = instr.from.y; idy <= instr.through.y; ++idy)
|
||||
{
|
||||
switch(instr.action)
|
||||
{
|
||||
case TURN_OFF:
|
||||
lights[idx][idy] = false;
|
||||
break;
|
||||
|
||||
case TURN_ON:
|
||||
lights[idx][idy] = true;
|
||||
break;
|
||||
|
||||
case TOGGLE:
|
||||
lights[idx][idy] = !lights[idx][idy];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
size_t lights_on = 0;
|
||||
for (size_t idx = 0; idx != lights.size(); ++idx)
|
||||
{
|
||||
for (size_t idy = 0; idy != lights[idx].size(); ++idy)
|
||||
{
|
||||
if (lights[idx][idy])
|
||||
++lights_on;
|
||||
}
|
||||
}
|
||||
|
||||
cout << "There are " << lights_on << " lights turned on!\n";
|
||||
}
|
||||
54
advent_of_code_2015/day6/main2.cc
Normal file
54
advent_of_code_2015/day6/main2.cc
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
#include "main.ih"
|
||||
|
||||
int main()
|
||||
{
|
||||
vector<Instruction> instructions;
|
||||
|
||||
while(!cin.eof())
|
||||
{
|
||||
Instruction instr;
|
||||
cin >> instr;
|
||||
|
||||
instructions.push_back(instr);
|
||||
cout << "Instruction set " << instr.action << "\n";
|
||||
cout << " from: [" << instr.from.x << ", " << instr.from.y << "]\n";
|
||||
cout << " througn: [" << instr.through.x << ", " << instr.through.y << "]\n";
|
||||
}
|
||||
|
||||
vector<vector<int>> lights;
|
||||
lights.resize(1000, vector<int>(1000, 0));
|
||||
|
||||
for (Instruction const &instr : instructions)
|
||||
{
|
||||
for (size_t idx = instr.from.x; idx <= instr.through.x; ++idx)
|
||||
{
|
||||
for (size_t idy = instr.from.y; idy <= instr.through.y; ++idy)
|
||||
{
|
||||
switch(instr.action)
|
||||
{
|
||||
case TURN_OFF:
|
||||
lights[idx][idy]
|
||||
= lights[idx][idy] ? lights[idx][idy] - 1 : 0;
|
||||
break;
|
||||
|
||||
case TURN_ON:
|
||||
lights[idx][idy] += 1;
|
||||
break;
|
||||
|
||||
case TOGGLE:
|
||||
lights[idx][idy] += 2;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
size_t brightness = 0;
|
||||
for (size_t idx = 0; idx != lights.size(); ++idx)
|
||||
{
|
||||
for (size_t idy = 0; idy != lights[idx].size(); ++idy)
|
||||
brightness += lights[idx][idy];
|
||||
}
|
||||
|
||||
cout << "Total brightness: " << brightness << "\n";
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue