Completed day 10

This commit is contained in:
Gnarwhal 2020-12-09 21:38:13 -08:00
parent de0da386ae
commit 571bfdb12f
Signed by: Gnarwhal
GPG key ID: 0989A73D8C421174
5 changed files with 1261 additions and 1 deletions

76
2020/day10.cpp Normal file
View file

@ -0,0 +1,76 @@
/*******************************************************************************
*
* Copyright (c) 2020 Gnarwhal
*
* -----------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files(the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*******************************************************************************/
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include "types.hpp"
struct Pair {
u64 jolt;
u64 count;
};
auto operator<(const Pair & left, const Pair & right) -> bool {
return left.jolt < right.jolt;
}
auto main(i32 argc, char * argv[]) -> i32 {
auto jolts = std::vector<Pair>{{ 0, 1 }};
{
auto line = std::string();
auto file = std::ifstream("day10.input");
while (getline(file, line)) {
jolts.push_back({ (u64) std::stoll(line), 0 });
}
}
std::sort(jolts.begin(), jolts.end());
jolts.push_back({ jolts[jolts.size() - 1].jolt + 3, 0 });
auto dif1 = usize(0);
auto dif3 = usize(0);
for (auto i = usize(0); i < jolts.size() - 1; ++i) {
if (jolts[i + 1].jolt - jolts[i].jolt == 1) ++dif1;
else if (jolts[i + 1].jolt - jolts[i].jolt == 3) ++dif3;
}
std::cout << (dif1 * dif3) << std::endl;
for (auto i = i32(0); i < jolts.size() - 1; ++i) {
for (auto j = i + 1; j < jolts.size(); ++j) {
if (jolts[j].jolt <= jolts[i].jolt + 3) {
jolts[j].count += jolts[i].count;
} else {
break;
}
}
}
std::cout << jolts[jolts.size() - 1].count << std::endl;
return 0;
}

96
2020/day10.input Normal file
View file

@ -0,0 +1,96 @@
133
157
39
74
108
136
92
55
86
46
111
58
80
115
84
67
98
30
40
61
71
114
17
9
123
142
49
158
107
139
104
132
155
96
91
15
11
23
54
6
63
126
3
10
116
87
68
72
109
62
134
103
1
16
101
117
35
120
151
102
85
145
135
79
2
147
33
41
93
52
48
64
81
29
20
110
129
43
148
36
53
26
42
156
154
77
88
73
27
34
12
146
78
47
28
97

88
2020/day9.cpp Normal file
View file

@ -0,0 +1,88 @@
/*******************************************************************************
*
* Copyright (c) 2020 Gnarwhal
*
* -----------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files(the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*******************************************************************************/
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include "types.hpp"
auto find_sum(usize index, std::vector<u64> xmas) {
for (auto i = index - 25; i < index - 1; ++i) {
for (auto j = i + 1; j < index; ++j) {
if (xmas[i] + xmas[j] == xmas[index]) {
return true;
}
}
}
return false;
}
auto main(i32 argc, char * argv[]) -> i32 {
auto xmas = std::vector<u64>();
auto line = std::string();
auto file = std::ifstream("day9.input");
while (getline(file, line)) {
xmas.push_back(std::stoll(line));
}
auto invalid = u64(0);
for (auto i = usize(25); i < xmas.size(); ++i) {
if (!find_sum(i, xmas)) {
std::cout << (invalid = xmas[i]) << std::endl;
break;
}
}
auto head_index = 0;
auto tail_index = 1;
auto sum = xmas[0] + xmas[1];
while (sum != invalid) {
if (sum < invalid) {
sum += xmas[++tail_index];
} else if (head_index + 1 < tail_index) {
sum -= xmas[head_index];
++head_index;
} else {
head_index = tail_index + 1;
tail_index = head_index + 1;
sum = xmas[head_index] + xmas[tail_index];
}
}
auto min = xmas[tail_index];
auto max = xmas[tail_index];
for (auto i = head_index; i < tail_index; ++i) {
if (xmas[i] < min) {
min = xmas[i];
} else if (xmas[i] > max) {
max = xmas[i];
}
}
std::cout << (min + max) << std::endl;
return 0;
}

1000
2020/day9.input Normal file

File diff suppressed because it is too large Load diff

View file

@ -34,7 +34,7 @@
auto main(i32 argc, char * argv[]) -> i32 { auto main(i32 argc, char * argv[]) -> i32 {
{ {
auto line = std::string(); auto line = std::string();
auto file = std::ifstream("day$NUM.input"); auto file = std::ifstream("dayNUM.input");
while (getline(file, line)) { while (getline(file, line)) {
} }