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

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