Added older advents
This commit is contained in:
parent
8db2505049
commit
9cf858b860
78 changed files with 13807 additions and 0 deletions
1
advent_of_code_2018/day9/input.in
Normal file
1
advent_of_code_2018/day9/input.in
Normal file
|
|
@ -0,0 +1 @@
|
|||
468 players; last marble is worth 7184300 points
|
||||
120
advent_of_code_2018/day9/main1.cc
Normal file
120
advent_of_code_2018/day9/main1.cc
Normal 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";
|
||||
}
|
||||
146
advent_of_code_2018/day9/main2.cc
Normal file
146
advent_of_code_2018/day9/main2.cc
Normal 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();
|
||||
|
||||
}
|
||||
1
advent_of_code_2018/day9/test.in
Normal file
1
advent_of_code_2018/day9/test.in
Normal file
|
|
@ -0,0 +1 @@
|
|||
10 players; last marble is worth 1618 points
|
||||
Loading…
Add table
Add a link
Reference in a new issue