Skip to content

GH-96: Solve problem aoc2022day5 part 1 #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions collections/advent-of-code-2022/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
</thead>
<tbody>
<tr>
<td>Day 25 (Part 1)</td>
<td><a href="https://adventofcode.com/2022/day/25">Link</a></td>
<td>
<a href="../../problems/aoc2022day25/src/main/solution1.cpp"><code>cpp🐀</code></a>
</td>
<td>
</td>
</tr>
<tr>
<td>Day 24 (!)</td>
<td><a href="https://adventofcode.com/2022/day/24">Link</a></td>
<td>
Expand Down
1 change: 1 addition & 0 deletions problems/aoc2022day25/.gitginore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
solution
10 changes: 10 additions & 0 deletions problems/aoc2022day25/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# AoC Problem

## Usage

Run program with an example

```
bazel run src/main:solution1 `pwd`/data/input0.txt
bazel run src/main:solution1 `pwd`/data/input1.txt
```
13 changes: 13 additions & 0 deletions problems/aoc2022day25/data/input0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
1=-0-2
12111
2=0=
21
2=01
111
20012
112
1=-1=
1-12
12
1=
122
119 changes: 119 additions & 0 deletions problems/aoc2022day25/data/input1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
22-=0-1-=
1-==1200200=22-1==2=
2-01=--01==01
20-=
21=210=002
12=-0002
1=100-==0
1101
2==-=0=0110-
2=100122==--1-
2=1-2==2--1
1==0=2=0==1100
2=0==-2
1=1222202101--=
1101=2111-11=
1=11=2022-11-0=
1=2=1102-
1--21=1--=
11-
1001-10-
201222-1-2
122--2
2-
11--2-=20=11-2=0
1-000=1221==2-
1=-==-=2-0
121-
1--2-=---0=01
1=00=010-1-20
1=110=2
1110-=01--0-12--0
200022-
2=-=0=-1==2-=2222
20201
11-0
1==
1-=---2==20=01
2=2=-1
1==01-100
1-=----=--=
1-011--20=1-==2121
101---1=-1--0=21--2
221=0100--020=11
1=-121=2=-1-=
11210-
1=0=2=22-==-==-
2=2220210221-202
1--1=---00-11
2=0--
1=-2=-1==2=0
1=0=0=
122012210-=010=1
21-1-220=21-=-00
2-=2==0
1220
20
1=0=22=-21101
2-===0==1-210
1=-220=121=----2
21=0=-=0---0===1=--
202-22112---0-2=
10=0
11
102120--=0-021-121
100--221=
2--011=-=202
1=-
110=0-0-021-
121
1-0210-0
2-10=2
2=-0-1=00--22
121-=2
12-212110-022-2211
1=222==11=-=-01=10
200-0120=
1-00===12=-1=-0--
100-1
102-2201
10-=0-==2=2==2=0
12=01
1012-2
1=0-02=21=1==1
2=021-
1-001-
10202-00=12===11-1
1=
20=11=0112=
2--1=1-1121
2-2=---222
1=-001==-0-
1-=112-=
11011
1=2==1
1-=2=2201
2==222-==
101-0-=2
1-10-0212-0==0-10
1-2-1-=01100
121=0-12-2
20=0=0-0
22000-==0-1-2=0=0=
10=
2101102
1120===10=2-2=02-
1-=10--211
2===-01==1=--===-
1=21-201-==200=
1=-=2=10
220-120=-0-1010
12011
1-0=2
1---01
1=-=1=021=0=0=011
1=10-212-0
2=
10-0=-1=
1-21=22=1101000-1-
11=00222-=100--
1 change: 1 addition & 0 deletions problems/aoc2022day25/data/output0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
1 change: 1 addition & 0 deletions problems/aoc2022day25/data/output1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10
13 changes: 13 additions & 0 deletions problems/aoc2022day25/src/main/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
load("@rules_cc//cc:defs.bzl", "cc_binary")

cc_binary(
name = "solution1",
srcs = ["solution1.cpp"],
visibility = ["//visibility:public"]
)

cc_binary(
name = "solution2",
srcs = ["solution2.cpp"],
visibility = ["//visibility:public"]
)
67 changes: 67 additions & 0 deletions problems/aoc2022day25/src/main/solution1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <cmath>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <iostream>
#include <fstream>
#include <filesystem>
#include <set>

using namespace std;

string encrypt(long long num){
char c;
string s = "";
map<int, string> m = {
{0, "0"},
{1, "1"},
{2, "2"},
{-1, "-"},
{-2, "="}
};
while(num > 0){
int d = num % 5;
if(d == 3 || d == 4){
d = d - 5;
}
// cout << "num = " << num << endl;
// cout << "d = " << d << endl;
s = m[d] + s;
num = (num - d)/5;
}
return s;
}

long long decrypt(string line){
int n = line.size();
long long num = 0;
map<char, int> m = {
{'0', 0},
{'1', 1},
{'2', 2},
{'-', -1},
{'=', -2},
};
for(int i=0; i<n; i++){
num += m[line[i]] * pow(5, n-1-i);
}
// cout << num << endl;
return num;
}
int main(int argc, char* argv[]){
string input = argv[1];
string line;
fstream f(input);
long long result = 0;
while(getline(f, line)){
if(line.size() != 0){
cout << line << endl;
result += decrypt(line);
}
}
cout << "result = " << result << endl;
string s = encrypt(result);
// string s = encrypt(4890);
cout << "decrypt = " << s << endl;
}
29 changes: 29 additions & 0 deletions problems/aoc2022day25/src/main/solution2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <cmath>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <iostream>
#include <fstream>
#include <filesystem>

using namespace std;

int main(int argc, char* argv[]){
string input = argv[1];
string line;
fstream f(input);
long long sum_c = 0;
vector<long long> v;
while(getline(f, line)){
if(line.size() == 0){
v.push_back(sum_c);
sum_c = 0;
} else {
sum_c += stoi(line);
}
}
sort(v.begin(), v.end(), greater<long long>());
long long total = v[0] + v[1] + v[2];
cout << "max_c = " << total << endl;
}