Skip to content

GH-321: Solve aco2015day1 #322

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 5 commits into from
Aug 12, 2023
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ An algorithm is a finite sequence of rigorous instructions, typically used to so

**Competitive Programming Events**

* 🎄 Advent of Code ([2022](collections/advent-of-code-2022/))
* 🎄 Advent of Code ([2015](collections/advent-of-code/2015/), [2022](collections/advent-of-code/2022/))
* 🔰 Google Code Jam ([2022](collections/codejam-2022/))

**Coding Problems Website**
Expand Down
30 changes: 30 additions & 0 deletions collections/advent-of-code/2015/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# 🎄 Advent of Code 2015

> Advent of Code is an Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like. People use them as interview prep, company training, university coursework, practice problems, a speed contest, or to challenge each other.

> You don't need a computer science background to participate - just a little programming knowledge and some problem solving skills will get you pretty far. Nor do you need a fancy computer; every problem has a solution that completes in at most 15 seconds on ten-year-old hardware.

* Website: https://adventofcode.com/2015/

## Problems

<table>
<thead>
<th></th>
<th>Statement</th>
<th>Part 1</th>
<th>Part 2</th>
</thead>
<tbody>
<tr>
<td>Day 1</td>
<td><a href="https://adventofcode.com/2015/day/1">Link</a></td>
<td>
<a href="../../../problems/aoc2015day1/src/main/solution1.cpp"><code>cpp</code></a>
</td>
<td>
<a href="../../../problems/aoc2022day1/src/main/solution1.cpp"><code>cpp</code></a>
</td>
</tr>
</tbody>
</table>
5 changes: 5 additions & 0 deletions problems/aoc2015day1/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
solution
solution1
solution2
*.dSYM
python/main/__pycache__
10 changes: 10 additions & 0 deletions problems/aoc2015day1/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/1.in
bazel run src/main:solution2 `pwd`/data/2.in
```
1 change: 1 addition & 0 deletions problems/aoc2015day1/data/1.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
()()(()()()(()()((()((()))((()((((()()((((()))()((((())(((((((()(((((((((()(((())(()()(()((()()(()(())(()((((()((()()()((((())((((((()(()(((()())(()((((()))())(())(()(()()))))))))((((((((((((()())()())())(())))(((()()()((((()(((()(()(()()(()(()()(()(((((((())(())(())())))((()())()((((()()((()))(((()()()())))(())))((((())(((()())(())(()))(()((((()())))())((()(())(((()((((()((()(())())))((()))()()(()(()))))((((((((()())((((()()((((()(()())(((((()(()())()))())(((()))()(()(()(()((((()(())(()))(((((()()(()()()(()(((())())(((()()(()()))(((()()(((())())(()(())())()()(())()()()((()(((()(())((()()((())()))((()()))((()()())((((()(()()(()(((()))()(()))))((()(((()()()))(()(((())()(()((()())(()(()()(()())(())()(((()(()())()((((()((()))))())()))((()()()()(())()())()()()((((()))))(()(((()()(((((((())()))()((((()((())()(()())(())()))(()(()())(((((((())))(((()))())))))()))())((())(()()((())()())()))))()((()()())(())((())((((()())())()()()(((()))())))()()))())(()()()(()((((((()()))())()))()(((()(((())((((()()()(()))())()()))))())()))())((())()())(((((())())((())())))(((())(((())(((((()(((((())(()(()())())(()(())(()))(()((((()))())()))))())))((()(()))))())))(((((())()))())()))))()))))(((()))()))))((()))((()((()(()(())()())))(()()()(())()))()((((())))))))(())(()((()()))(()))(()))(()((()))))))()()((((()()))()())()))))))()()()))(()((())(()))((()()()())()(((()((((())())))()((((()(()))))))())))()()())()))(()))))(()())()))))))((())))))))())()))()((())())))(()((()))()))(())))))(()))()())()()))((()(()))()()()()))))())()()))())(())()()))()))((()))))()()(()())))))()()()))((((()))()))))(()(())))(()())))((())())(()))()))))()())))()())()())))))))))()()))))())))((())((()))))())))(((()())))))))(()))()()))(()))()))))()())))))())((((()())))))))())))()()))))))))()))()))))()))))))(())))))))))())))))))))))))))())())((())))))))))()))((())))()))))))))())()(()))))))())))))()()()())()(()()()(()())(()))()()()(()())))())())))()))))())))))))()()()()())(())())()())()))))(()()()()()))))()))())())))((()())()())))()))()))))(()())))()))))))))(((()))()()))))))))))))))))))))(()))(()((()))())))())(()))(()(()(())))))()(()))()))()()))))))))))))()((()())(())())()(())))))())()())((()()))))(()()))))())()(())()))))))))))))))))))))()))(()(()())))))))()()((()))()))))))((())))()))))))))((()))())()()))())()()))((()))())))))))))))(()())()))(())((()(()()))(()())(())))()())(()(())()()))))()))()(()))))))(()))))))))))(()))())))))))))())))))())))(())))))()))))(())())))))))))()(()))))()())))())(()))()())))))))))))))())()()))))()))))))())))))()))))(())(()()()()((())()))())(()))((())()))())())(())(()()))))()))(())()()((())(())))(())))()))())))))))))()(((((())())))(())()))))(())))((()))()(((((((()))))()()))(())))))()(()))))(()()))()))())))))))(()())()))))))))())))(()))())()))(())()((())())()())())(()(()))))()))))))((()())(())()()(()())))()()))(())(())(()))())))()))(()))()()))((((()))))()))((()()()))))()))()))())))(()))()))))(())))()))())()(()))()())))())))))))())))())))()()))))))(()))())())))()))()()())())))))))))))))())))()))(()()))))())))())()(())))())))))))))))))))))()()())())))))()()()((()(()))()()(())()())()))()))))()()()))))))((()))))))))()(()(()((((((()()((()())))))))))))()))())))))((())())(()))())))())))))())()()())(())))())))()())())(())))))))()()(())))()))())))())())())()))))))))()))(()()()())())())))(())())))))))()()())()))))())))())()(())())))))))()())()))(()()(())())))()(()((()()((()()(((((())(()())()))(())()))(())))(())))))))()))()))((()))()))()))))))))()))))))))((()()())(()))(((()))(())))()))((())(((())))()())))())))))((())))))(())())((((((())())()(()))()(()((()())))((())()(()(()))))(())(()()())(())))())((()(((())())))(((()())())))())()(())())((((()()))))())((()))()()()()(())(((((((()()()((()))())(()())))(())())((((()()(()))))()((())))((())()))()(((()))())))()))((()(()))(())(()((((())((((()()(()()))(((())(()))))((((()(()))(())))))((()))(()))((()(((()(()))(()(()((()(())(()(()(()(()()((()))())(((())(()(()))))(()))()()))(())))(())()(((())(()))()((((()()))))())(()))))((())()((((()(((()))())())(((()))()())((())(())())(())()(())()(()()((((((()()))))()()(((()()))))()())()(((()(()))(()(()())(()(()))))(((((()(((())())))))(((((()((()()((())())((((((()(())(()()((()()()()()()()(()()))()(((()))()))(((((((())(((()((()())()((((())(((()(())))()((()(()()()((())((()())()))()))())))())((((((()))(()(()()()))(()((()(()(()))()((()(((()()()((())(((((())()(()))())())((()(())))(()(()())(())((())())())(((()()()(())))))())(()))))))()))))))())((()()()))((()((((((()))(((()((((()()()(((()))())()(()()(((()((()()()()())()()))()()()(()(())((()))))(()))())))))))()(()()(((((())()(()(((((()((()(()()())(()((((((((()((((((())()((((()()()((()((()((((((()))((())))))))())()))((()(()))()(()()(()((())((()()((((((((((((()())(()()()))((((()((((((())(()))())(()()((()()))()(((((((()((()()((((((()(((())))((())))((((((((()()(((((((())(((((()())(((())((())()((((()(((((((()(()(((()((((((()(((()(((((((((((()()((()()(()))((()()(((()(((())))((((())()(()(((())()(()(((())(((((((((((()))())))((((((())((()()((((()())())((((()()))((())(((((()(()()(()()()((())(()((()()((((()(((((()((()(()((((()())((((((()(((((()()(()(()((((())))(())(())(())((((()(()()((((()((((()()((()((((((())))(((((()))))()))(()((((((((()(((())())(((())))(()(()((())(((()((()()(((((()((()()(((())()(()))(((((((())(()(((((()))((()((()((()))(())())((((()((((())()(()))(((()(((((((((((((((())(((((((((()))(((()(()()()()((((((()((())()((((((((()(())(((((((((((()(()((())()((()()(()(()()((((()()((())(()((()()(()()((((()(((((((())))((((())(())()(((()()((()()((((()((()(((()((())(((()()()((((()((((()()(()(()((((((((())(()(((((())(()())(((((((()())()(()((((()((())(()()())((((()()(((()((((())(())(()()(((((((((()()))()(((())(()(()((((((())(()()())(()))()()(((()(((()((())(()(((((((()(()(()((()(((((()(()((()(()((((((()((((()()((((()(((()((())(()(()((()()((((()()(())()(())(((())(()((((((((()())(((((((((()(())()((((())))()))()()(((((()()((((((())(()()(((()(()(((((((()(()(((((((())(())((((()((()(())))((((()()())(()))((()())((((()(((((()(()(())(()(()()())(((((()(((((()((((()()((((((((()()))(()((((((())((((())()(()(((()()()(((()(()(())(())(((((()(())())((((())(())(()(((()(((((())((((())())((()(((((((()(((())(()(()))(((((((((()((()((()()(()((((())(((()((())((((())(()(((()(((()(()((((()(((())(()(((()(()()(()(()((()()(()())(())())((()(()(((()(((()(((()()(((((((((()(((((((((()()(((()(((()())((((()(()(((()()()((())((((((((((())(()(((()((((()())((((()((()))(((()()()(((((()(((((((())((()())(()((((())((((((((())(()((()((((((((((()()((()((()()))(((()())()())()(((()())()()(()(()(((((((())()))(())()))())()()((())()((()((((()((()((())(((((()((((((()(())))(()))())(((()))((()()(()(((()))((((())()(((()))))()(()(())()(((((())(()(()(())(())()((()()()((((()(())((()())(()(()))(()(()(()()(())()()(()((())()((()))))()))((()(()()()()((()())(()))())()(()(((((((((())())((()((()((((((())()((((())(((())((()(()()()((())(()((())(((()((((()()((()(()(((((())()))()((((((()))((())(((()()))(((())(())()))(((((((())(())())()(())(((((()))()((()))()(()()((()()()()()())(((((((
1 change: 1 addition & 0 deletions problems/aoc2015day1/data/1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
280
15 changes: 15 additions & 0 deletions problems/aoc2015day1/src/main/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
load("@rules_cc//cc:defs.bzl", "cc_binary")

cc_binary(
name = "solution1",
srcs = ["solution1.cpp", "helpers.h"],
visibility = ["//visibility:public"],
copts = ["-std=c++17"]
)

cc_binary(
name = "solution2",
srcs = ["solution2.cpp", "helpers.h"],
visibility = ["//visibility:public"],
copts = ["-std=c++17"]
)
59 changes: 59 additions & 0 deletions problems/aoc2015day1/src/main/helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#pragma once

#include <string>
#include <vector>
#include <iostream>
#include <stack>
#include <queue>

using namespace std;

/* Temmplate class T should be a Nary Tree Node
* See leetcode 589 (https://leetcode.com/problems/n-ary-tree-preorder-traversal/description/)
* class Node {
* public:
* int val;
* vector<Node*> children;
*
* Node() {}
*
* Node(int _val) { val = _val; }
*
* Node(int _val, vector<Node*> _children) {
* val = _val;
* children = _children;
* }
* };
*/
template <typename T> T* parse_nary_tree(vector<string> s) {
T* fake = new T();
T* current = fake;
queue<T*> q;
for(string e: s){
if(e == "null"){
current = q.front();
q.pop();
} else {
T* child = new T(stoi(e));
q.push(child);
current->children.push_back(child);
}
}
return fake->children[0];
}

vector<string> parse_line(string line) {
vector<string> v;
string s;
for (char c : line) {
if (c == ' ') {
v.push_back(s);
s = "";
} else {
s += c;
}
}
v.push_back(s);
return v;
}

41 changes: 41 additions & 0 deletions problems/aoc2015day1/src/main/solution1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <algorithm>
#include <cmath>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

#include "helpers.h"

using namespace std;

pair<int, int> solve(string s){
int f = 0; // floor
int p = -1; // position
for(int i=0; i<s.size(); i++){
if(s[i] == '('){
f++;
} else {
f--;
}
if(f == -1 && p == -1){
p = i + 1;
}
}
return make_pair(f, p);
}

int main(int argc, char* argv[]) {
string input = argv[1];
string line;
fstream f(input);
getline(f, line);
f.close();
auto [finalFloor, firstBasementEntry] = solve(line);
cout << "finalFloor = " << finalFloor << endl;
cout << "firstBasementEntry = " << firstBasementEntry << endl;
return 0;
}
31 changes: 31 additions & 0 deletions problems/aoc2015day1/src/main/solution2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <algorithm>
#include <cmath>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <map>
#include <string>
#include <vector>

#include "helpers.h"

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;
}
7 changes: 7 additions & 0 deletions tools/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Tools

Craft a problem with the following command:

```
python craft.py
```
6 changes: 5 additions & 1 deletion tools/craft/templates/aoc/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
solution
solution
solution1
solution2
*.dSYM
python/main/__pycache__
4 changes: 2 additions & 2 deletions tools/craft/templates/aoc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
Run program with an example

```
bazel run src/main:solution1 `pwd`/data/input1.txt
bazel run src/main:solution1 `pwd`/data/input2.txt
bazel run src/main:solution1 `pwd`/data/1.in
bazel run src/main:solution2 `pwd`/data/2.in
```
4 changes: 4 additions & 0 deletions tools/craft/templates/aoc/data/2.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1
2
3
4
1 change: 1 addition & 0 deletions tools/craft/templates/aoc/data/2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10