Skip to content

Commit

Permalink
Day 24 - Electromagnetic Moat
Browse files Browse the repository at this point in the history
  • Loading branch information
watmough committed Dec 24, 2017
1 parent 138c68d commit 61ae879
Show file tree
Hide file tree
Showing 7 changed files with 400 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"ios": "cpp",
"iosfwd": "cpp",
"type_traits": "cpp",
"xutility": "cpp"
"xutility": "cpp",
"utility": "cpp"
}
}
129 changes: 128 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,133 @@
# Advent-of-Code-2017

### Day 22 - Coprocessor Conflagration
### Day 24 - Electromagnetic Moat

Rank: 2240 / 2221

Search space algorithms beyond just a simple flood-fill or dumping a tree are
somewhat of a blindspot I'm fixing in my coding.

Part 1:
What is the strength of the strongest bridge you can make with the components you have available?

Part 2:
What is the strength of the longest bridge you can make? If you can make multiple bridges of the
longest length, pick the strongest one.

```C++
// Advent of Code 2016
// Day 24 - Electromagnetic Moat

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <tuple>
#include <algorithm>
using namespace std;

// parse input
void parse(vector<pair<int,int>>& parts)
{
auto row{""s};
auto p1{0},p2{0};
ifstream data("day_24.inp");
while (getline(data,row)) {
stringstream line(row);
line >> p1; line.get();
line >> p2;
parts.push_back(pair<int,int>(p1,p2));
}
}

bool not_used(vector<int> used, int part_num)
{
return find(begin(used),end(used),part_num)==end(used);
}

bool can_use(pair<int,int> part, int port)
{
return part.first==port || part.second==port;
}

vector<int> get_moves(vector<pair<int,int>>& parts, vector<int> used,int port) // components matching port that have not been used
{
vector<int> moves;
for (int i=0;i<parts.size();++i) {
if (not_used(used,i) && can_use(parts[i],port)) {
moves.push_back(i);
}
}
return moves;
}

// Part 1 - Just solve for best strength
tuple<int,vector<int>> solve_strength(vector<pair<int,int>>& parts, vector<int> used, int port, int strength)
{
vector<int> moves = get_moves(parts,used,port);
auto best_strength = strength;
vector<int> best_path = used;
for (auto m : moves) {
used.push_back(m);
auto new_port = parts[m].first==port ? parts[m].second : parts[m].first;
auto new_strength = strength+parts[m].first+parts[m].second;
auto solution = solve_strength(parts,used,new_port,new_strength);
if (get<0>(solution)>best_strength) {
best_strength = get<0>(solution);
best_path = get<1>(solution);
}
used.pop_back();
}
return make_tuple(best_strength,best_path);
}

// Part 2 - Solve for longest path
tuple<int,int,vector<int>> solve_length(vector<pair<int,int>>& parts, vector<int> used, int port, int strength,int length)
{
vector<int> moves = get_moves(parts,used,port);
auto best_length = length;
auto best_strength = strength;
vector<int> best_path = used;
for (auto m : moves) {
used.push_back(m);
auto new_port = parts[m].first==port ? parts[m].second : parts[m].first;
auto new_strength = strength+parts[m].first+parts[m].second;
auto new_length = length+1;
auto solution = solve_length(parts,used,new_port,new_strength,new_length);
if (get<0>(solution)>best_length || (get<0>(solution)==best_length && get<1>(solution)>best_strength)) {
best_length = get<0>(solution);
best_strength = get<1>(solution);
best_path = get<2>(solution);
}
used.pop_back();
}
return make_tuple(best_length,best_strength,best_path);
}

int main()
{
vector<pair<int,int>> parts;
parse(parts);

auto solution_strength = solve_strength(parts,vector<int>{},0,0);
cout << "Part 1: " << get<0>(solution_strength) << endl;

auto solution_length = solve_length(parts,vector<int>{},0,0,0);
cout << "Part 2: " << get<1>(solution_length) << endl;

return 0;
}
```
Output:
```
Part 1: 1906
Part 2: 1824
```
### Day 23 - Coprocessor Conflagration
Rank: 345 / ---
Expand Down
110 changes: 110 additions & 0 deletions day_24.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Advent of Code 2016
// Day 24 - Electromagnetic Moat

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <tuple>
#include <algorithm>
using namespace std;

// parse input
void parse(vector<pair<int,int>>& parts)
{
auto row{""s};
auto p1{0},p2{0};
ifstream data("day_24.inp");
while (getline(data,row)) {
stringstream line(row);
line >> p1; line.get();
line >> p2;
parts.push_back(pair<int,int>(p1,p2));
}
}

bool not_used(vector<int> used, int part_num)
{
return find(begin(used),end(used),part_num)==end(used);
}

bool can_use(pair<int,int> part, int port)
{
return part.first==port || part.second==port;
}

vector<int> get_moves(vector<pair<int,int>>& parts, vector<int> used,int port) // components matching port that have not been used
{
vector<int> moves;
for (int i=0;i<parts.size();++i) {
if (not_used(used,i) && can_use(parts[i],port)) {
moves.push_back(i);
}
}
return moves;
}

// Part 1 - Just solve for best strength
tuple<int,vector<int>> solve_strength(vector<pair<int,int>>& parts, vector<int> used, int port, int strength)
{
vector<int> moves = get_moves(parts,used,port);
auto best_strength = strength;
vector<int> best_path = used;
for (auto m : moves) {
used.push_back(m);
auto new_port = parts[m].first==port ? parts[m].second : parts[m].first;
auto new_strength = strength+parts[m].first+parts[m].second;
auto solution = solve_strength(parts,used,new_port,new_strength);
if (get<0>(solution)>best_strength) {
best_strength = get<0>(solution);
best_path = get<1>(solution);
}
used.pop_back();
}
return make_tuple(best_strength,best_path);
}

// Part 2 - Solve for longest path
tuple<int,int,vector<int>> solve_length(vector<pair<int,int>>& parts, vector<int> used, int port, int strength,int length)
{
vector<int> moves = get_moves(parts,used,port);
auto best_length = length;
auto best_strength = strength;
vector<int> best_path = used;
for (auto m : moves) {
used.push_back(m);
auto new_port = parts[m].first==port ? parts[m].second : parts[m].first;
auto new_strength = strength+parts[m].first+parts[m].second;
auto new_length = length+1;
auto solution = solve_length(parts,used,new_port,new_strength,new_length);
if (get<0>(solution)>best_length || (get<0>(solution)==best_length && get<1>(solution)>best_strength)) {
best_length = get<0>(solution);
best_strength = get<1>(solution);
best_path = get<2>(solution);
}
used.pop_back();
}
return make_tuple(best_length,best_strength,best_path);
}

int main()
{
vector<pair<int,int>> parts;
parse(parts);

auto solution_strength = solve_strength(parts,vector<int>{},0,0);
cout << "Part 1: " << get<0>(solution_strength) << endl;

auto solution_length = solve_length(parts,vector<int>{},0,0,0);
cout << "Part 2: " << get<1>(solution_length) << endl;

return 0;
}

/*
Output:
Part 1: 1906
Part 2: 1824
*/
56 changes: 56 additions & 0 deletions day_24.inp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
31/13
34/4
49/49
23/37
47/45
32/4
12/35
37/30
41/48
0/47
32/30
12/5
37/31
7/41
10/28
35/4
28/35
20/29
32/20
31/43
48/14
10/11
27/6
9/24
8/28
45/48
8/1
16/19
45/45
0/4
29/33
2/5
33/9
11/7
32/10
44/1
40/32
2/45
16/16
1/18
38/36
34/24
39/44
32/37
26/46
25/33
9/10
0/29
38/8
33/33
49/19
18/20
49/39
18/39
26/13
19/32
8 changes: 8 additions & 0 deletions day_24.tst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
0/2
2/2
2/3
3/4
3/5
0/1
10/1
9/10
Loading

0 comments on commit 61ae879

Please sign in to comment.