Skip to content

Commit 5e6964d

Browse files
committed
Add 2021 day 06 cpp
1 parent 6d4df3f commit 5e6964d

5 files changed

Lines changed: 108 additions & 1 deletion

File tree

2021/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ This folder contains solutions to each of the problems in Advent of Code 2021 in
99
| <nobr> [Day 3: Binary Diagnostic](https://adventofcode.com/2021/day/3) </nobr> | <nobr> [Part 1](/2021/cpp/day_03a.cpp) [Part 2](/2021/cpp/day_03b.cpp) </nobr> | </nobr> [Link](/2021/input/day_03_input) </nobr> | </nobr> [Link](/2021/sample_input/day_03_sample_input) </nobr> | </nobr> [Link](/2021/puzzles/day_03_puzzle) </nobr> |
1010
| <nobr> [Day 4: Giant Squid](https://adventofcode.com/2021/day/4) </nobr> | <nobr> [Part 1](/2021/cpp/day_04a.cpp) [Part 2](/2021/cpp/day_04b.cpp) </nobr> | </nobr> [Link](/2021/input/day_04_input) </nobr> | </nobr> [Link](/2021/sample_input/day_04_sample_input) </nobr> | </nobr> [Link](/2021/puzzles/day_04_puzzle) </nobr> |
1111
| <nobr> [Day 5: Hydrothermal Venture](https://adventofcode.com/2021/day/5) </nobr> | <nobr> [Part 1](/2021/cpp/day_05a.cpp) [Part 2](/2021/cpp/day_05b.cpp) </nobr> | </nobr> [Link](/2021/input/day_05_input) </nobr> | </nobr> [Link](/2021/sample_input/day_05_sample_input) </nobr> | </nobr> [Link](/2021/puzzles/day_05_puzzle) </nobr> |
12+
| <nobr> [Day 6: Lanternfish](https://adventofcode.com/2021/day/6) </nobr> | <nobr> [Part 1](/2021/cpp/day_06a.cpp) [Part 2](/2021/cpp/day_06b.cpp) </nobr> | </nobr> [Link](/2021/input/day_06_input) </nobr> | </nobr> [Link](/2021/sample_input/day_06_sample_input) </nobr> | </nobr> [Link](/2021/puzzles/day_06_puzzle) </nobr> |

2021/cpp/day_06a.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <fstream>
2+
#include <iostream>
3+
#include <string>
4+
#include <vector>
5+
6+
void print(const std::vector<int>& v) {
7+
for (const auto ele : v) {
8+
std::cout << ele << ',';
9+
}
10+
std::cout << '\n';
11+
}
12+
13+
int main(int argc, char * argv[]) {
14+
std::string input = "../input/day_06_input";
15+
if (argc > 1) {
16+
input = argv[1];
17+
}
18+
19+
std::string line;
20+
std::fstream file(input);
21+
std::getline(file, line);
22+
std::vector<int> fish;
23+
std::size_t start = 0;
24+
std::size_t end = line.find(',', start);
25+
while (end != std::string::npos) {
26+
fish.emplace_back(std::stoi(line.substr(start, end - start)));
27+
start = end + 1;
28+
end = line.find(',', start);
29+
}
30+
fish.emplace_back(std::stoi(line.substr(start, line.size() - start)));
31+
// print(fish);
32+
for (int day = 0; day < 80; day++) {
33+
int to_add = 0;
34+
for (auto& f : fish) {
35+
if (f == 0) {
36+
to_add++;
37+
f = 7;
38+
}
39+
f--;
40+
}
41+
for (int i = 0; i < to_add; i++) {
42+
fish.emplace_back(8);
43+
}
44+
// print(fish);
45+
}
46+
std::cout << fish.size() << '\n';
47+
return 0;
48+
}

2021/cpp/day_06b.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <fstream>
2+
#include <iostream>
3+
#include <string>
4+
#include <unordered_map>
5+
6+
void print(const std::unordered_map<int, long long>& m) {
7+
for (const auto& p : m) {
8+
std::cout << p.first << ": " << p.second << '\n';
9+
}
10+
std::cout << '\n';
11+
}
12+
13+
int main(int argc, char * argv[]) {
14+
std::string input = "../input/day_06_input";
15+
if (argc > 1) {
16+
input = argv[1];
17+
}
18+
19+
std::string line;
20+
std::fstream file(input);
21+
std::getline(file, line);
22+
std::size_t start = 0;
23+
std::size_t end = line.find(',', start);
24+
std::unordered_map<int, long long> fish;
25+
for (int i = 0; i < 9; i++) {
26+
fish[i] = 0;
27+
}
28+
while (end != std::string::npos) {
29+
const auto val = std::stoll(line.substr(start, end - start));
30+
fish[val]++;
31+
start = end + 1;
32+
end = line.find(',', start);
33+
}
34+
const auto val = std::stoll(line.substr(start, line.size() - start));
35+
fish[val]++;
36+
// print(fish);
37+
38+
for (int day = 0; day < 256; day++) {
39+
const long long to_add = fish[0];
40+
for (int i = 1; i < 7; i++) {
41+
fish[i-1] = fish[i];
42+
}
43+
fish[6] = to_add + fish[7];
44+
fish[7] = fish[8];
45+
fish[8] = to_add;
46+
// print(fish);
47+
}
48+
49+
// std::cout << "Final numbers" << '\n';
50+
// print(fish);
51+
long long sum = 0;
52+
for (const auto& p : fish) {
53+
sum += p.second;
54+
}
55+
std::cout << sum << '\n';
56+
return 0;
57+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3,4,3,1,2

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ This repository contains solutions to the Advent of Code puzzles.
66

77
| Year | C++ | Python | Link to folders | Link to README.md |
88
|:----:|:----:|:----:|:----:|:----:|
9-
|2021 |1-5 | - | [Link](/2021/) |[Link](/2021/README.md) |
9+
|2021 |1-6 | - | [Link](/2021/) |[Link](/2021/README.md) |
1010
|2020 |1-25 |1-25 | [Link](/2020/) |[Link](/2020/README.md) |
1111
|2019 |1-25 |1-7 | [Link](/2019/) |[Link](/2019/README.md) |

0 commit comments

Comments
 (0)