-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
400 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
"ios": "cpp", | ||
"iosfwd": "cpp", | ||
"type_traits": "cpp", | ||
"xutility": "cpp" | ||
"xutility": "cpp", | ||
"utility": "cpp" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.