Added older advents

This commit is contained in:
Jos van Goor 2022-12-01 13:46:47 +01:00
parent 8db2505049
commit 9cf858b860
78 changed files with 13807 additions and 0 deletions

File diff suppressed because one or more lines are too long

View 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";
}
}

View 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;
}
}
}
}

File diff suppressed because it is too large Load diff

View 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";
}

File diff suppressed because one or more lines are too long

View 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";
}

View 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";
}

View 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;
}
}
}

View 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();
}

View 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

File diff suppressed because it is too large Load diff

View 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";
}

View 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";
}

View 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

View 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;
}

View 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";
}

View 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";
}

994
advent_of_code_2018/day1/input.in Executable file
View file

@ -0,0 +1,994 @@
+5
-11
-1
-16
+12
+9
-15
+20
+4
+17
-4
-14
+6
-17
+6
-19
-12
+17
-6
-18
+1
+15
-14
+10
-4
-2
+5
+6
+8
+3
+17
-9
+18
-1
+17
-15
+7
-6
-17
-17
+19
+19
-13
+18
-15
+16
+7
-10
-5
+22
+8
-13
+4
+3
+19
+17
-19
-1
-11
+2
+1
-13
+1
+3
-14
+16
-9
+19
+13
-20
+6
+22
+10
+6
+15
-13
-13
-8
+16
+20
-5
+1
+7
+11
-13
+5
-16
-2
-4
+25
-5
-10
-12
-9
+16
-2
-19
-3
-12
+5
+17
+16
+11
+16
+10
+4
+3
+11
-12
+19
+3
+17
+15
-6
-6
-4
+3
+10
+8
+15
-7
+13
+3
+6
+2
+16
+20
+3
-11
+3
-11
+12
+6
+3
+11
+5
+2
+12
-4
-2
+4
+3
-4
+20
+1
+19
+14
+5
-16
-15
-16
-4
-10
-13
-10
-18
+10
-6
-7
-3
-2
-14
+12
-2
-6
-11
-5
-6
+20
-11
-1
+9
-1
+5
+6
+11
-19
-9
-14
-4
-6
-1
+2
-6
-12
+20
-12
-13
-9
-3
-7
-17
+14
-9
+20
-4
-8
-1
-6
+16
+6
-18
-12
+6
-22
-15
+10
+6
+20
-16
+1
-18
+4
+19
+9
-17
+1
+13
+21
+23
-4
+7
-16
+3
-4
-3
-17
-20
-10
-20
+16
+3
+14
-11
+3
-29
-1
+7
-18
+5
-7
-16
+6
-7
-15
+9
+22
-2
-21
-18
+1
+10
+13
+13
-24
-6
-22
-10
+9
+4
-8
-16
+3
-5
-19
-6
-11
+3
-8
-16
-14
-16
-15
-6
+3
+13
+8
-19
-18
-8
+9
-15
-14
-2
+18
-17
+2
+18
-6
-3
-19
+16
-4
+6
+21
-6
+7
+17
-8
-4
+6
+19
-2
+5
+20
-3
-6
+5
+5
-17
-18
+16
+4
+7
+6
+8
+6
-1
+10
-13
-4
-10
+8
-14
+17
+9
+2
-7
-22
-16
+20
-13
+8
-17
+1
+19
+14
+19
+11
-6
+25
+18
+19
+5
+20
+17
+4
-10
+47
+4
-1
+23
+24
-8
-10
+31
+19
-11
-17
+8
-24
+9
+32
+18
-1
+4
-13
+4
+17
-2
+11
+11
+4
-3
-3
+22
+19
+14
-21
+13
+9
-14
-16
+20
+16
-14
+5
-15
+12
-11
+1
+3
-24
-9
-9
-22
-12
-10
-7
-3
+12
+13
+17
-11
+35
+12
-9
+16
+22
-18
-18
+19
-8
+23
+19
+3
+8
+15
-2
+11
+14
+10
+15
+8
-1
+13
+1
+18
-11
+4
+4
+11
+9
-18
-9
+13
-18
+10
+3
-6
-5
-10
-11
-14
-2
-9
+3
-4
-6
+13
-2
-17
-4
+7
+6
-12
-7
+14
-6
-18
+19
-18
-2
-13
+6
-15
+14
+7
+9
-14
+9
-19
+1
+13
-2
-10
-11
+13
-6
-6
+19
+10
+17
-8
-7
+19
+16
-1
+7
+16
-15
-17
+12
+16
-2
+14
-3
-16
-17
-5
+16
+29
-3
-2
+13
-18
+15
-4
-14
-12
-16
-19
+39
-9
+19
+3
-11
+10
+12
+19
-4
+12
+13
+19
+3
-19
+9
-16
+10
+18
+12
-6
-2
-11
+20
+15
+15
+4
+12
-38
+9
+33
+24
+6
+4
-36
-47
-15
-13
-3
+1
+12
+14
+9
-32
+7
-16
-5
-29
+12
-2
+15
-12
-11
-39
-40
+3
-34
-5
+78
+4
+52
+1
+90
+112
+152
-80
+81800
-18
+15
-9
+11
+2
-5
+16
-5
+15
-1
+2
-14
-7
+12
-10
-9
+20
+14
-9
+12
+9
+13
+3
+11
+14
-3
+10
+8
+12
+5
+10
+1
+14
-5
+1
-4
-13
+9
+18
+17
+16
+9
-1
-10
+13
+13
+11
-18
+15
-9
-13
+3
+1
+11
-3
-10
+19
-11
+13
+12
-8
-10
-6
+13
+5
-13
-7
-1
+18
+7
+14
-2
+5
+1
+16
-7
+18
+13
-6
+18
-1
+12
-4
-10
+7
-15
-9
-7
-7
-6
+11
-16
+7
-4
+11
-4
-6
-14
+1
+6
-3
+2
+21
+12
-16
+18
-5
+17
+4
+19
+6
-13
+17
-3
-2
-17
+7
-3
-7
+14
+17
+8
+7
-17
+18
-15
-4
+7
+2
+1
-14
-1
-6
+9
-10
-4
+15
-14
-18
-14
-9
-11
+9
+3
+12
+1
+3
-10
-15
+7
+6
+13
+9
+7
-4
+9
-14
+21
-9
+24
+10
+6
+6
-19
+22
-10
-18
+10
+14
-4
+3
+7
+14
+11
+16
-10
+11
-9
-5
-6
-2
-15
+3
-16
+19
+8
+2
-17
+3
+5
+14
-16
-4
-5
+1
-3
+4
+6
+18
+16
+4
-18
+6
+10
-6
+1
+6
+9
+17
-8
-15
-12
+1
-6
+4
+12
+2
+17
-9
-7
+10
+12
+17
+8
+15
+7
-18
-1
+4
+13
-3
+16
+4
+2
-8
-5
+14
+10
+18
-15
+19
-11
-4
-16
+13
+16
-15
+10
-20
-7
-15
-10
-2
-6
+19
-3
-9
-18
+12
-8
+3
+13
+20
+18
-16
+15
+12
+1
-18
+10
-14
-17
-7
-9
-20
+8
-9
+7
-11
-19
+7
-14
+6
+4
+14
+8
-11
+2
-1
-19
+16
+9
-10
+20
+12
+11
+13
-3
-2
+3
-20
+6
-17
+3
-9
-6
-16
+7
+7
-23
-6
+14
-3
-31
-18
-19
-12
-26
+23
+9
-7
-10
+22
+17
+17
-7
-8
+9
+28
+5
+24
-28
+36
+37
+7
+12
+19
+15
-3
-20
+17
+18
+4
-8
+9
-4
-2
-19
+11
-1
+3
-7
+8
+9
-8
+19
+9
+12
-11
-5
-13
+9
-6
+7
+17
-13
+16
+10
-5
+20
+6
-1
+17
-14
+10
+18
-1
+3
-10
-4
-10
-10
-16
-2
+7
+16
-12
+6
+8
+22
+7
-6
-13
-14
+10
-1
-21
-1
+8
-15
+9
-17
-2
-21
+4
-19
-13
-8
+26
+4
-27
-8
+2
+30
-11
-82484

View file

@ -0,0 +1,46 @@
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
int read()
{
string line;
getline(cin, line);
bool plus = line[0] == '+';
int val = stoi(line.substr(1));
return plus ? val : -val;
}
int main()
{
vector<int> sequence;
while (!cin.eof())
sequence.push_back(read());
// cout << "Buffered " << sequence.size() << " frequencies\n";
int total = 0;
set<int> used;
used.insert(0);
while (true)
{
for (int i : sequence)
{
total += i;
if (used.find(total) != used.end())
{
cout << "Found first repeating frequency: " << total << "\n";
return 0;
}
used.insert(total);
}
}
}

View file

@ -0,0 +1,385 @@
position=<-52592, 31869> velocity=< 5, -3>
position=<-20934, 52988> velocity=< 2, -5>
position=<-20910, 31871> velocity=< 2, -3>
position=<-31503, -52596> velocity=< 3, 5>
position=<-42061, -10364> velocity=< 4, 1>
position=< 10776, -31475> velocity=<-1, 3>
position=<-10348, 31875> velocity=< 1, -3>
position=< 52969, 52985> velocity=<-5, -5>
position=< 31848, -10366> velocity=<-3, 1>
position=<-10376, -10366> velocity=< 1, 1>
position=<-10376, -31474> velocity=< 1, 3>
position=<-42058, 52990> velocity=< 4, -5>
position=<-52628, -10358> velocity=< 5, 1>
position=<-52630, 52985> velocity=< 5, -5>
position=< 31888, -31474> velocity=<-3, 3>
position=<-31492, 42429> velocity=< 3, -4>
position=< 31892, 52988> velocity=<-3, -5>
position=< 52961, 52989> velocity=<-5, -5>
position=<-20908, -42031> velocity=< 2, 4>
position=<-52627, 10759> velocity=< 5, -1>
position=< 21307, -42034> velocity=<-2, 4>
position=<-20937, -31475> velocity=< 2, 3>
position=< 21315, -52594> velocity=<-2, 5>
position=< 10744, -10366> velocity=<-1, 1>
position=< 21287, -10358> velocity=<-2, 1>
position=< 21298, -31478> velocity=<-2, 3>
position=<-10384, 31866> velocity=< 1, -3>
position=< 52953, -10364> velocity=<-5, 1>
position=<-42029, -52597> velocity=< 4, 5>
position=< 21319, 10757> velocity=<-2, -1>
position=<-10352, 21310> velocity=< 1, -2>
position=<-10395, -42034> velocity=< 1, 4>
position=< 10756, 31873> velocity=<-1, -3>
position=<-10384, 42425> velocity=< 1, -4>
position=<-42050, 42427> velocity=< 4, -4>
position=< 31866, 10759> velocity=<-3, -1>
position=<-52592, -10357> velocity=< 5, 1>
position=<-42066, 31874> velocity=< 4, -3>
position=<-52605, -20924> velocity=< 5, 2>
position=< 21274, 10753> velocity=<-2, -1>
position=<-20933, 42433> velocity=< 2, -4>
position=< 52961, -10365> velocity=<-5, 1>
position=<-20913, -20923> velocity=< 2, 2>
position=< 10716, -42039> velocity=<-1, 4>
position=<-42070, 21315> velocity=< 4, -2>
position=<-42040, 31875> velocity=< 4, -3>
position=<-42029, 52989> velocity=< 4, -5>
position=< 52953, -20917> velocity=<-5, 2>
position=<-42058, -10364> velocity=< 4, 1>
position=<-52574, -42031> velocity=< 5, 4>
position=<-52600, -42032> velocity=< 5, 4>
position=< 10740, -52598> velocity=<-1, 5>
position=< 42438, -20919> velocity=<-4, 2>
position=<-20937, -31477> velocity=< 2, 3>
position=<-42072, 42427> velocity=< 4, -4>
position=<-20921, -52598> velocity=< 2, 5>
position=<-52619, -20918> velocity=< 5, 2>
position=<-31467, -20915> velocity=< 3, 2>
position=<-42066, -10366> velocity=< 4, 1>
position=< 21334, -42035> velocity=<-2, 4>
position=< 21309, 21317> velocity=<-2, -2>
position=< 42414, -10364> velocity=<-4, 1>
position=< 21290, 21311> velocity=<-2, -2>
position=< 52972, -20917> velocity=<-5, 2>
position=< 10776, 10755> velocity=<-1, -1>
position=< 21298, -10363> velocity=<-2, 1>
position=< 42430, 52986> velocity=<-4, -5>
position=< 21294, 21312> velocity=<-2, -2>
position=< 10752, -10357> velocity=<-1, 1>
position=< 42438, 21315> velocity=<-4, -2>
position=<-42050, -20915> velocity=< 4, 2>
position=<-31504, -52598> velocity=< 3, 5>
position=< 52999, 21317> velocity=<-5, -2>
position=<-10390, -10362> velocity=< 1, 1>
position=< 21300, 10754> velocity=<-2, -1>
position=<-20933, -10366> velocity=< 2, 1>
position=< 10725, -10366> velocity=<-1, 1>
position=< 10737, 31874> velocity=<-1, -3>
position=<-20913, 10758> velocity=< 2, -1>
position=<-42063, 21312> velocity=< 4, -2>
position=<-10358, 10754> velocity=< 1, -1>
position=< 21334, 52986> velocity=<-2, -5>
position=< 42430, 31871> velocity=<-4, -3>
position=< 21319, 21310> velocity=<-2, -2>
position=< 21290, -52592> velocity=<-2, 5>
position=< 21295, 10753> velocity=<-2, -1>
position=< 42390, -42036> velocity=<-4, 4>
position=< 10721, 52990> velocity=<-1, -5>
position=< 21285, -52598> velocity=<-2, 5>
position=< 31837, -42037> velocity=<-3, 4>
position=<-42018, 52989> velocity=< 4, -5>
position=<-10355, 31871> velocity=< 1, -3>
position=< 42411, -31477> velocity=<-4, 3>
position=<-52632, 42428> velocity=< 5, -4>
position=< 42441, 10759> velocity=<-4, -1>
position=<-52624, -10365> velocity=< 5, 1>
position=<-10360, -42038> velocity=< 1, 4>
position=< 21314, 21311> velocity=<-2, -2>
position=< 52988, 42430> velocity=<-5, -4>
position=<-10350, -31482> velocity=< 1, 3>
position=<-52592, 10753> velocity=< 5, -1>
position=<-20954, -52590> velocity=< 2, 5>
position=<-52611, -42033> velocity=< 5, 4>
position=< 31841, 42424> velocity=<-3, -4>
position=< 10729, 52990> velocity=<-1, -5>
position=< 31837, 42426> velocity=<-3, -4>
position=<-20958, 42429> velocity=< 2, -4>
position=< 10737, 42432> velocity=<-1, -4>
position=<-52624, -31477> velocity=< 5, 3>
position=<-20949, 10754> velocity=< 2, -1>
position=< 10729, -20923> velocity=<-1, 2>
position=< 31892, -52597> velocity=<-3, 5>
position=<-10355, 21314> velocity=< 1, -2>
position=< 52960, 42428> velocity=<-5, -4>
position=<-52587, 42424> velocity=< 5, -4>
position=<-52592, 52983> velocity=< 5, -5>
position=< 31832, 21315> velocity=<-3, -2>
position=< 52956, -10362> velocity=<-5, 1>
position=< 52992, 42428> velocity=<-5, -4>
position=< 10749, -20924> velocity=<-1, 2>
position=<-31504, 21308> velocity=< 3, -2>
position=<-20950, -42032> velocity=< 2, 4>
position=<-20907, 31866> velocity=< 2, -3>
position=<-52624, 42432> velocity=< 5, -4>
position=< 31865, -10357> velocity=<-3, 1>
position=<-52611, 52988> velocity=< 5, -5>
position=< 42441, 31866> velocity=<-4, -3>
position=< 42438, 21309> velocity=<-4, -2>
position=< 42398, 21310> velocity=<-4, -2>
position=<-10352, -10360> velocity=< 1, 1>
position=<-42050, -42036> velocity=< 4, 4>
position=< 52948, 42429> velocity=<-5, -4>
position=< 52993, 21311> velocity=<-5, -2>
position=<-10384, -52598> velocity=< 1, 5>
position=<-42029, -31473> velocity=< 4, 3>
position=<-10375, -52589> velocity=< 1, 5>
position=<-31460, -31475> velocity=< 3, 3>
position=< 42418, -31473> velocity=<-4, 3>
position=<-10392, 52984> velocity=< 1, -5>
position=< 10767, 10750> velocity=<-1, -1>
position=<-52573, 52991> velocity=< 5, -5>
position=<-52611, 52984> velocity=< 5, -5>
position=<-52592, 42431> velocity=< 5, -4>
position=<-10380, 21312> velocity=< 1, -2>
position=<-42029, -42038> velocity=< 4, 4>
position=<-42056, -10362> velocity=< 4, 1>
position=<-42041, -20915> velocity=< 4, 2>
position=<-10395, 31868> velocity=< 1, -3>
position=<-42045, 52991> velocity=< 4, -5>
position=< 21319, 21313> velocity=<-2, -2>
position=< 31844, -20920> velocity=<-3, 2>
position=<-20918, 52990> velocity=< 2, -5>
position=<-31514, -52595> velocity=< 3, 5>
position=< 10752, 31869> velocity=<-1, -3>
position=<-42034, -52597> velocity=< 4, 5>
position=< 10733, -20920> velocity=<-1, 2>
position=<-31476, 21309> velocity=< 3, -2>
position=< 10716, -42037> velocity=<-1, 4>
position=< 10720, 42431> velocity=<-1, -4>
position=<-20909, -20915> velocity=< 2, 2>
position=<-52627, 10750> velocity=< 5, -1>
position=<-31489, -52589> velocity=< 3, 5>
position=< 10756, -20915> velocity=<-1, 2>
position=<-31483, -20918> velocity=< 3, 2>
position=< 31861, 10759> velocity=<-3, -1>
position=<-31471, 21308> velocity=< 3, -2>
position=< 52948, -42040> velocity=<-5, 4>
position=< 21302, -31478> velocity=<-2, 3>
position=<-52587, -31476> velocity=< 5, 3>
position=< 21308, -31477> velocity=<-2, 3>
position=<-31492, 42429> velocity=< 3, -4>
position=<-31513, -31476> velocity=< 3, 3>
position=<-10388, -52598> velocity=< 1, 5>
position=<-20953, 31867> velocity=< 2, -3>
position=<-42038, -20921> velocity=< 4, 2>
position=< 10732, 31870> velocity=<-1, -3>
position=< 52964, -52595> velocity=<-5, 5>
position=< 21284, 10750> velocity=<-2, -1>
position=< 21299, 21317> velocity=<-2, -2>
position=<-42041, 31872> velocity=< 4, -3>
position=<-10360, 10750> velocity=< 1, -1>
position=< 10774, 10759> velocity=<-1, -1>
position=< 42406, 42429> velocity=<-4, -4>
position=<-42022, 42433> velocity=< 4, -4>
position=<-52606, 42428> velocity=< 5, -4>
position=< 31832, -31476> velocity=<-3, 3>
position=< 21295, -20923> velocity=<-2, 2>
position=<-42050, -42039> velocity=< 4, 4>
position=< 21284, -52594> velocity=<-2, 5>
position=<-42034, -42035> velocity=< 4, 4>
position=< 21279, 21313> velocity=<-2, -2>
position=< 31880, 42427> velocity=<-3, -4>
position=<-31484, 21317> velocity=< 3, -2>
position=< 42391, -20922> velocity=<-4, 2>
position=<-42058, -10365> velocity=< 4, 1>
position=< 42409, 52986> velocity=<-4, -5>
position=<-20918, 42431> velocity=< 2, -4>
position=< 31851, 21312> velocity=<-3, -2>
position=<-10387, -31479> velocity=< 1, 3>
position=<-42033, -31478> velocity=< 4, 3>
position=<-42047, -42036> velocity=< 4, 4>
position=< 21301, -31482> velocity=<-2, 3>
position=<-10365, -31473> velocity=< 1, 3>
position=<-31471, -42031> velocity=< 3, 4>
position=<-42074, -10357> velocity=< 4, 1>
position=<-31500, 10758> velocity=< 3, -1>
position=< 21311, 10751> velocity=<-2, -1>
position=< 42398, -10362> velocity=<-4, 1>
position=<-20921, 42424> velocity=< 2, -4>
position=< 52985, -52597> velocity=<-5, 5>
position=<-42073, 52983> velocity=< 4, -5>
position=< 42390, 31874> velocity=<-4, -3>
position=<-42061, -10360> velocity=< 4, 1>
position=< 21301, 42428> velocity=<-2, -4>
position=<-20942, -20917> velocity=< 2, 2>
position=<-10350, -52589> velocity=< 1, 5>
position=<-52595, 31875> velocity=< 5, -3>
position=< 42432, -52594> velocity=<-4, 5>
position=<-20937, -20924> velocity=< 2, 2>
position=< 42395, 31873> velocity=<-4, -3>
position=<-31508, 42433> velocity=< 3, -4>
position=< 10751, -31473> velocity=<-1, 3>
position=<-42032, 52986> velocity=< 4, -5>
position=< 42447, -10357> velocity=<-4, 1>
position=<-42048, 42428> velocity=< 4, -4>
position=<-10344, -42032> velocity=< 1, 4>
position=< 21290, -42034> velocity=<-2, 4>
position=<-42014, -42037> velocity=< 4, 4>
position=<-20906, -42040> velocity=< 2, 4>
position=< 10721, -20920> velocity=<-1, 2>
position=<-10379, 42433> velocity=< 1, -4>
position=< 21319, 52986> velocity=<-2, -5>
position=<-10368, 42431> velocity=< 1, -4>
position=<-20950, -20924> velocity=< 2, 2>
position=< 31877, -52595> velocity=<-3, 5>
position=<-31482, 42429> velocity=< 3, -4>
position=< 21334, 42432> velocity=<-2, -4>
position=<-10364, 42427> velocity=< 1, -4>
position=<-42074, 10755> velocity=< 4, -1>
position=< 21300, -10366> velocity=<-2, 1>
position=< 42408, 10754> velocity=<-4, -1>
position=< 10732, 10753> velocity=<-1, -1>
position=<-52632, 21310> velocity=< 5, -2>
position=< 10740, -20920> velocity=<-1, 2>
position=< 52956, 42429> velocity=<-5, -4>
position=< 10756, -52598> velocity=<-1, 5>
position=<-31506, 52991> velocity=< 3, -5>
position=< 10724, 10751> velocity=<-1, -1>
position=<-20921, 21310> velocity=< 2, -2>
position=<-20946, -42031> velocity=< 2, 4>
position=< 42435, 52986> velocity=<-4, -5>
position=<-42037, -31473> velocity=< 4, 3>
position=< 21287, -52592> velocity=<-2, 5>
position=< 10717, -42039> velocity=<-1, 4>
position=<-52627, 31872> velocity=< 5, -3>
position=< 31875, 52986> velocity=<-3, -5>
position=< 42411, -42036> velocity=<-4, 4>
position=< 52964, -20915> velocity=<-5, 2>
position=<-31488, 42428> velocity=< 3, -4>
position=< 42426, 21308> velocity=<-4, -2>
position=<-52579, 10751> velocity=< 5, -1>
position=<-42014, 10753> velocity=< 4, -1>
position=<-42034, 10752> velocity=< 4, -1>
position=< 21275, 42425> velocity=<-2, -4>
position=<-10350, -10366> velocity=< 1, 1>
position=<-52628, -52591> velocity=< 5, 5>
position=< 31845, -52591> velocity=<-3, 5>
position=< 21285, -31473> velocity=<-2, 3>
position=< 21319, 21312> velocity=<-2, -2>
position=< 42427, -31480> velocity=<-4, 3>
position=< 52956, -52591> velocity=<-5, 5>
position=<-10389, 52986> velocity=< 1, -5>
position=< 42400, 10754> velocity=<-4, -1>
position=<-31508, -10362> velocity=< 3, 1>
position=< 10740, 10756> velocity=<-1, -1>
position=<-20898, 42424> velocity=< 2, -4>
position=< 21319, -42031> velocity=<-2, 4>
position=<-52574, 10759> velocity=< 5, -1>
position=< 21274, -31476> velocity=<-2, 3>
position=<-31463, -10358> velocity=< 3, 1>
position=<-20950, 21311> velocity=< 2, -2>
position=<-52600, -31474> velocity=< 5, 3>
position=< 31864, 42431> velocity=<-3, -4>
position=< 21279, 21316> velocity=<-2, -2>
position=< 21287, 42426> velocity=<-2, -4>
position=< 10716, 10754> velocity=<-1, -1>
position=<-42042, 52989> velocity=< 4, -5>
position=<-31492, 42427> velocity=< 3, -4>
position=< 21298, -10364> velocity=<-2, 1>
position=<-42040, 21308> velocity=< 4, -2>
position=<-42045, 10750> velocity=< 4, -1>
position=< 31835, -20919> velocity=<-3, 2>
position=< 10773, 42433> velocity=<-1, -4>
position=< 42443, 10758> velocity=<-4, -1>
position=< 52973, -52598> velocity=<-5, 5>
position=< 21295, -52592> velocity=<-2, 5>
position=<-10392, 31868> velocity=< 1, -3>
position=< 21282, -42031> velocity=<-2, 4>
position=< 10737, -20920> velocity=<-1, 2>
position=< 52985, 31875> velocity=<-5, -3>
position=<-42049, -20920> velocity=< 4, 2>
position=< 52992, 10754> velocity=<-5, -1>
position=< 10716, 31866> velocity=<-1, -3>
position=< 31884, -20924> velocity=<-3, 2>
position=< 42442, -20924> velocity=<-4, 2>
position=< 10732, -10361> velocity=<-1, 1>
position=<-31508, 10756> velocity=< 3, -1>
position=< 31872, 21314> velocity=<-3, -2>
position=<-42042, 10750> velocity=< 4, -1>
position=<-42069, 21315> velocity=< 4, -2>
position=<-20898, -10365> velocity=< 2, 1>
position=<-42071, -20918> velocity=< 4, 2>
position=< 10752, -10357> velocity=<-1, 1>
position=<-20946, -10362> velocity=< 2, 1>
position=<-52572, -10358> velocity=< 5, 1>
position=< 10716, 42424> velocity=<-1, -4>
position=<-52611, 42426> velocity=< 5, -4>
position=< 31856, 52991> velocity=<-3, -5>
position=< 10750, 52982> velocity=<-1, -5>
position=<-31495, -52598> velocity=< 3, 5>
position=< 52975, 21308> velocity=<-5, -2>
position=< 10729, -20919> velocity=<-1, 2>
position=<-52620, 10759> velocity=< 5, -1>
position=< 10751, -10366> velocity=<-1, 1>
position=<-20898, -52591> velocity=< 2, 5>
position=<-42023, 52991> velocity=< 4, -5>
position=<-31500, 31875> velocity=< 3, -3>
position=<-52584, 52990> velocity=< 5, -5>
position=< 21314, 52990> velocity=<-2, -5>
position=< 10776, 21309> velocity=<-1, -2>
position=<-10376, 31875> velocity=< 1, -3>
position=<-20946, -52589> velocity=< 2, 5>
position=<-52628, -52590> velocity=< 5, 5>
position=< 52988, -31474> velocity=<-5, 3>
position=<-31491, -42040> velocity=< 3, 4>
position=<-20918, -42036> velocity=< 2, 4>
position=<-20947, 10754> velocity=< 2, -1>
position=< 52953, -31474> velocity=<-5, 3>
position=<-42031, 31870> velocity=< 4, -3>
position=<-52583, 42424> velocity=< 5, -4>
position=< 42393, -20919> velocity=<-4, 2>
position=< 42411, 10750> velocity=<-4, -1>
position=< 10716, -42032> velocity=<-1, 4>
position=<-31503, 42429> velocity=< 3, -4>
position=<-42053, 10753> velocity=< 4, -1>
position=<-31481, -20920> velocity=< 3, 2>
position=<-31468, -31481> velocity=< 3, 3>
position=< 21284, -20924> velocity=<-2, 2>
position=< 42409, -42036> velocity=<-4, 4>
position=<-42063, 31875> velocity=< 4, -3>
position=< 42450, -10358> velocity=<-4, 1>
position=<-20897, -52598> velocity=< 2, 5>
position=< 42407, -10362> velocity=<-4, 1>
position=< 52983, 10754> velocity=<-5, -1>
position=< 52969, -31477> velocity=<-5, 3>
position=< 53000, -10357> velocity=<-5, 1>
position=<-52619, -31475> velocity=< 5, 3>
position=< 10756, 10756> velocity=<-1, -1>
position=< 31889, -42031> velocity=<-3, 4>
position=<-31511, 52985> velocity=< 3, -5>
position=<-31511, 42425> velocity=< 3, -4>
position=< 21322, -31476> velocity=<-2, 3>
position=< 10764, 21312> velocity=<-1, -2>
position=< 10724, 21314> velocity=<-1, -2>
position=< 53001, 10758> velocity=<-5, -1>
position=<-52607, -10362> velocity=< 5, 1>
position=<-31482, -20919> velocity=< 3, 2>
position=< 21276, 10754> velocity=<-2, -1>
position=< 42426, 21317> velocity=<-4, -2>
position=< 21299, -10362> velocity=<-2, 1>
position=< 42450, -20922> velocity=<-4, 2>
position=< 10719, 10755> velocity=<-1, -1>
position=<-52623, 10759> velocity=< 5, -1>
position=< 31840, -42040> velocity=<-3, 4>
position=< 10724, -20918> velocity=<-1, 2>
position=<-10341, 21308> velocity=< 1, -2>
position=< 42403, -10365> velocity=<-4, 1>
position=< 31877, 31866> velocity=<-3, -3>
position=<-42048, 31875> velocity=< 4, -3>
position=< 52953, 52986> velocity=<-5, -5>
position=<-10340, -10360> velocity=< 1, 1>
position=< 31869, 10752> velocity=<-3, -1>
position=<-52612, 21312> velocity=< 5, -2>
position=<-42053, -52592> velocity=< 4, 5>
position=<-31490, 10750> velocity=< 3, -1>

View file

@ -0,0 +1,49 @@
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Position
{
int xpos;
int ypos;
Position() : xpos(0), ypos(0) {}
};
struct Velocity
{
int dx;
int dy;
Velocity() : dx(0), dy(0) {}
};
inline istream &operator>>(istream &in, Position &position)
{
cin.ignore(10); //ignore 'position=<'
cin >> position.xpos;
cin.ignore(2); //ignore ', '
cin >> position.ypos;
cin.ignore(2); //ignore '> '
return in;
}
inline istream &operator>>(istream &in, Velocity &velocity)
{
cin.ignore(10); //ignore 'velocity=<'
cin >> velocity.dx;
cin.ignore(2); //ignore ', '
cin >> velocity.dy;
cin.ignore(2); // ignore '>\n'
return in;
}
inline Position operator+(Position pos, Velocity const &vel)
{
pos.xpos += vel.dx;
pos.ypos += vel.dy;
return pos;
}

View file

@ -0,0 +1,101 @@
#include "main.ih"
struct Star
{
Position pos;
Velocity vel;
void update()
{
pos = pos + vel;
}
};
Star read_star()
{
Star rval;
cin >> rval.pos;
cin >> rval.vel;
return rval;
}
size_t grid_size(vector<Star> const &stars)
{
int minx = numeric_limits<int>::max();
int maxx = numeric_limits<int>::min();
int miny = minx;
int maxy = maxx;
for (Star const &star : stars)
{
minx = min(minx, star.pos.xpos);
maxx = max(maxx, star.pos.xpos);
miny = min(miny, star.pos.ypos);
maxy = max(maxy, star.pos.ypos);
}
size_t width = maxx - minx + 1;
size_t height = maxy - miny + 1;
return width * height;
}
void write_to_console(vector<Star> const &stars)
{
int minx = numeric_limits<int>::max();
int maxx = numeric_limits<int>::min();
int miny = minx;
int maxy = maxx;
for (Star const &star : stars)
{
minx = min(minx, star.pos.xpos);
maxx = max(maxx, star.pos.xpos);
miny = min(miny, star.pos.ypos);
maxy = max(maxy, star.pos.ypos);
}
int xpos = minx;
int ypos = miny;
int width = maxx - minx + 1;
int height = maxy - miny + 1;
vector<vector<bool>> grid;
grid.resize(width, vector<bool>(height));
for (Star const &star : stars)
grid[star.pos.xpos - xpos][star.pos.ypos - ypos] = true;
for (int idy = 0; idy < height; ++idy)
{
for (int idx = 0; idx < width; ++idx)
cout << (grid[idx][idy] ? '#' : ' ');
cout << "\n";
}
}
int main()
{
vector<Star> stars;
while (!cin.eof())
stars.push_back(read_star());
size_t size = grid_size(stars);
for (size_t time = 0; true; ++time)
{
vector<Star> next_position(stars);
for (Star &star : next_position)
star.update();
size_t next_size = grid_size(next_position);
if (next_size > size)
{
write_to_console(stars);
cout << "That took " << time << " seconds!\n"; // part 2
return 0;
}
stars.swap(next_position);
size = next_size;
}
}

View file

@ -0,0 +1,31 @@
position=< 9, 1> velocity=< 0, 2>
position=< 7, 0> velocity=<-1, 0>
position=< 3, -2> velocity=<-1, 1>
position=< 6, 10> velocity=<-2, -1>
position=< 2, -4> velocity=< 2, 2>
position=<-6, 10> velocity=< 2, -2>
position=< 1, 8> velocity=< 1, -1>
position=< 1, 7> velocity=< 1, 0>
position=<-3, 11> velocity=< 1, -2>
position=< 7, 6> velocity=<-1, -1>
position=<-2, 3> velocity=< 1, 0>
position=<-4, 3> velocity=< 2, 0>
position=<10, -3> velocity=<-1, 1>
position=< 5, 11> velocity=< 1, -2>
position=< 4, 7> velocity=< 0, -1>
position=< 8, -2> velocity=< 0, 1>
position=<15, 0> velocity=<-2, 0>
position=< 1, 6> velocity=< 1, 0>
position=< 8, 9> velocity=< 0, -1>
position=< 3, 3> velocity=<-1, 1>
position=< 0, 5> velocity=< 0, -1>
position=<-2, 2> velocity=< 2, 0>
position=< 5, -2> velocity=< 1, 2>
position=< 1, 4> velocity=< 2, 1>
position=<-2, 7> velocity=< 2, -2>
position=< 3, 6> velocity=<-1, -1>
position=< 5, 0> velocity=< 1, 0>
position=<-6, 0> velocity=< 2, 0>
position=< 5, 9> velocity=< 1, -2>
position=<14, 7> velocity=<-2, 0>
position=<-3, 6> velocity=< 2, -1>

View file

@ -0,0 +1,3 @@
8199
300
300

View file

@ -0,0 +1,80 @@
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int evaluate_square(vector<vector<int>> const &grid, size_t topx, size_t lefty, size_t size = 3)
{
int sum = 0;
for (size_t idx = 0; idx != size; ++idx)
{
for (size_t idy = 0; idy != size; ++idy)
sum += grid[topx + idx][lefty + idy];
}
return sum;
}
int main()
{
size_t grid_width;
size_t grid_height;
int grid_serial_number;
cin >> grid_serial_number;
cin >> grid_width;
cin >> grid_height;
vector<vector<int>> grid;
grid.resize(grid_width, vector<int>(grid_height, 0));
for (size_t idy = 0; idy < grid_height; ++idy)
{
for (size_t idx = 0; idx < grid_width; ++idx)
{
int rack_id = idx + 11;
int powerlevel = rack_id * (idy + 1);
powerlevel += grid_serial_number;
powerlevel *= rack_id;
string text = to_string(powerlevel);
powerlevel = text.size() >= 3 ? (text[text.size() - 3] - '0') : 0;
powerlevel -= 5;
grid[idx][idy] = powerlevel;
// cout << setw(4) << powerlevel;
}
// cout << "\n";
}
int top_value = numeric_limits<int>::min();
size_t top_size;
size_t top_x;
size_t top_y;
for (size_t size = 0; size != 300; ++size)
{
cout << "Size " << setw(3) << size << "/300\r" << flush;
for (size_t idx = 1; idx != (grid_width - (size - 1)); ++idx)
{
for (size_t idy = 0; idy != (grid_width - (size - 1)); ++idy)
{
int value = evaluate_square(grid, idx, idy, size);
if (value > top_value)
{
top_value = value;
top_size = size;
top_x = idx + 1;
top_y = idy + 1;
}
}
}
}
cout << "Top x,y,size is " << top_x << "," << top_y << "," << top_size
<< " with " << top_value << " power\n";
}

View file

@ -0,0 +1,3 @@
18
300
300

View file

@ -0,0 +1,34 @@
initial state: ##.######...#.##.#...#...##.####..###.#.##.#.##...##..#...##.#..##....##...........#.#.#..###.#
.###. => #
#.##. => .
.#.## => #
...## => .
###.# => #
##.## => .
..... => .
#..#. => #
..#.. => #
#.### => #
##.#. => .
..#.# => #
#.#.# => #
.##.# => #
.#..# => #
#..## => #
##..# => #
#...# => .
...#. => #
##### => .
###.. => #
#.#.. => .
....# => .
.#### => #
..### => .
..##. => #
.##.. => .
#.... => .
####. => #
.#.#. => .
.#... => #
##... => #

View file

@ -0,0 +1,127 @@
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
using namespace std;
unordered_map<string, char> read_rules()
{
unordered_map<string, char> rules;
while(!cin.eof())
{
char result;
string rule;
cin >> rule;
cin.ignore(4); //ignore ' => '
cin >> result;
cin.ignore(1); //ignore '\n'
rules[rule] = result;
}
return rules;
}
vector<char> initial_state()
{
cin.ignore(15); // ignore 'initial state: '
string buf;
getline(cin, buf);
return vector<char>(buf.begin(), buf.end());
}
string environment(vector<char> const &plants, size_t idx)
{
string sig;
switch(idx)
{
case 0:
sig += "..";
sig += string(plants.begin(), plants.begin() + 3);
return sig;
case 1:
sig += ".";
sig += string(plants.begin(), plants.begin() + 4);
return sig;
default:
break;
}
if (idx == (plants.size() - 2))
{
auto it = plants.begin() + idx;
return string(it - 2, it + 2) + '.';
}
else if (idx == (plants.size() - 1))
{
auto it = plants.begin() + idx;
return string(it - 2, it + 1) + "..";
}
auto it = plants.begin() + idx;
return string(it - 2, it + 3);
}
int main()
{
int first = 0;
vector<char> plants = initial_state();
unordered_map<string, char> rules = read_rules();
string original = string(plants.begin(), plants.end());
cout << original << "\n";
for (size_t iterations = 20; iterations--; )
{
if (find(plants.begin(), plants.begin() + 3, '#')
!= plants.begin() + 3)
{
first -= 2;
plants.insert(plants.begin(), {'.', '.'});
}
if (find(plants.end() - 3, plants.end(), '#')
!= plants.end())
{
plants.insert(plants.end(), {'.', '.'});
}
vector<char> nextgen(plants.size(), '.');
// cout << "\n\n # now env new\n";
for (size_t idx = 0; idx != plants.size(); ++idx)
{
// cout << setw(3) << first + static_cast<int>(idx) << " ";
// cout << "[" << plants[idx] << "]: ";
string env = environment(plants, idx);
// cout << env << " => ";
char result = '?';
if (auto it = rules.find(env); it != rules.end())
result = it->second;
// cout << result << "\n";
nextgen[idx] = result;
}
plants.swap(nextgen);
}
// cout << string(-first, ' ') << original << "\n";
// cout << string(plants.begin(), plants.end()) << "\n";
int score = 0;
for (size_t idx = 0; idx < plants.size(); ++idx)
{
score += (plants[idx] == '#' ? idx + first : 0);
}
cout << "score: " << score << "\n";
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,250 @@
jplenqtlagxhivmwmscfukzodp
jbrehqtlagxhivmeyscfuvzodp
jbreaqtlagxzivmwysofukzodp
jxrgnqtlagxhivmwyscfukwodp
jbrenqtwagjhivmwysxfukzodp
jbrenqplagxhivmwyscfuazoip
jbrenqtlagxhivzwyscfldzodp
jbrefqtlagxhizmwyfcfukzodp
jbrenqtlagxhevmwfsafukzodp
jbrunqtlagxrivmsyscfukzodp
jbrenqtlaguhivmwyhlfukzodp
jbrcnstsagxhivmwyscfukzodp
jbrenqtlagozivmwyscbukzodp
jbrenqwlagxhivswysrfukzodp
jbrenstlagxhuvmiyscfukzodp
jbranqtlhgxhivmwysnfukzodp
jbrenqtvagxhinmxyscfukzodp
jbrenqtlagdhivmwyscfukxody
jbrenqtlagelavmwyscfukzodp
jbrenqtlagxhtvmwyhcfukzbdp
jbrenqwlagxhivmwyscfutzopp
jbrenqtlavxhibmtyscfukzodp
jbronqtlagxnivmwyscfuzzodp
jbredqtlagxhppmwyscfukzodp
jbrcnqtlogxhivmwysxfukzodp
jbremqtlagehivswyscfukzodp
jbrenqolagxhivmcyscfukzokp
jbrehqtlacxhgvmwyscfukzodp
fbrlnqtlagxhivmwyscbukzodp
zbrfnqtlagxhivrwyscfukzodp
jbregqtlagxnivmwyscfhkzodp
jbrenqtllgxnivmwystfukzodp
jurenqtlagxhivmwyscfulzoup
jbrenitdagxhivmwyxcfukzodp
jbrenqtlagxqivmwyscvuszodp
jbqenqwlagxhivmwyscfckzodp
jbrenqtlagxhivmwxscqupzodp
jbrenqtlagxhivmwysciuqiodp
jbrjnjtlagxhivmwyscfukzode
jbrenqtlagxhuvmwqscfukzods
jbrenqtlagxhuvmuyscfukzudp
ibrenqtlagxhivmwyscfuktokp
jbsenqtlagxhivcwyscfuksodp
jbrfnntlagxhivmwnscfukzodp
jzrenqulagxhivmwyscfukzodx
jbrenqtqygxhivmwyscfukzwdp
jbrenqtlagxfixmwyscfukzcdp
jbrenqaoagxhivmwyshfukzodp
jbrenqtlazxhivmworcfukzodp
jbrenqtlagxhicowyscfukrodp
jbrqnqtlagxhivzwyzcfukzodp
jbrenqtlalxhuvxwyscfukzodp
jbrenqtlqgxhhviwyscfukzodp
jbrenqtuggxhivmoyscfukzodp
jbrenqtlagxpivmwyscfukkodw
zbrenqhlagxhivmwyscdukzodp
jbrenutlagxxivmwyscfukzoqp
obrenqtlagxhivmwxscfuszodp
jbrenqtlagxhlvmwyscfuixodp
rbrenqtlagdhixmwyscfukzodp
jbrenqtlagxhivmwescfyszodp
jbrfnqtlagxhivmwyscaukzhdp
jbrenqtiagxhivmbyscfuxzodp
cbrrnqtuagxhivmwyscfukzodp
jbkenqtlagxhigmwysufukzodp
jbjewqtlagxhivmwyscfuqzodp
jbrznqtlagxvivmwyscfukzovp
jbrenttlacxhivmwyscfhkzodp
jblenqtlagxhivmwylcfukaodp
jbrenqtlagxhivmqysiftkzodp
jbrenqtlagwhikmwyscfukqodp
jbrenqtlegxhivmwvsckukzodp
jbrenqwzagxhiamwyscfukzodp
jbrenqtlagxhivcwyscfgkzodc
jbrenqtlagxxhvmwxscfukzodp
jbrenqtlngxhivmwyscfukoowp
jbreomtlagxhivmwpscfukzodp
jfrenqtlagxhivmwyscnumzodp
jbrenqtlagphipmwyscfukfodp
jvrenqtlagxhivmwyscfmkzodw
jbrenqtlaxxoiemwyscfukzodp
jbrenqtlagxhivmwyscemkzpdp
jbrenyjldgxhivmwyscfukzodp
jbrenqtlagxhivfvyspfukzodp
kbrenctlakxhivmwyscfukzodp
jbrmhqtlagxhivmwyscfuizodp
jbjenqtlagxlivmbyscfukzodp
jbrenqtlaaxhivmmyshfukzodp
jbronqtlagxhirmvyscfukzodp
jbcrnqtlagxwivmwyscfukzodp
jxrenszlagxhivmwyscfukzodp
jbpenqtlagxhivmwyscfukkody
jbrewqtlawxhivmwyscfukzhdp
jbrenqylagxhivmwlxcfukzodp
jbrenqtlagxxivtwywcfukzodp
jbrenqtlagxhcvgayscfukzodp
jbrenitlagxhivmwiscfukzohp
jbrenutlagxhivmwyscbukvodp
nbrenqtlagxhivmwysnfujzodp
jbrenqtlagxhivmwyqcfupzoop
jbrenqtrarxhijmwyscfukzodp
jbrenqtlagxhivmwysdfukzovy
jbrrnqtlagxhivmwyvcfukzocp
jbrenqtlagxhivmwyscfuvzzhp
jbhenitlagxhivmwysufukzodp
jbrenqtlagxhivmwyscfuwzoqx
kbrenqtlagxhivmwysqfdkzodp
jbrenqtlagxhivmwyxmfukzodx
jbcenatlagxxivmwyscfukzodp
jbrenhtlagvhdvmwyscfukzodp
jxrenqtlafxhivfwyscfukzodp
jbreaztlpgxhivmwyscfukzodp
tqrenqtlagxfivmwyscfukzodp
jbrenqgllgxhwvmwyscfukzodp
jbrejjtlagxhivmgyscfukzodp
jbrenqtlagxhivmwyscvukzoqv
jbrvnqtlagxsibmwyscfukzodp
jbrenqttagxhuvmwyscfukvodp
jbrenqteagxhivmwcscfukqodp
jbrenqtsabxhivmwyspfukzodp
jbbenqtlagxhivmwyscjukztdp
jnrenqtlagxhiimwydcfukzodp
jbrenqtlagxhfvmwyscxukzodu
jbrenqtluyxhiomwyscfukzodp
jbrenqvlagxhivmwyscuukzolp
ebrenqtlagxnivmwysrfukzodp
jbreeqtlatxhigmwyscfukzodp
jbrenqtvxgxhivmwyscfukzedp
jbrmnqtlagxhivmwywcfuklodp
jbreeqtvagxhivmwyscfukzody
jbrenptlagxhivmxyscfumzodp
jbrenqtlagxhivmwysgfukzfsp
jurenqtlagjhivmwkscfukzodp
jbrenntlagxhivmwtscffkzodp
jbrenqglagxhivmwyocfokzodp
wbrenqtlagxhivmwhscfukiodp
jbrenqtligxhivmqascfukzodp
jbrenqtlagxhivmwxscfukpojp
jurenqtlagxhivmmyscfbkzodp
jbrenqtmagxhivmwyscfrbzodp
jcrenqtlagxhivmwysefukzodm
jbrenqtlagxhicmwywcfukzodl
jbvanqtlagfhivmwyscfukzodp
jbmenqjlagxhivmwyscfdkzodp
jbrenqtlagohivvwysctukzodp
jbrenqtdagxdivmwyscfckzodp
kbrefqtlagxhivmwyscfuazodp
jbrwnqtoagxhivmwyswfukzodp
jjhenqtlagxhivmwyscfukzorp
jbgsnqtlagxhivkwyscfukzodp
jbrynqtlagxhivmsyspfukzodp
jbrenftlmkxhivmwyscfukzodp
nbrenqtxagxhmvmwyscfukzodp
jbrunqtlagxhijmwysmfukzodp
jbrenqtlagmaivmwyscfukzowp
jbrerqtlagxhihmwyscfukzudp
jbrenqtlagahivmwysckukzokp
kbrenqtlagxhirmwyscfupzodp
jbrrnqtlagxhivmwyecfukzodz
jbrenqtlavxhivmwyscfusiodp
jnrenqtlagxhivmwyhcfukzodw
jbretqtlagfhivmwyscfukzrdp
jbreoqtnagxhivmwyscfukzopp
jbrenbtllgxhivmwsscfukzodp
jbrenqtlmgxhivmwyscfuwzedp
jbnenqtlagxhivbwyscfukzokp
jbrenqslagxhivmvyscfukzndp
jbrenqtlagxzivmwuscfukztdp
gbrenqtlagxhyvmwyscfukjodp
jbrenqteagxhivmwyscfuszedp
jbrenqtlaglhivmwysafukkodp
jbrenqtlagxhcvmwascfukzogp
jbrenqtlagxhsvmkcscfukzodp
jbrenqslbgxhivmwyscfufzodp
cbrenqtlagxhivkwtscfukzodp
jbrenqtltgxhivmzyscfukzodj
jbrgnqtlagihivmwyycfukzodp
vbrenqauagxhivmwyscfukzodp
jbrqnqtlagjhivmwyscfqkzodp
jbrenqtlakxhivmwyscfukvobp
jcrenqelagxhivmwtscfukzodp
jbrrnqtlagxhlvmwyscfukzodw
jbrenqtlagxhivmwkscaumzodp
jbrenqdlagxhivmiescfukzodp
jhrenqtlagxhqvmwyscmukzodp
jbrenqtlaghhivmwyscfukkoyp
jowenqtlagxyivmwyscfukzodp
jbrenitaagxhivmwyscfqkzodp
jbrenqtlagxhivmwyscfnkbudp
jbyenqtlagxhivmiyscfukzhdp
jbrenitlagxhibjwyscfukzodp
jbrenqtlavxhjvmwpscfukzodp
jbrenqyaagxhivmwyscflkzodp
jbrenqylagxhivmwyicfupzodp
jbrenqtlagxmevmwylcfukzodp
lbrenqtlagxhiqmwyscfikzodp
jbrenqtnarxhivmwyscfmkzodp
jbrenqtlamxhivmwyscfnkzorp
jbbenqtlavxhivmwyscjukztdp
jbrenqtlagxhivmwyscfnliodp
jbwenetlagxhivmwyscfukzodt
jbrenatlagxhivmwysmfujzodp
jbrsnstlagxhivmwyscfukgodp
jbwvnitlagxhivmwyscfukzodp
jbrenqtbagxhkvmwypcfukzodp
jbrlnqtlafxhivmwyscfukdodp
jbrenztlanxhivmwyscjukzodp
jbrepqtlagxhivmwudcfukzodp
jbrenqtlagxiivmwdscfskzodp
jbrdjqtlagxhivmwyschukzodp
jbrenqtoaguhivmwyccfukzodp
jdrexqjlagxhivmwyscfukzodp
jbrenqtlagxhovmwysckukaodp
pbrfnqblagxhivmwyscfukzodp
jbrenqtlagxrivgiyscfukzodp
jbrelqtgagxhivmryscfukzodp
jbrenqtlagxhicmwjscfikzodp
jbrenqjlagxhivmwyscfmkjodp
jbrenqtlagxpivmwzscgukzodp
jbienqzlagxpivmwyscfukzodp
jbrenqvlagxhivmwdscfukzodx
owrenqtlagxhivmwyicfukzodp
gbrenqtlaathivmwyscfukzodp
jbgenqtlafxhivmwysqfukzodp
jbrenqtlagxhivtwsscfukzokp
jbrnnqylanxhivmwyscfukzodp
ebrenqolagxhivmcyscfukzodp
jbrenqtlarnhivgwyscfukzodp
jbmenqtlagxhivmvyscfukzgdp
jbrevqtlaglhivmwystfukzodp
jbrenqplanthivmwyscfukzodp
jbrenqtlagxhivmkyscbukzosp
jbrenqtlagxaivmwyscfugzodo
jbrenqplagxhnvmwyscfjkzodp
jbrenqtlagxhivgwyscfusrodp
cbrenqtlagxhivmwysmfukzody
jbrenquwaexhivmwyscfukzodp
jbredqtlagxhdvmwyscfukzoup
jbrxnqtlagxhivmwysczukaodp
jbrenqtlafnhivmwyscfuczodp
jbbdkqtlagxhivmwyscfukzodp
ubrenqtlagxhivkwyucfukzodp
ebjenqtlagxhivmwyscfukzodj
jbgenqtlugxxivmwyscfukzodp
jbrenqtoagxqivmwdscfukzodp
bbeenqtlagxhivmwyscfumzodp
jbfeeqtlagxhivmwmscfukzodp
jbrlnqtlagxhiimwescfukzodp
jbrenqtlagwoivmwyscfukhodp
jbrenqtlagnhivmwyscfuszovp

View file

@ -0,0 +1,40 @@
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main()
{
size_t dubbles = 0;
size_t tripples = 0;
string buffer;
while(getline(cin, buffer))
{
bool twice = false;
bool thrice = false;
size_t letters[26] = { };
for (char c : buffer)
{
++letters[c - 'a'];
}
for (size_t idx = 0; idx < 26; ++idx)
{
if (letters[idx] == 2)
twice = true;
if (letters[idx] == 3)
thrice = true;
}
if (twice)
++dubbles;
if (thrice)
++tripples;
}
cout << "checsum: " << dubbles * tripples << "\n";
}

View file

@ -0,0 +1,56 @@
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
size_t label_diff(string const &lhs, string const &rhs)
{
size_t diff = 0;
for (size_t idx = 0; idx != lhs.size(); ++idx)
diff += lhs[idx] - rhs[idx] ? 1 : 0;
return diff;
}
string equal_part(string const &lhs, string const &rhs)
{
string rval;
rval.reserve(lhs.size() - 1);
for (size_t idx = 0; idx != lhs.size(); ++idx)
{
if (lhs[idx] == rhs[idx])
rval.push_back(lhs[idx]);
}
return rval;
}
int main()
{
size_t dubbles = 0;
size_t tripples = 0;
string buffer;
vector<string> labels;
while (getline(cin, buffer))
labels.push_back(buffer);
for (string const &lhs : labels)
{
for (string const &rhs : labels)
{
size_t difference = label_diff(rhs, lhs);
if (difference == 1)
{
cout << "Equal part: " << equal_part(lhs, rhs) << "\n";
return 0;
}
}
}
}
//jbbenqtlaxhivmwyscjukztdp correct

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,137 @@
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Star
{
int x;
int y;
int z;
int t;
};
istream& operator>>(istream& in, Star &star)
{
in >> star.x;
in.ignore(); //ignore ','
in >> star.y;
in.ignore();
in >> star.z;
in.ignore();
in >> star.t;
in.ignore();
return in;
}
ostream &operator<<(ostream &out, Star const &star)
{
out << "[" << star.x << ", " << star.y << ", " << star.z << ", " << star.t << "]";
return out;
}
size_t distance(Star const &lhs, Star const &rhs)
{
int dist = abs(lhs.x - rhs.x)
+ abs(lhs.y - rhs.y)
+ abs(lhs.z - rhs.z)
+ abs(lhs.t - rhs.t);
return dist;
}
bool in_cluster(vector<Star> const &cluster, Star const &star)
{
for (Star clst : cluster)
{
if (distance(clst, star) <= 3)
return true;
}
return false;
}
bool same_cluster(vector<Star> const &lhs, vector<Star> const &rhs)
{
for (Star left : lhs)
{
for (Star right : rhs)
{
if (distance(left, right) <= 3)
return true;
}
}
return false;
}
int main()
{
vector<Star> stars;
while (cin)
{
Star star;
cin >> star;
stars.push_back(star);
}
// kickstart 1st cluster
vector<vector<Star>> clusters;
for (Star star : stars)
{
bool matched = false;
for (vector<Star> &cluster : clusters)
{
if (in_cluster(cluster, star))
{
cluster.push_back(star);
matched = true;
break;
}
}
if (!matched)
{
clusters.push_back({});
clusters.back().push_back(star);
}
}
bool merged = true;
while (merged)
{
merged = false;
for (size_t idx = 0; idx < clusters.size(); ++idx)
{
for (size_t jdx = idx + 1; jdx < clusters.size(); ++jdx)
{
if (same_cluster(clusters[idx], clusters[jdx]))
{
clusters[idx].insert(clusters[idx].end(), clusters[jdx].begin(), clusters[jdx].end());
clusters.erase(clusters.begin() + jdx);
merged = true;
break;
}
}
if (merged) break;
}
}
for (vector<Star> cluster : clusters)
{
cout << "\n\nCluster:\n";
for (Star star : cluster)
{
cout << " Star: " << star << "\n";
}
}
cout << "Num clusters: " << clusters.size() << "\n";
}

View file

@ -0,0 +1,10 @@
1,-1,0,1
2,0,-1,0
3,2,-1,0
0,0,3,1
0,0,-1,-1
2,3,-2,0
-2,2,0,0
2,-2,0,-1
1,-1,0,-1
3,2,0,2

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,83 @@
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Claim
{
size_t id;
size_t px;
size_t py;
size_t width;
size_t height;
};
istream &operator>>(istream &in, Claim &claim)
{
// reading into char in favor if in.ignore(), to automatically
// toss any present whitespace.
char at;
cin >> at; //ignore '#'
in >> claim.id;
in >> at; //ignore '@'
in >> claim.px;
cin >> at; //ignore ','
in >> claim.py;
in >> at; //ignore ':'
in >> claim.width;
in >> at; //ignore 'x'
in >> claim.height;
return in;
}
void layout_claim(vector<vector<int>> &canvas, Claim const &claim)
{
size_t end_x = claim.px + claim.width;
size_t end_y = claim.py + claim.height;
for (size_t px = claim.px; px != end_x; ++px)
{
for (size_t py = claim.py; py != end_y; ++py)
++canvas[px][py];
}
}
int main()
{
size_t total_width = 0;
size_t total_height = 0;
vector<Claim> claims;
vector<vector<int>> canvas;
while (!cin.eof())
{
Claim claim;
cin >> claim;
total_width = max(total_width, claim.px + claim.width);
total_height = max(total_height, claim.py + claim.height);
claims.push_back(claim);
}
canvas.resize(total_width, vector<int>(total_height, 0));
for (Claim const &claim : claims)
layout_claim(canvas, claim);
size_t overlap = 0;
for (size_t py = 0; py < total_height; ++py)
{
for (size_t px = 0; px < total_width; ++px)
{
if (canvas[px][py] > 1)
++overlap;
}
}
cout << "Overlap: " << overlap << "\n";
}

View file

@ -0,0 +1,105 @@
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Claim
{
size_t id;
size_t px;
size_t py;
size_t width;
size_t height;
};
istream &operator>>(istream &in, Claim &claim)
{
// reading into char in favor if in.ignore(), to automatically
// toss any present whitespace.
char at;
cin >> at; //ignore '#'
in >> claim.id;
in >> at; //ignore '@'
in >> claim.px;
cin >> at; //ignore ','
in >> claim.py;
in >> at; //ignore ':'
in >> claim.width;
in >> at; //ignore 'x'
in >> claim.height;
return in;
}
void layout_claim(vector<vector<int>> &canvas, Claim const &claim)
{
size_t end_x = claim.px + claim.width;
size_t end_y = claim.py + claim.height;
for (size_t px = claim.px; px != end_x; ++px)
{
for (size_t py = claim.py; py != end_y; ++py)
++canvas[px][py];
}
}
bool overlaps(vector<vector<int>> &canvas, Claim const &claim)
{
size_t end_x = claim.px + claim.width;
size_t end_y = claim.py + claim.height;
for (size_t px = claim.px; px != end_x; ++px)
{
for (size_t py = claim.py; py != end_y; ++py)
{
if (canvas[px][py] != 1)
return true;
}
}
return false;
}
int main()
{
size_t total_width = 0;
size_t total_height = 0;
vector<Claim> claims;
vector<vector<int>> canvas;
while (!cin.eof())
{
Claim claim;
cin >> claim;
total_width = max(total_width, claim.px + claim.width);
total_height = max(total_height, claim.py + claim.height);
claims.push_back(claim);
}
canvas.resize(total_width, vector<int>(total_height, 0));
for (Claim const &claim : claims)
layout_claim(canvas, claim);
size_t overlap = 0;
for (size_t py = 0; py < total_height; ++py)
{
for (size_t px = 0; px < total_width; ++px)
{
if (canvas[px][py] > 1)
++overlap;
}
}
for (size_t idx = 0; idx != claims.size(); ++idx)
{
if (!overlaps(canvas, claims[idx]))
cout << "No overlap for " << idx << "\n";
}
}

View file

@ -0,0 +1,3 @@
#1 @ 1,3: 4x4
#2 @ 3,1: 4x4
#3 @ 5,5: 2x2

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,159 @@
#include <algorithm>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
struct Event
{
tm time;
string event;
};
istream &operator>>(istream &in, Event &event)
{
char toss;
in >> toss; // toss '['
in >> event.time.tm_year;
event.time.tm_year -= 1900;
in >> toss; // toss '-'
in >> event.time.tm_mon;
in >> toss; // toss '-'
in >> event.time.tm_mday;
in >> event.time.tm_hour;
in >> toss; // toss ':'
in >> event.time.tm_min;
in >> toss; // toss ']'
getline(cin, event.event);
return in;
}
ostream &operator<<(ostream &out, Event const &event)
{
out << "Event: \n";
out << " " << put_time(&event.time, "%c") << "\n";
out << " - " << event.event << "\n";
return out;
}
int main()
{
vector<Event> events;
map<int, int[60]> guards;
cout << setfill(' ');
while(!cin.eof())
{
Event event{};
cin >> event;
events.push_back(event);
}
std::sort(events.begin(), events.end(),
[](Event &lhs, Event &rhs)
{
time_t l_time = mktime(&lhs.time);
time_t r_time = mktime(&rhs.time);
return l_time < r_time;
});
int current_id;
for (Event const &event : events)
{
size_t numstart = event.event.find("#");
if (numstart != string::npos)
{
size_t end = event.event.find(" ", numstart);
current_id = stoi(event.event.substr(numstart + 1, end - numstart - 1));
}
else if (event.event.find("asleep") != string::npos)
{
for (size_t idx = event.time.tm_min; idx != 60; ++idx)
++guards[current_id][idx];
}
else if (event.event.find("wakes") != string::npos)
{
for (size_t idx = event.time.tm_min; idx != 60; ++idx)
--guards[current_id][idx];
}
}
size_t max_id = 0;
size_t sleeper = 0;
// cout << " Guard ID Minute\n";
// cout << " 1 2 3 4 5\n";
// cout << " 012345678901234567890123456789012345678901234567890123456789\n";
cout << " ";
for (size_t idx = 0; idx != 60; ++idx)
cout << setw(3) << idx;
cout << " | total\n";
for (auto const &[id, sleep] : guards)
{
cout << "Guard " << setw(5) << id << ": ";
size_t total = 0;
for (int num : sleep)
{
cout << setw(3) << num;
total += num;
}
cout << " | " << total << "\n";
if (total > sleeper)
{
max_id = id;
sleeper = total;
}
}
cout << "Max sleeper: " << max_id << "\n";
size_t min_id = 0;
sleeper = 0;
for (size_t idx = 0; idx != 60; ++idx)
{
if (guards[max_id][idx] > sleeper)
{
sleeper = guards[max_id][idx];
min_id = idx;
}
}
cout << "Slept most in minute " << min_id << ", for " << sleeper << " minutes.\n";
sleeper = 0;
min_id = 0;
size_t minute = 0;
for (auto const &[id, sleep] : guards)
{
for (size_t idx = 0; idx != 60; ++idx)
{
if (sleep[idx] >= sleeper)
{
sleeper = sleep[idx];
min_id = id;
minute = idx;
}
}
}
cout << "Guard " << min_id << " is most frequently asleep on minute ";
cout << minute << "\n";
cout << sleeper * minute << "\n";
}

View file

@ -0,0 +1,17 @@
[1518-11-01 00:00] Guard #10 begins shift
[1518-11-01 00:05] falls asleep
[1518-11-01 00:25] wakes up
[1518-11-01 00:30] falls asleep
[1518-11-01 00:55] wakes up
[1518-11-01 23:58] Guard #99 begins shift
[1518-11-02 00:40] falls asleep
[1518-11-02 00:50] wakes up
[1518-11-03 00:05] Guard #10 begins shift
[1518-11-03 00:24] falls asleep
[1518-11-03 00:29] wakes up
[1518-11-04 00:02] Guard #99 begins shift
[1518-11-04 00:36] falls asleep
[1518-11-04 00:46] wakes up
[1518-11-05 00:03] Guard #99 begins shift
[1518-11-05 00:45] falls asleep
[1518-11-05 00:55] wakes up

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,56 @@
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
size_t collapse_polymer(vector<char> polymer)
{
int react_diff = abs('a' - 'A');
size_t oldsize = 0;
while(oldsize != polymer.size())
{
oldsize = polymer.size();
for (size_t idx = 0; idx < polymer.size() - 1; ++idx)
{
if (abs(polymer[idx] - polymer[idx + 1]) == react_diff)
{
polymer.erase(polymer.begin() + idx, polymer.begin() + idx + 2);
--idx;
}
}
}
return polymer.size();
}
int main()
{
vector<char> polymer {
istreambuf_iterator<char>(cin),
istreambuf_iterator<char>()
};
cout << "Read polymer of size: " << polymer.size() << "\n";
size_t minsize = polymer.size();
for (size_t idx = 0; idx < 26; ++idx)
{
char current = 'a' + idx;
vector<char> filtered{polymer};
auto it = remove_if(filtered.begin(), filtered.end(), [=](char c)
{
if (c == current || c == toupper(current))
return true;
return false;
});
filtered.erase(it, filtered.end());
minsize = min(collapse_polymer(filtered), minsize);
}
cout << "Smallest collapsed polymer size: " << minsize << "\n";
}

View file

@ -0,0 +1,50 @@
118, 274
102, 101
216, 203
208, 251
309, 68
330, 93
91, 179
298, 278
201, 99
280, 272
141, 312
324, 290
41, 65
305, 311
198, 68
231, 237
164, 224
103, 189
216, 207
164, 290
151, 91
166, 250
129, 149
47, 231
249, 100
262, 175
299, 237
62, 288
228, 219
224, 76
310, 173
80, 46
312, 65
183, 158
272, 249
57, 141
331, 191
163, 359
271, 210
142, 137
349, 123
55, 268
160, 82
180, 70
231, 243
133, 353
246, 315
164, 206
229, 97
268, 94

View file

@ -0,0 +1,125 @@
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Point
{
size_t x;
size_t y;
Point()
: x(0), y(0)
{ }
Point(size_t x, size_t y)
: x(x), y(y)
{ }
};
istream &operator>>(istream& in, Point &point)
{
cin >> point.x;
cin.ignore(); // ignore ','
cin >> point.y;
return in;
}
inline size_t manhattan_distance(Point const &p1, Point const &p2)
{
return (p1.x > p2.x ? p1.x - p2.x : p2.x - p1.x)
+ (p1.y > p2.y ? p1.y - p2.y : p2.y - p1.y);
}
int closest(Point location, vector<Point> const &points)
{
int index = -1;
size_t mindist = std::numeric_limits<size_t>::max();
for (size_t idx = 0; idx != points.size(); ++idx)
{
size_t distance = manhattan_distance(location, points[idx]);
if (distance == mindist)
index = -1;
else if (distance < mindist)
{
mindist = distance;
index = idx;
}
}
return index;
}
int main()
{
size_t width = 0;
size_t height = 0;
vector<Point> points;
while(!cin.eof())
{
Point point;
cin >> point;
points.push_back(point);
width = max(width, point.x + 1);
height = max(height, point.y + 1);
}
cout << "grid size: " << width << " x " << height << "\n";
vector<int> counts(points.size(), 0);
for (size_t idx = 0; idx != width; ++idx)
{
for (size_t idy = 0; idy != height; ++idy)
{
int index = closest(Point{idx, idy}, points);
if (index == -1) continue;
++counts[index];
}
}
// if a class hits the edge its infinite.
// we check highest and lowers rows first.
for (size_t idx = 0; idx != width; ++idx)
{
int index = closest(Point{idx, 0}, points);
int index2 = closest(Point{idx, height - 1}, points);
if (index != -1)
counts[index] = -1;
if (index2 != -1)
counts[index2] = -1;
}
//side rows
for (size_t idx = 0; idx != height; ++idx)
{
int index = closest(Point{0, idx}, points);
int index2 = closest(Point{width - 1, idx}, points);
if (index != -1)
counts[index] = -1;
if (index2 != -1)
counts[index2] = -1;
}
size_t maxclass = 0;
int space = counts[0];
for (size_t idx = 1; idx != counts.size(); ++idx)
{
if (counts[idx] > space)
{
space = counts[idx];
maxclass = idx;
}
}
cout << "biggest class is " << maxclass << " with a size of " << space << "\n";
}

View file

@ -0,0 +1,73 @@
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Point
{
size_t x;
size_t y;
Point()
: x(0), y(0)
{ }
Point(size_t x, size_t y)
: x(x), y(y)
{ }
};
istream &operator>>(istream& in, Point &point)
{
cin >> point.x;
cin.ignore(); // ignore ','
cin >> point.y;
return in;
}
inline size_t manhattan_distance(Point const &p1, Point const &p2)
{
return (p1.x > p2.x ? p1.x - p2.x : p2.x - p1.x)
+ (p1.y > p2.y ? p1.y - p2.y : p2.y - p1.y);
}
int main()
{
size_t width = 0;
size_t height = 0;
vector<Point> points;
while(!cin.eof())
{
Point point;
cin >> point;
points.push_back(point);
width = max(width, point.x + 1);
height = max(height, point.y + 1);
}
size_t region_size = 0;
size_t distance_limit = 10000;
for (size_t idx = 0; idx != width; ++idx)
{
for (size_t idy = 0; idy != height; ++idy)
{
size_t sum = 0;
Point current {idx, idy};
for_each(points.begin(), points.end(), [&](Point const &point)
{
sum += manhattan_distance(current, point);
});
if (sum < distance_limit)
region_size++;
}
}
cout << "region size: " << region_size << "\n";
}

View file

@ -0,0 +1,101 @@
Step J must be finished before step K can begin.
Step N must be finished before step X can begin.
Step S must be finished before step G can begin.
Step T must be finished before step R can begin.
Step H must be finished before step L can begin.
Step V must be finished before step W can begin.
Step G must be finished before step U can begin.
Step K must be finished before step A can begin.
Step D must be finished before step Z can begin.
Step C must be finished before step E can begin.
Step X must be finished before step P can begin.
Step Y must be finished before step U can begin.
Step R must be finished before step O can begin.
Step W must be finished before step U can begin.
Step O must be finished before step Q can begin.
Step A must be finished before step P can begin.
Step B must be finished before step E can begin.
Step F must be finished before step E can begin.
Step Q must be finished before step U can begin.
Step M must be finished before step E can begin.
Step P must be finished before step U can begin.
Step L must be finished before step Z can begin.
Step Z must be finished before step U can begin.
Step U must be finished before step E can begin.
Step I must be finished before step E can begin.
Step H must be finished before step G can begin.
Step X must be finished before step I can begin.
Step K must be finished before step X can begin.
Step Z must be finished before step I can begin.
Step S must be finished before step M can begin.
Step L must be finished before step U can begin.
Step A must be finished before step M can begin.
Step W must be finished before step A can begin.
Step N must be finished before step A can begin.
Step S must be finished before step E can begin.
Step W must be finished before step Q can begin.
Step J must be finished before step L can begin.
Step Q must be finished before step L can begin.
Step M must be finished before step U can begin.
Step H must be finished before step E can begin.
Step D must be finished before step E can begin.
Step V must be finished before step P can begin.
Step Q must be finished before step M can begin.
Step X must be finished before step W can begin.
Step K must be finished before step I can begin.
Step T must be finished before step H can begin.
Step Y must be finished before step L can begin.
Step G must be finished before step O can begin.
Step M must be finished before step Z can begin.
Step F must be finished before step Z can begin.
Step Q must be finished before step E can begin.
Step H must be finished before step C can begin.
Step Q must be finished before step P can begin.
Step D must be finished before step U can begin.
Step Z must be finished before step E can begin.
Step O must be finished before step M can begin.
Step L must be finished before step I can begin.
Step J must be finished before step A can begin.
Step Q must be finished before step Z can begin.
Step P must be finished before step I can begin.
Step K must be finished before step O can begin.
Step R must be finished before step E can begin.
Step W must be finished before step F can begin.
Step D must be finished before step Q can begin.
Step R must be finished before step U can begin.
Step W must be finished before step P can begin.
Step S must be finished before step Z can begin.
Step T must be finished before step P can begin.
Step B must be finished before step Q can begin.
Step S must be finished before step T can begin.
Step R must be finished before step A can begin.
Step K must be finished before step R can begin.
Step N must be finished before step G can begin.
Step C must be finished before step W can begin.
Step T must be finished before step A can begin.
Step B must be finished before step Z can begin.
Step C must be finished before step P can begin.
Step D must be finished before step P can begin.
Step B must be finished before step P can begin.
Step F must be finished before step U can begin.
Step V must be finished before step X can begin.
Step K must be finished before step W can begin.
Step Y must be finished before step I can begin.
Step C must be finished before step B can begin.
Step X must be finished before step L can begin.
Step X must be finished before step M can begin.
Step H must be finished before step P can begin.
Step S must be finished before step F can begin.
Step J must be finished before step Y can begin.
Step Y must be finished before step Z can begin.
Step B must be finished before step I can begin.
Step S must be finished before step C can begin.
Step K must be finished before step E can begin.
Step N must be finished before step Q can begin.
Step A must be finished before step Z can begin.
Step J must be finished before step I can begin.
Step Y must be finished before step O can begin.
Step Y must be finished before step F can begin.
Step S must be finished before step U can begin.
Step D must be finished before step W can begin.
Step V must be finished before step D can begin.

View file

@ -0,0 +1,63 @@
#include <algorithm>
#include <array>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Constraint
{
bool placed = false;
vector<char> requires;
vector<char> required_by;
};
bool satisfied(Constraint const &constraint, string const &plan)
{
for (char c : constraint.requires)
{
if (plan.find(c) == string::npos)
return false;
}
return true;
}
int main()
{
array<Constraint, 26> constraints {};
while (!cin.eof())
{
char step;
char requires;
cin.ignore(5); // 'Step '
cin >> requires;
cin.ignore(30); // ' must be finished before step '
cin >> step;
cin.ignore(12); //' can begin.\n'
cout << step << " requires " << requires << "\n";
constraints[step - 'A'].requires.push_back(requires);
constraints[requires - 'A'].required_by.push_back(step);
}
string plan;
while(plan.size() != 26)
{
for (size_t idx = 0; idx < 26; ++idx)
{
if (!constraints[idx].placed && satisfied(constraints[idx], plan))
{
plan += static_cast<char>('A' + idx);
constraints[idx].placed = true;
break;
}
}
cout << plan.size() << " / '" << plan << "'\n";
}
cout << plan.size() << " / '" << plan << "'\n";
}

View file

@ -0,0 +1,142 @@
#include <algorithm>
#include <array>
#include <iostream>
#include <string>
#include <vector>
#include <chrono>
#include <thread>
#include <unistd.h>
using namespace std;
struct Constraint
{
bool placed = false;
vector<char> requires;
vector<char> required_by;
};
struct Task
{
char step = '\0';
size_t time_left = 0;
};
bool satisfied(Constraint const &constraint, string const &plan)
{
for (char c : constraint.requires)
{
if (plan.find(c) == string::npos)
{
cout << "Constraint not met in plan '" << plan << "', missing " << c << "\n";
return false;
}
}
return true;
}
char find_satisfied(array<Constraint, 26> const &constraints, string const&plan, string const&goal)
{
cout << "Finding next step from plan '" << plan << "', already done: '" << goal << "'\n";
for (char c : plan)
{
if (satisfied(constraints[c - 'A'], goal))
return c;
}
return 0;
}
int main()
{
array<Constraint, 26> constraints {};
size_t plansize = 26;
size_t baseline = 60;
while (!cin.eof())
{
char step;
char requires;
cin.ignore(5); // 'Step '
cin >> requires;
cin.ignore(30); // ' must be finished before step '
cin >> step;
cin.ignore(12); //' can begin.\n'
cout << step << " requires " << requires << "\n";
constraints[step - 'A'].requires.push_back(requires);
constraints[requires - 'A'].required_by.push_back(step);
}
string plan;
while(plan.size() != plansize)
{
for (size_t idx = 0; idx < 26; ++idx)
{
if (!constraints[idx].placed && satisfied(constraints[idx], plan))
{
plan += static_cast<char>('A' + idx);
constraints[idx].placed = true;
break;
}
}
cout << plan.size() << " / '" << plan << "'\n";
}
cout << plan.size() << " / '" << plan << "'\n";
/************************************/
/************************************/
/************************************/
string done;
Task workers[5];
size_t duration = 0;
while (done.size() != plansize)
{
++duration;
for(Task &task : workers)
if (task.time_left != 0) --task.time_left;
for (Task &task : workers)
{
if (task.time_left == 0 && task.step != '\0')
{
done += task.step;
task.step = '\0';
}
}
for (Task &task : workers)
{
char next = find_satisfied(constraints, plan, done);
if (next == 0) break;
if (task.time_left == 0)
{
if (plan.size() > 0 && satisfied(constraints[next - 'A'], done))
{
task.time_left = baseline + (next - 'A') + 1;
task.step = next;
plan.erase(plan.find(next), 1);
}
}
}
cout << "Plan / Done: '" << plan << "' / '" << done << "'\n";
cout << "Workers on: \n";
for (Task &task : workers)
{
cout << " Step: " << (task.step == '\0' ? '-' : task.step) << "\n";
cout << " Time: " << task.time_left << "\n";
}
cout << "\n";
// this_thread::sleep_for(std::chrono::milliseconds(1000));
}
cout << "Total time: " << duration << "s \n";
}

View file

@ -0,0 +1,7 @@
Step C must be finished before step A can begin.
Step C must be finished before step F can begin.
Step A must be finished before step B can begin.
Step A must be finished before step D can begin.
Step B must be finished before step E can begin.
Step D must be finished before step E can begin.
Step F must be finished before step E can begin.

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,83 @@
#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
using namespace std;
struct Node
{
size_t child_count;
size_t metadata_count;
vector<Node> children;
vector<int> metadata;
Node()
: child_count(0), metadata_count(0)
{ }
};
size_t metadata_sum = 0;
Node parse_node(vector<int> &data)
{
Node rval;
rval.child_count = data.back();
data.pop_back();
rval.metadata_count = data.back();
data.pop_back();
for (size_t idx = 0; idx != rval.child_count; ++idx)
{
rval.children.push_back(parse_node(data));
}
for (size_t idx = 0; idx != rval.metadata_count; ++idx)
{
rval.metadata.push_back(data.back());
data.pop_back();
metadata_sum += rval.metadata.back();
}
return rval;
}
size_t node_value(Node const &node)
{
if (node.children.empty())
{
return accumulate(node.metadata.begin(), node.metadata.end(), 0);
}
size_t sum = 0;
for (int idx : node.metadata)
{
int index = idx - 1;
if (index >= node.children.size())
continue;
sum += node_value(node.children[index]);
}
return sum;
}
int main()
{
vector<int> data;
while(!cin.eof())
{
int tmp;
cin >> tmp;
data.push_back(tmp);
}
vector<int> copy(data.rbegin(), data.rend()); //reverse data
Node root = parse_node(copy);
cout << "metadata sum: " << metadata_sum << "\n"; // answer to 1
cout << "node value: " << node_value(root) << "\n"; // answer to 2
}

View file

@ -0,0 +1 @@
2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2

View file

@ -0,0 +1 @@
468 players; last marble is worth 7184300 points

View file

@ -0,0 +1,120 @@
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
/* Slow-as-fuck solution:
$ time ./a.out < input.in
Num players: 468
Last marble: 7184300
Best player had a score of 3156297594
./a.out < input.in 5143.54s user 1.02s system 99% cpu 1:25:53.91 total
*/
struct Game
{
size_t num_players;
size_t last_marble;
};
istream &operator>>(istream &in, Game &game)
{
cin >> game.num_players;
cin.ignore(31); // ignore ' players; last marble is worth '.
cin >> game.last_marble;
cin.ignore(8); // ignore ' points\n'
return in;
}
size_t index(size_t main, size_t size)
{
main += 2;
main = main % size;
return main;
}
void print_gamestate(size_t round, vector<size_t> const &game, size_t main)
{
cout << setw(7) << ("["s + to_string(round) + "]: "s);
for (size_t idx = 0; idx != game.size(); ++idx)
{
if (idx == main)
cout << setw(5) << ("("s + to_string(game[idx]) + ")"s);
else
cout << setw(5) << game[idx];
}
cout << "\n";
}
size_t insert_marble(vector<size_t> *game, size_t main, size_t marble)
{
size_t before = (main + 2) % game->size();
if (before == 0)
{
game->push_back(marble);
return game->size() - 1;
}
game->insert(game->begin() + before, marble);
return before;
}
size_t bonus_marble(size_t main, size_t size)
{
if (main < 7)
return size - (7 - main);
return main - 7;
}
int main()
{
Game game;
cin >> game;
cout << "Num players: " << game.num_players << "\n";
cout << "Last marble: " << game.last_marble << "\n";
size_t main_marble = 0;
vector<size_t> players;
players.resize(game.num_players, 0);
vector<size_t> marbles { 0 };
// print_gamestate(0, marbles, main_marble);
for (size_t idx = 1; idx != game.last_marble + 1; ++idx)
{
cout << "Progress: " << idx / static_cast<float>(game.last_marble) * 100 << "\r";
if (idx % 23 == 0) //special case
{
size_t player = (idx - 1) % game.num_players;
players[player] += idx;
main_marble = bonus_marble(main_marble, marbles.size());
players[player] += marbles[main_marble];
marbles.erase(marbles.begin() + main_marble);
}
else
{
main_marble = insert_marble(&marbles, main_marble, idx);
}
// print_gamestate(idx, marbles, main_marble);
}
size_t best_player = 0;
for (size_t idx = 1; idx < players.size(); ++idx)
{
if (players[best_player] < players[idx])
best_player = idx;
}
cout << "Best player had a score of " << players[best_player] << "\n";
}

View file

@ -0,0 +1,146 @@
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
/*
$ time ./a.out < input.in
Num players: 468
Last marble: 7184300
Highest reached score: 3156297594
./a.out < input.in 0.16s user 0.05s system 99% cpu 0.203 total
*/
struct Game
{
size_t num_players;
size_t last_marble;
};
istream &operator>>(istream &in, Game &game)
{
cin >> game.num_players;
cin.ignore(31); // ignore ' players; last marble is worth '.
cin >> game.last_marble;
cin.ignore(8); // ignore ' points\n'
return in;
}
struct List
{
size_t marble;
List *next;
List *previous;
List(size_t num)
: marble(num), next(this), previous(this)
{ }
List *rotate(int direction)
{
List *rval = this;
if (direction > 0)
{
for (; direction--; )
rval = rval->next;
}
else
{
direction *= -1;
for (; direction--; )
rval = rval->previous;
}
return rval;
}
List* insert_after(List *entry) //assume single entry
{
entry->next = next;
entry->next->previous = entry;
entry->previous = this;
next = entry;
return entry;
}
//returns next in list. or nullptr when list size == 1
List* remove_current()
{
if (next == this)
{
delete this;
return nullptr;
}
next->previous = previous;
previous->next = next;
List *rval = next;
delete this;
return rval;
}
void print_list()
{
List *current = next;
cout << setw(5) << marble;
while (current != this)
{
cout << setw(5) << current->marble;
current = current->next;
}
}
};
int main()
{
Game game;
cin >> game;
cout << "Num players: " << game.num_players << "\n";
cout << "Last marble: " << game.last_marble << "\n";
List *root = new List(0);
List *main = root;
vector<size_t> players;
players.resize(game.num_players, 0);
for (size_t idx = 1; idx < game.last_marble + 1; ++idx)
{
if (idx % 23 == 0)
{
size_t player = (idx + 1) % game.num_players;
players[player] += idx;
main = main->rotate(-7); //?
players[player] += main->marble;
main = main->remove_current();
}
else
{
main = main->rotate(1)->insert_after(new List(idx));
}
}
size_t player = 0;
for (size_t idx = 0; idx < game.num_players; ++idx)
{
if (players[player] < players[idx])
player = idx;
}
cout << "Highest reached score: " << players[player] << "\n";
while(root)
root = root->remove_current();
}

View file

@ -0,0 +1 @@
10 players; last marble is worth 1618 points

View file

@ -0,0 +1,100 @@
84242
87800
131272
84629
105398
91086
99863
146591
58757
51370
108422
95689
91513
113692
79189
113603
52750
123562
147710
145313
98785
86959
89755
97093
62048
98872
145829
76682
65788
119356
124600
69459
80167
56122
117390
72303
141896
140568
82565
75431
54613
124106
104628
78531
63748
139285
111926
101999
53435
57906
58120
146795
147754
79892
65395
121551
50577
122520
66441
86009
121899
71715
112666
112863
140695
54016
78041
91757
130007
89595
142289
149842
136738
70046
89586
142234
142090
147759
85957
136288
86895
131370
71565
128290
95531
110317
115170
56454
71468
113938
64193
115562
73585
81194
92754
105826
104739
137106
104467
120320

View file

@ -0,0 +1,25 @@
#include <iostream>
using namespace std;
int required_fuel(int mass)
{
return (mass / 3) - 2;
}
int main(int argv, char **argc)
{
int sum = 0;
while (!cin.eof())
{
int mass;
cin >> mass;
cout << mass << " requires " << required_fuel(mass) << "\n";
sum += required_fuel(mass);
}
cout << "Total required fuel: " << sum << "\n";
}

View file

@ -0,0 +1,29 @@
#include <iostream>
using namespace std;
int required_fuel(int mass)
{
int fuel = (mass / 3) - 2;
if (fuel <= 0)
return 0;
return fuel + required_fuel(fuel);
}
int main(int argv, char **argc)
{
int sum = 0;
while (!cin.eof())
{
int mass;
cin >> mass;
sum += required_fuel(mass);
// cout << mass << " requires " << required_fuel(mass) << "\n";
}
cout << "Total required fuel: " << sum << "\n";
}

View file

@ -0,0 +1 @@
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,10,19,1,19,5,23,1,6,23,27,1,27,5,31,2,31,10,35,2,35,6,39,1,39,5,43,2,43,9,47,1,47,6,51,1,13,51,55,2,9,55,59,1,59,13,63,1,6,63,67,2,67,10,71,1,9,71,75,2,75,6,79,1,79,5,83,1,83,5,87,2,9,87,91,2,9,91,95,1,95,10,99,1,9,99,103,2,103,6,107,2,9,107,111,1,111,5,115,2,6,115,119,1,5,119,123,1,123,2,127,1,127,9,0,99,2,0,14,0

View file

@ -0,0 +1,59 @@
#include <iostream>
#include <vector>
using namespace std;
vector<int> read_input()
{
vector<int> rval;
int buffer;
char comma;
while (cin)
{
cin >> buffer;
rval.push_back(buffer);
cin >> comma;
}
return rval;
}
int main(int argc, char *argv[])
{
vector<int> program = read_input();
size_t instruction = 0;
// fix according to assignment
program[1] = 12;
program[2] = 2;
while(true)
{
switch(program[instruction++])
{
case 1:
{
size_t lhs = program[instruction++];
size_t rhs = program[instruction++];
size_t result = program[instruction++];
program[result] = program[lhs] + program[rhs];
}
break;
case 2:
{
size_t lhs = program[instruction++];
size_t rhs = program[instruction++];
size_t result = program[instruction++];
program[result] = program[lhs] * program[rhs];
}
break;
case 99:
cout << "Result: " << program[0] << "\n";
return 0;
}
}
}

View file

@ -0,0 +1,79 @@
#include <iostream>
#include <vector>
using namespace std;
vector<int> read_input()
{
vector<int> rval;
int buffer;
char comma;
while (cin)
{
cin >> buffer;
rval.push_back(buffer);
cin >> comma;
}
return rval;
}
int run_program(vector<int> program, int noun = 12, int verb = 2)
{
cout << "running program!\n";
size_t instruction = 0;
program[1] = noun;
program[2] = verb;
while(true)
{
switch(program[instruction++])
{
case 1:
{
size_t lhs = program[instruction++];
size_t rhs = program[instruction++];
size_t result = program[instruction++];
program[result] = program[lhs] + program[rhs];
}
break;
case 2:
{
size_t lhs = program[instruction++];
size_t rhs = program[instruction++];
size_t result = program[instruction++];
program[result] = program[lhs] * program[rhs];
}
break;
case 99:
return program[0];
}
}
}
int main(int argc, char *argv[])
{
vector<int> program = read_input();
int target_output = 19690720;
for (int noun = 0; noun < 100; ++noun)
{
cout << "\rIteration: " << noun << " / 100";
for (int verb = 0; verb < 100; ++verb)
{
int result = run_program(program, noun, verb);
if (result == target_output)
{
cout << "Noun: " << noun << "\n";
cout << "Verb: " << verb << "\n";
cout << "Answer: " << (100 * noun + verb) << "\n";
return 0;
}
}
}
}

View file

@ -0,0 +1 @@
2,3,0,3,99

View file

@ -0,0 +1,2 @@
R1009,U263,L517,U449,L805,D78,L798,D883,L777,D562,R652,D348,R999,D767,L959,U493,R59,D994,L225,D226,R634,D200,R953,U343,L388,U158,R943,U544,L809,D785,R618,U499,L476,U600,L452,D693,L696,U764,L927,D346,L863,D458,L789,U268,R586,U884,L658,D371,L910,U178,R524,U169,R973,D326,R483,U233,R26,U807,L246,D711,L641,D75,R756,U365,R203,D377,R624,U430,L422,U367,R547,U294,L916,D757,R509,D332,R106,D401,L181,U5,L443,U197,R406,D829,R878,U35,L958,U31,L28,D362,R188,D582,R358,U750,R939,D491,R929,D513,L541,U418,R861,D639,L917,U582,R211,U725,R711,D718,L673,U921,L157,U83,L199,U501,L66,D993,L599,D947,L26,U237,L981,U833,L121,U25,R641,D372,L757,D645,R287,U390,R274,U964,R288,D209,R109,D364,R983,U715,L315,U758,R36,D500,R626,U893,L840,U716,L606,U831,L969,D643,L300,D838,R31,D751,L632,D702,R468,D7,L169,U149,R893,D33,R816,D558,R152,U489,L237,U415,R434,D472,L198,D874,L351,U148,R761,U809,R21,D25,R586,D338,L568,U20,L157,U221,L26,U424,R261,D227,L551,D754,L90,U110,L791,U433,R840,U323,R240,U124,L723,D418,R938,D173,L160,U293,R773,U204,R192,U958,L472,D703,R556,D168,L263,U574,L845,D932,R165,D348,R811,D834,R960,U877,R935,D141,R696,U748,L316,U236,L796,D566,R524,U449,R378,U480,L79,U227,R867,D185,R474,D757,R366,U153,R882,U252,R861,U900,R28,U381,L845,U642,L849,U352,R134,D294,R788,D406,L693,D697,L433,D872,R78,D364,R240,U995,R48,D681,R727,D825,L583,U44,R743,D929,L616,D262,R997,D15,R575,U341,R595,U889,R254,U76,R962,D944,R724,D261,R608,U753,L389,D324,L569,U308,L488,D358,L695,D863,L712,D978,R149,D177,R92
L1003,D960,L10,D57,R294,U538,R867,D426,L524,D441,R775,U308,R577,D785,R495,U847,R643,D895,R448,U685,L253,U312,L312,U753,L89,U276,R799,D923,L33,U595,R400,U111,L664,D542,R171,U709,L809,D713,L483,U918,L14,U854,L150,D69,L158,D500,L91,D800,R431,D851,L798,U515,L107,U413,L94,U390,L17,U221,L999,D546,L191,U472,L568,U114,L913,D743,L713,D215,L569,D674,L869,U549,L789,U259,L330,D76,R243,D592,L646,U880,L363,U542,L464,D955,L107,U473,R818,D786,R852,U968,R526,D78,L275,U891,R480,U991,L981,D391,R83,U691,R689,D230,L217,D458,R10,U736,L317,D145,R902,D428,R344,U334,R131,D739,R438,D376,L652,U304,L332,D452,R241,D783,R82,D317,R796,U323,R287,D487,L302,D110,R233,U631,R584,U973,L878,D834,L930,U472,R120,U78,R806,D21,L521,U988,R251,D817,R44,D789,R204,D669,R616,D96,R624,D891,L532,U154,R438,U469,R785,D431,R945,U649,R670,D11,R840,D521,L235,D69,L551,D266,L454,U807,L885,U590,L647,U763,R449,U194,R68,U809,L884,U962,L476,D648,L139,U96,L300,U351,L456,D202,R168,D698,R161,U834,L273,U47,L8,D157,L893,D200,L454,U723,R886,U92,R474,U262,L190,U110,L407,D723,R786,D786,L572,D915,L904,U744,L820,D663,R205,U878,R186,U247,L616,D386,R582,U688,L349,D399,R702,U132,L276,U866,R851,D633,R468,D263,R678,D96,L50,U946,R349,D482,R487,U525,R464,U977,L499,D187,R546,U708,L627,D470,R673,D886,L375,U616,L503,U38,L775,D8,L982,D556,R159,U680,L124,U777,L640,D607,R248,D671,L65,D290,R445,U778,L650,U679,L846,D1,L769,U659,R734,D962,R588,U178,R888,D753,R223,U318,L695,D586,R430,D61,R105,U801,R953,U721,L856,U769,R937,D335,R895

View file

@ -0,0 +1,104 @@
#include <iostream>
#include <vector>
#include <sstream>
#include <tuple>
using namespace std;
struct Instr//uction
{
char dir;
int steps;
};
struct Point
{
int x;
int y;
};
void print_grid(vector<char> const &grid, size_t width)
{
for (size_t idx = 0; idx < grid.size(); idx += width)
{
for (size_t col = 0; col < width; ++col)
{
cout << grid[idx + col];
}
cout << "\n";
}
}
size_t index(Point const &point, int width)
{
return point.y * width + point.x;
}
Point index_to_point(int index, int width)
{
return {index % width, index / width};
}
Point update(Point point, Instr const &instr)
{
switch(instr.dir)
{
case 'R':
point.x += instr.steps;
break;
case 'L':
point.x -= instr.steps;
break;
case 'U':
point.y -= instr.steps;
break;
case 'D':
point.y += instr.steps;
break;
default:
cout << "unknown direction '" << instr.dir << "'\n";
}
return point;
}
auto dimensions(vector<Instr> const &instructions, Point start = {0, 0})
{
Point min{0, 0};
Point max{0, 0};
for (Instr const &instr : instructions)
{
start = update(start, instr);
min.x = std::min(start.x, min.x);
max.x = std::max(start.x, max.x);
min.y = std::min(start.y, min.y);
max.y = std::max(start.y, max.y);
}
return tuple<Point, Point>(min, max);
}
vector<Instr> parse_instructions(std::string const &line)
{
stringstream ss{line};
vector<Instr> rval;
char dir;
int steps;
while (ss)
{
ss >> dir;
ss >> steps;
rval.push_back({dir, steps});
ss >> dir; // parse comma
}
return rval;
}
vector<Instr> parse_line()
{
string buffer;
getline(cin, buffer);
return parse_instructions(buffer);
}

View file

@ -0,0 +1,143 @@
#include "main.ih"
#include <limits>
int main(int argc, char *argv[])
{
vector<Instr> wire_one = parse_line();
vector<Instr> wire_two = parse_line();
auto [min1, max1] = dimensions(wire_one);
auto [min2, max2] = dimensions(wire_two);
Point min = {std::min(min1.x, min2.x), std::min(min1.y, min2.y)};
Point max = {std::max(max1.x, max2.x), std::max(max1.y, max2.y)};
int width = max.x + abs(min.x) + 2;
int height = max.y + abs(min.y) + 2;
Point startp = {abs(min.x) + 1, abs(min.y) + 1};
vector<Point> overlaps;
vector<char> grid;
grid.resize(width * height, '.');
size_t location = index(startp, width);
size_t stored_start = location;
grid[location] = 'O';
for (Instr const &instr : wire_one)
{
switch (instr.dir)
{
case 'R':
for (int idx = 0; idx < instr.steps; ++idx)
{
location += 1;
grid[location] = '1';
}
break;
case 'L':
for (int idx = 0; idx < instr.steps; ++idx)
{
location -= 1;
grid[location] = '1';
}
break;
case 'U':
for (int idx = 0; idx < instr.steps; ++idx)
{
location -= width;
grid[location] = '1';
}
break;
case 'D':
for (int idx = 0; idx < instr.steps; ++idx)
{
location += width;
grid[location] = '1';
}
break;
}
}
// some delish copy-paste
location = stored_start;
for (Instr const &instr : wire_two)
{
switch (instr.dir)
{
case 'R':
for (int idx = 0; idx < instr.steps; ++idx)
{
location += 1;
if (grid[location] == '1')
{
grid[location] = 'X';
overlaps.push_back(index_to_point(location, width));
}
else
grid[location] = '2';
}
break;
case 'L':
for (int idx = 0; idx < instr.steps; ++idx)
{
location -= 1;
if (grid[location] == '1')
{
grid[location] = 'X';
overlaps.push_back(index_to_point(location, width));
}
else
grid[location] = '2';
}
break;
case 'U':
for (int idx = 0; idx < instr.steps; ++idx)
{
location -= width;
if (grid[location] == '1')
{
grid[location] = 'X';
overlaps.push_back(index_to_point(location, width));
}
else
grid[location] = '2';
}
break;
case 'D':
for (int idx = 0; idx < instr.steps; ++idx)
{
location += width;
if (grid[location] == '1')
{
grid[location] = 'X';
overlaps.push_back(index_to_point(location, width));
}
else
grid[location] = '2';
}
break;
}
}
// print_grid(grid, width);
auto manhattan = [](Point p1, Point p2) -> int
{
return abs(p1.x - p2.x) + abs(p1.y - p2.y);
};
int mindist = numeric_limits<int>::max();
for (Point overlap : overlaps)
{
mindist = std::min(mindist, manhattan(startp, overlap));
}
cout << "mindist: " << mindist << "\n";
}

View file

@ -0,0 +1,144 @@
#include "main.ih"
#include <limits>
struct Location
{
int wire_one_dist = -1;
int wire_two_dist = -1;
int time()
{
if (wire_one_dist == -1 || wire_two_dist == -1)
return numeric_limits<int>::max();
return wire_one_dist + wire_two_dist;
}
};
int main(int argc, char *argv[])
{
vector<Instr> wire_one = parse_line();
vector<Instr> wire_two = parse_line();
auto [min1, max1] = dimensions(wire_one);
auto [min2, max2] = dimensions(wire_two);
Point min = {std::min(min1.x, min2.x), std::min(min1.y, min2.y)};
Point max = {std::max(max1.x, max2.x), std::max(max1.y, max2.y)};
int width = max.x + abs(min.x) + 2;
int height = max.y + abs(min.y) + 2;
Point startp = {abs(min.x) + 1, abs(min.y) + 1};
vector<int> overlaps;
vector<Location> grid;
grid.resize(width * height);
size_t location = index(startp, width);
size_t stored_start = location;
grid[location] = {0, 0};
int distance = 0;
for (Instr const &instr : wire_one)
{
switch (instr.dir)
{
case 'R':
for (int idx = 0; idx < instr.steps; ++idx)
{
++distance;
location += 1;
grid[location].wire_one_dist = grid[location].wire_one_dist == -1 ? distance : grid[location].wire_one_dist;
}
break;
case 'L':
for (int idx = 0; idx < instr.steps; ++idx)
{
++distance;
location -= 1;
grid[location].wire_one_dist = grid[location].wire_one_dist == -1 ? distance : grid[location].wire_one_dist;
}
break;
case 'U':
for (int idx = 0; idx < instr.steps; ++idx)
{
++distance;
location -= width;
grid[location].wire_one_dist = grid[location].wire_one_dist == -1 ? distance : grid[location].wire_one_dist;
}
break;
case 'D':
for (int idx = 0; idx < instr.steps; ++idx)
{
++distance;
location += width;
grid[location].wire_one_dist = grid[location].wire_one_dist == -1 ? distance : grid[location].wire_one_dist;
}
break;
}
}
// some delish copy-paste
location = stored_start;
distance = 0;
for (Instr const &instr : wire_two)
{
switch (instr.dir)
{
case 'R':
for (int idx = 0; idx < instr.steps; ++idx)
{
++distance;
location += 1;
grid[location].wire_two_dist = grid[location].wire_two_dist == -1 ? distance : grid[location].wire_two_dist;
if (grid[location].wire_one_dist != -1)
overlaps.push_back(location);
}
break;
case 'L':
for (int idx = 0; idx < instr.steps; ++idx)
{
++distance;
location -= 1;
grid[location].wire_two_dist = grid[location].wire_two_dist == -1 ? distance : grid[location].wire_two_dist;
if (grid[location].wire_one_dist != -1)
overlaps.push_back(location);
}
break;
case 'U':
for (int idx = 0; idx < instr.steps; ++idx)
{
++distance;
location -= width;
grid[location].wire_two_dist = grid[location].wire_two_dist == -1 ? distance : grid[location].wire_two_dist;
if (grid[location].wire_one_dist != -1)
overlaps.push_back(location);
}
break;
case 'D':
for (int idx = 0; idx < instr.steps; ++idx)
{
++distance;
location += width;
grid[location].wire_two_dist = grid[location].wire_two_dist == -1 ? distance : grid[location].wire_two_dist;
if (grid[location].wire_one_dist != -1)
overlaps.push_back(location);
}
break;
}
}
int mintime = numeric_limits<int>::max();
for (int overlap : overlaps)
{
cout << "time: " << grid[overlap].time() << "\n";
mintime = std::min(grid[overlap].time(), mintime);
}
cout << "mintime: " << mintime << "\n";
}

View file

@ -0,0 +1,2 @@
R75,D30,R83,U83,L12,D49,R71,U7,L72
U62,R66,U55,R34,D71,R55,D58,R83

View file

@ -0,0 +1,41 @@
#include <iostream>
#include <string>
using namespace std;
bool meets_criteria(size_t passwd)
{
string number = to_string(passwd);
bool adjacent = false;
char min = '0';
char prev = '1';
for (char ch : number)
{
adjacent = adjacent | (prev == ch);
if (ch < min)
return false;
min = ch;
prev = ch;
}
return adjacent;
}
int main()
{
const size_t LOWER = 178416;
const size_t UPPER = 676461;
size_t valid = 0;
for (size_t idx = LOWER; idx <= UPPER; ++idx)
{
if (meets_criteria(idx))
++valid;
}
cout << "valid: " << valid << "\n";
}

View file

@ -0,0 +1,62 @@
#include <iostream>
#include <string>
using namespace std;
bool meets_criteria(size_t passwd)
{
string number = to_string(passwd);
bool adjacent = false;
int groupsize = 0;
char min = '0';
char prev = '1';
for (char ch : number)
{
adjacent = adjacent | (prev == ch);
if (prev == ch)
++groupsize;
else
{
if (groupsize > 1 && groupsize % 2 != 0)
{
cout << "groupsize bounced: " << passwd << "\n";
return false; //uneven group
}
prev = ch;
groupsize = 1;
}
if (ch < min)
return false;
min = ch;
}
if (groupsize == 1)
return adjacent;
else if (groupsize % 2 != 0)
{
cout << "groupsize bounced (end): " << passwd << "\n";
return false;
}
return true;
}
int main()
{
const size_t LOWER = 178416;
const size_t UPPER = 676461;
size_t valid = 0;
for (size_t idx = LOWER; idx <= UPPER; ++idx)
{
if (meets_criteria(idx))
++valid;
}
cout << "valid: " << valid << "\n";
}

View file

@ -0,0 +1,200 @@
997
1582
1790
1798
1094
1831
1879
1730
1995
1702
1680
1869
1964
1777
1862
1928
1997
1741
1604
1691
1219
1458
1749
1717
1786
1665
1724
1998
1589
1828
1953
1848
1500
1590
1968
1948
1323
1800
1986
679
1907
1916
1820
1661
1479
1808
1824
1825
1952
1666
1541
1791
1906
1638
1557
1999
1710
1549
1912
1974
1628
1748
1411
1978
1865
1932
1839
1892
1981
1807
357
912
1443
1972
1816
1890
1029
1175
1522
1750
2001
1655
1955
1949
1660
233
1891
1994
1934
1908
1573
1712
1622
1770
1574
1778
1851
2004
1818
1200
1229
1110
1005
1716
1765
1835
1773
15
1914
1833
1689
1843
1718
1872
390
1941
1178
1670
1899
1864
1913
2010
1855
1797
1767
1673
1657
1607
1305
1341
1662
1845
1980
1534
1789
1876
1849
1926
1958
977
1709
1647
1832
1785
1854
1667
1679
1970
1186
2000
1681
1684
1614
1988
1561
1594
1636
1327
1696
1915
1045
1829
1079
1295
1213
1714
1992
1984
1951
1687
1842
1792
87
1732
428
1799
1850
1962
1629
1965
1142
1040
131
1844
1454
1779
1369
1960
1887
1725
1893
1465
1676
1826
1462
1408
1937
1643
1069
1759

View file

@ -0,0 +1,35 @@
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<int> numbers;
ifstream in("input.txt");
while (true)
{
int tmp;
in >> tmp;
if (!in)
break;
numbers.push_back(tmp);
cout << "read " << tmp << "\n";
}
cout << "Read " << numbers.size() << " numbers.\n";
for (size_t outer = 0; outer < numbers.size(); ++outer)
{
for (size_t inner = outer + 1; inner < numbers.size(); ++inner)
{
if (numbers[outer] + numbers[inner]== 2020)
cout << "Answer: " << numbers[outer] * numbers[inner] << "\n";
}
}
}

View file

@ -0,0 +1,42 @@
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<int> numbers;
ifstream in("input.txt");
while (true)
{
int tmp;
in >> tmp;
if (!in)
break;
numbers.push_back(tmp);
cout << "read " << tmp << "\n";
}
cout << "Read " << numbers.size() << " numbers.\n";
for (size_t outer = 0; outer < numbers.size(); ++outer)
{
for (size_t inner = outer + 1; inner < numbers.size(); ++inner)
{
for (size_t innest = inner + 1; innest < numbers.size(); ++innest)
{
if (numbers[outer] + numbers[inner] + numbers[innest] == 2020)
cout << "Answer: " << numbers[outer] * numbers[inner] * numbers[innest] << "\n";
}
}
}
}