Restructured project and updated day 3
This commit is contained in:
parent
f7c94b2a17
commit
530e53c22c
29 changed files with 245 additions and 1304 deletions
86
2020/days/day1.cpp
Normal file
86
2020/days/day1.cpp
Normal file
|
@ -0,0 +1,86 @@
|
|||
/*******************************************************************************
|
||||
*
|
||||
* 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 <algorithm>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
#include "../misc/types.hpp"
|
||||
#include "../misc/print.hpp"
|
||||
|
||||
// Find 2 numbers that sum to 2020
|
||||
auto find_2020_x2(const std::vector<i32> & list) -> void {
|
||||
auto begin = 0;
|
||||
auto end = list.size() - 1;
|
||||
auto sum = 0;
|
||||
while ((sum = list[begin] + list[end]) != 2020) {
|
||||
if (sum < 2020) {
|
||||
++begin;
|
||||
} else {
|
||||
--end;
|
||||
}
|
||||
}
|
||||
print((list[begin] * list[end]));
|
||||
}
|
||||
|
||||
// Find 3 numbers that sum to 2020
|
||||
auto find_2020_x3(const std::vector<i32> & list) -> void {
|
||||
for (auto n0 = 0; n0 < list.size() - 2; ++n0) {
|
||||
for (auto n1 = 1; n1 < list.size() - 1; ++n1) {
|
||||
auto low = n0 + 1;
|
||||
auto high = n1;
|
||||
while (low < high) {
|
||||
auto n2 = (low + high) / 2;
|
||||
auto sum = 0;
|
||||
if ((sum = list[n0] + list[n1] + list[n2]) == 2020) {
|
||||
print((list[n0] * list[n1] * list[n2]));
|
||||
return;
|
||||
} else if (sum < 2020) {
|
||||
low = n2 + 1;
|
||||
} else {
|
||||
high = n2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto day1() -> void {
|
||||
auto list = std::vector<i32>();
|
||||
{
|
||||
auto line = std::string();
|
||||
auto file = std::ifstream("inputs/day1.input");
|
||||
while (getline(file, line)) {
|
||||
list.push_back(std::stoi(line));
|
||||
}
|
||||
}
|
||||
std::sort(list.begin(), list.end());
|
||||
|
||||
find_2020_x2(list);
|
||||
find_2020_x3(list);
|
||||
}
|
75
2020/days/day10.cpp
Normal file
75
2020/days/day10.cpp
Normal file
|
@ -0,0 +1,75 @@
|
|||
/*******************************************************************************
|
||||
*
|
||||
* 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 "../misc/types.hpp"
|
||||
#include "../misc/print.hpp"
|
||||
|
||||
struct Pair {
|
||||
u64 jolt;
|
||||
u64 count;
|
||||
};
|
||||
|
||||
auto operator<(const Pair & left, const Pair & right) -> bool {
|
||||
return left.jolt < right.jolt;
|
||||
}
|
||||
|
||||
auto day10() -> void {
|
||||
auto jolts = std::vector<Pair>{{ 0, 1 }};
|
||||
{
|
||||
auto line = std::string();
|
||||
auto file = std::ifstream("inputs/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;
|
||||
}
|
||||
|
||||
print((dif1 * dif3));
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
print(jolts[jolts.size() - 1].count);
|
||||
}
|
134
2020/days/day11.cpp
Normal file
134
2020/days/day11.cpp
Normal file
|
@ -0,0 +1,134 @@
|
|||
/*******************************************************************************
|
||||
*
|
||||
* 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 <array>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
#include "../misc/types.hpp"
|
||||
#include "../misc/print.hpp"
|
||||
|
||||
template <typename Lambda>
|
||||
auto iterate_seats(const auto & seats, Lambda callback) {
|
||||
for (auto i = i32(0); i < seats.size(); ++i) {
|
||||
for (auto j = i32(0); j < seats[0].size(); ++j) {
|
||||
if (seats[i][j] != '.') {
|
||||
callback(i, j);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto seats_equal(const auto & seats) -> bool {
|
||||
for (auto i = usize(0); i < seats[0].size(); ++i) {
|
||||
for (auto j = usize(0); j < seats[0][0].size(); ++j) {
|
||||
if (seats[0][i][j] != seats[1][i][j]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
auto count_seats(const auto & seats) -> usize {
|
||||
auto occupied = usize(0);
|
||||
iterate_seats(seats, [&seats, &occupied](i32 i, i32 j) {
|
||||
occupied += (seats[i][j] == '#');
|
||||
});
|
||||
return occupied;
|
||||
}
|
||||
|
||||
auto day11() -> void {
|
||||
auto bit = u8(0b1);
|
||||
auto seats = std::array<std::vector<std::string>, 2>();
|
||||
{
|
||||
auto line = std::string();
|
||||
auto file = std::ifstream("inputs/day11.input");
|
||||
while (getline(file, line)) {
|
||||
seats[bit].push_back(line);
|
||||
}
|
||||
seats[!bit] = seats[bit];
|
||||
}
|
||||
|
||||
do {
|
||||
auto & read = seats[bit];
|
||||
auto & write = seats[bit = !bit];
|
||||
|
||||
iterate_seats(read, [&read, &write](usize i, usize j) {
|
||||
auto occupied = u8(0);
|
||||
auto max_ii = std::min(i + 2, read.size());
|
||||
auto max_jj = std::min(j + 2, read[0].size());
|
||||
for (auto ii = bool(i) * (i - 1); ii < max_ii; ++ii) {
|
||||
for (auto jj = bool(j) * (j - 1); jj < max_jj; ++jj) {
|
||||
occupied += read[ii][jj] == '#';
|
||||
}
|
||||
}
|
||||
if (occupied == 0 && read[i][j] == 'L') write[i][j] = '#';
|
||||
else if (occupied >= 5 && read[i][j] == '#') write[i][j] = 'L'; // occupied >= 5 (not >= 4) because the occupied seat itself was counted as adjacent
|
||||
else write[i][j] = read[i][j];
|
||||
});
|
||||
}
|
||||
while (!seats_equal(seats));
|
||||
|
||||
print(count_seats(seats[0]));
|
||||
|
||||
iterate_seats(seats[0], [&seats](i32 i, i32 j) {
|
||||
seats[0][i][j] = seats[1][i][j] = 'L';
|
||||
});
|
||||
|
||||
do {
|
||||
auto & read = seats[bit];
|
||||
auto & write = seats[bit = !bit];
|
||||
|
||||
iterate_seats(read, [&read, &write](i32 i, i32 j) {
|
||||
auto occupied = u8(0);
|
||||
for (auto k = i32(0); k < 8; ++k) {
|
||||
auto dir = k + (k > 3);
|
||||
auto y = i;
|
||||
auto x = j;
|
||||
auto y_shift = (dir % 3) - 1;
|
||||
auto x_shift = (dir > 5) - (dir < 3);
|
||||
for (;;) {
|
||||
y += y_shift;
|
||||
x += x_shift;
|
||||
if (y == -1 || x == -1 || y == read.size() || x == read[0].size() || read[y][x] == 'L') {
|
||||
break;
|
||||
} else if (read[y][x] == '#') {
|
||||
++occupied;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (occupied == 0 && read[i][j] == 'L') write[i][j] = '#';
|
||||
else if (occupied >= 5 && read[i][j] == '#') write[i][j] = 'L'; // no longer counting the seat itself as adjacent so stays occupied >= 5
|
||||
else write[i][j] = read[i][j];
|
||||
});
|
||||
} while (!seats_equal(seats));
|
||||
|
||||
print(count_seats(seats[0]));
|
||||
}
|
94
2020/days/day2.cpp
Normal file
94
2020/days/day2.cpp
Normal file
|
@ -0,0 +1,94 @@
|
|||
/*******************************************************************************
|
||||
*
|
||||
* 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 "../misc/types.hpp"
|
||||
#include "../misc/print.hpp"
|
||||
|
||||
struct Password {
|
||||
u32 min;
|
||||
u32 max;
|
||||
char c;
|
||||
std::string password;
|
||||
};
|
||||
|
||||
auto extract_num(usize & index, const std::string & string) -> u32 {
|
||||
auto num = u32(0);
|
||||
--index;
|
||||
while ('0' <= string[++index] && '9' >= string[index]) {
|
||||
num = (num * 10) + (string[index] - '0');
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
auto count_valid_sled(const std::vector<Password> & passwords) -> u32 {
|
||||
auto valid = u32(0);
|
||||
for (auto password : passwords) {
|
||||
auto count = u32(0);
|
||||
for (auto c : password.password) {
|
||||
if (c == password.c) {
|
||||
++count;
|
||||
}
|
||||
}
|
||||
valid += (password.min <= count && count <= password.max);
|
||||
}
|
||||
return valid;
|
||||
}
|
||||
|
||||
auto count_valid_toboggan(const std::vector<Password> & passwords) -> u32 {
|
||||
auto valid = u32(0);
|
||||
for (auto password : passwords) {
|
||||
if ((password.password[password.min - 1] == password.c)
|
||||
^ (password.password[password.max - 1] == password.c)) {
|
||||
++valid;
|
||||
}
|
||||
}
|
||||
return valid;
|
||||
}
|
||||
|
||||
auto day2() -> void {
|
||||
auto passwords = std::vector<Password>();
|
||||
{
|
||||
auto line = std::string();
|
||||
auto file = std::ifstream("inputs/day2.input");
|
||||
while (getline(file, line)) {
|
||||
auto index = usize(0);
|
||||
auto password = Password{};
|
||||
password.min = extract_num( index, line);
|
||||
password.max = extract_num(++index, line);
|
||||
password.c = line[++index];
|
||||
password.password = line.substr(index + 3, line.size() - index - 3);
|
||||
passwords.push_back(password);
|
||||
}
|
||||
}
|
||||
|
||||
print(count_valid_sled(passwords) );
|
||||
print(count_valid_toboggan(passwords));
|
||||
}
|
81
2020/days/day3.cpp
Normal file
81
2020/days/day3.cpp
Normal file
|
@ -0,0 +1,81 @@
|
|||
/*******************************************************************************
|
||||
*
|
||||
* 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 "../misc/types.hpp"
|
||||
#include "../misc/print.hpp"
|
||||
|
||||
struct TreeVector{ usize horz; usize vert; };
|
||||
struct Coordinate{ usize x_index; usize y_index; };
|
||||
|
||||
auto read_field_and_count_trees(const std::vector<TreeVector> & vectors) -> std::vector<u64> {
|
||||
auto tree_counts = std::vector<u64>(vectors.size());
|
||||
auto coords = std::vector<Coordinate>(vectors.size());
|
||||
for (auto i = usize(0); i < coords.size(); ++i) {
|
||||
coords[i] = { usize(-vectors[i].horz), usize(-1) };
|
||||
}
|
||||
|
||||
auto line = std::string();
|
||||
auto file = std::ifstream("inputs/day3.input");
|
||||
|
||||
while (getline(file, line)) {
|
||||
for (auto i = usize(0); i < vectors.size(); ++i) {
|
||||
auto & vector = vectors[i];
|
||||
auto & coord = coords[i];
|
||||
|
||||
auto vertical_check = (++coord.y_index % vector.vert == 0);
|
||||
tree_counts[i] += vertical_check * (line[(coord.x_index += (vertical_check * vector.horz)) % line.size()] == '#');
|
||||
}
|
||||
}
|
||||
|
||||
return std::move(tree_counts);
|
||||
}
|
||||
|
||||
auto day3() -> void {
|
||||
auto trees = std::vector<std::string>();
|
||||
auto tree_counts = read_field_and_count_trees(
|
||||
std::vector<TreeVector>{
|
||||
{ 3, 1 },
|
||||
{ 1, 1 },
|
||||
{ 5, 1 },
|
||||
{ 7, 1 },
|
||||
{ 1, 2 },
|
||||
}
|
||||
);
|
||||
|
||||
print(tree_counts[0]);
|
||||
|
||||
auto tree_count_product = u64(1);
|
||||
for (auto tree_count : tree_counts) {
|
||||
tree_count_product *= tree_count;
|
||||
}
|
||||
|
||||
print(tree_count_product);
|
||||
}
|
114
2020/days/day4.cpp
Normal file
114
2020/days/day4.cpp
Normal file
|
@ -0,0 +1,114 @@
|
|||
/*******************************************************************************
|
||||
*
|
||||
* 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 <fstream>
|
||||
#include <iostream>
|
||||
|
||||
#include "../misc/types.hpp"
|
||||
#include "../misc/print.hpp"
|
||||
|
||||
struct KeyValue {
|
||||
std::string key;
|
||||
bool (*func)(const std::string & passport, usize index);
|
||||
};
|
||||
|
||||
template <usize Min, usize Max>
|
||||
auto try_num(const std::string & passport, usize index) -> bool {
|
||||
auto num = usize(0);
|
||||
--index;
|
||||
while (++index < passport.size() && '0' <= passport[index] && passport[index] <= '9') {
|
||||
num = (num * 10) + passport[index] - '0';
|
||||
}
|
||||
return Min <= num && num <= Max;
|
||||
};
|
||||
|
||||
auto day4() -> void {
|
||||
auto valid_count1 = usize(0);
|
||||
auto valid_count2 = usize(0);
|
||||
|
||||
auto passport = std::string();
|
||||
auto line = std::string();
|
||||
auto file = std::ifstream("inputs/day4.input");
|
||||
while (getline(file, line)) {
|
||||
if (line == "") {
|
||||
auto elements = std::vector<KeyValue>{
|
||||
{ "byr", try_num<1920, 2002> },
|
||||
{ "iyr", try_num<2010, 2020> },
|
||||
{ "eyr", try_num<2020, 2030> },
|
||||
{ "hgt", [](const std::string & passport, usize index) -> bool {
|
||||
return (try_num<150, 193>(passport, index) && passport.find("cm", index + 3) == index + 3)
|
||||
|| (try_num<59, 76 >(passport, index) && passport.find("in", index + 2) == index + 2);
|
||||
}},
|
||||
{ "hcl", [](const std::string & passport, usize index) -> bool {
|
||||
if (index < passport.size() && passport[index] == '#') {
|
||||
auto length = usize(0);
|
||||
auto true_index = usize(0);
|
||||
while ((true_index = index + ++length) < passport.size() && (
|
||||
('0' <= passport[true_index] && passport[true_index] <= '9')
|
||||
|| ('a' <= passport[true_index] && passport[true_index] <= 'f')
|
||||
|| ('A' <= passport[true_index] && passport[true_index] <= 'F')
|
||||
));
|
||||
return length == 7;
|
||||
}
|
||||
return false;
|
||||
}},
|
||||
{ "ecl", [](const std::string & passport, usize index) -> bool {
|
||||
const static auto eye_colors = std::vector<std::string>{ "amb", "blu", "brn", "gry", "grn", "hzl", "oth" };
|
||||
auto i = usize(0);
|
||||
for (; i < eye_colors.size() && passport.find(eye_colors[i], index) != index; ++i);
|
||||
return i < eye_colors.size();
|
||||
}},
|
||||
{ "pid", [](const std::string & passport, usize index) -> bool {
|
||||
auto length = usize(-1);
|
||||
auto true_index = usize( 0);
|
||||
while ((true_index = index + ++length) < passport.size()
|
||||
&& ('0' <= passport[true_index] && passport[true_index] <= '9')
|
||||
);
|
||||
return length == 9;
|
||||
}},
|
||||
};
|
||||
auto valid1 = true;
|
||||
auto valid2 = true;
|
||||
for (auto i = usize(0); i < elements.size() && valid1; ++i) {
|
||||
auto index = usize(0);
|
||||
valid2 =
|
||||
(valid1 = ((index = passport.find(elements[i].key)) != std::string::npos))
|
||||
&& valid2
|
||||
&& elements[i].func(passport, index + elements[i].key.size() + 1);
|
||||
}
|
||||
valid_count1 += valid1;
|
||||
valid_count2 += valid2;
|
||||
passport = "";
|
||||
} else {
|
||||
passport += line + ' ';
|
||||
}
|
||||
}
|
||||
|
||||
print(valid_count1);
|
||||
print(valid_count2);
|
||||
}
|
63
2020/days/day5.cpp
Normal file
63
2020/days/day5.cpp
Normal file
|
@ -0,0 +1,63 @@
|
|||
/*******************************************************************************
|
||||
*
|
||||
* 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 <algorithm>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
#include "../misc/types.hpp"
|
||||
#include "../misc/print.hpp"
|
||||
|
||||
auto day5() -> void {
|
||||
auto seat = usize(0);
|
||||
auto line = std::string();
|
||||
auto file = std::ifstream("inputs/day5.input");
|
||||
|
||||
auto seats = std::vector<usize>();
|
||||
|
||||
while (getline(file, line)) {
|
||||
auto local_seat = usize(0);
|
||||
for (auto i = usize(0); i < 7 + 3; ++i) {
|
||||
local_seat = (local_seat << 1) | (line[i] == 'B' || line[i] == 'R');
|
||||
}
|
||||
if (local_seat > seat) {
|
||||
seat = local_seat;
|
||||
}
|
||||
seats.push_back(local_seat);
|
||||
}
|
||||
print(seat);
|
||||
|
||||
std::sort(seats.begin(), seats.end());
|
||||
|
||||
for (auto i = usize(0); i < seats.size() - 1; ++i) {
|
||||
if (seats[i] + 2 == seats[i + 1]) {
|
||||
print((seats[i] + 1));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
64
2020/days/day6.cpp
Normal file
64
2020/days/day6.cpp
Normal file
|
@ -0,0 +1,64 @@
|
|||
/*******************************************************************************
|
||||
*
|
||||
* 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 "../misc/types.hpp"
|
||||
#include "../misc/print.hpp"
|
||||
|
||||
auto day6() -> void {
|
||||
{
|
||||
auto line = std::string();
|
||||
auto file = std::ifstream("inputs/day6.input");
|
||||
|
||||
auto answers = std::vector<u32>(26);
|
||||
|
||||
auto sum_1 = usize(0);
|
||||
|
||||
auto group_size = usize(0);
|
||||
auto sum_2 = usize(0);
|
||||
while (getline(file, line)) {
|
||||
if (line == "") {
|
||||
for (auto i = usize(0); i < answers.size(); ++i) {
|
||||
sum_1 += (answers[i] > 0);
|
||||
sum_2 += (answers[i] == group_size);
|
||||
answers[i] = 0;
|
||||
}
|
||||
group_size = 0;
|
||||
} else {
|
||||
++group_size;
|
||||
for (char c : line) {
|
||||
++answers[c - 'a'];
|
||||
}
|
||||
}
|
||||
}
|
||||
print(sum_1);
|
||||
print(sum_2);
|
||||
}
|
||||
}
|
103
2020/days/day7.cpp
Normal file
103
2020/days/day7.cpp
Normal file
|
@ -0,0 +1,103 @@
|
|||
/*******************************************************************************
|
||||
*
|
||||
* 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 <stack>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
#include "../misc/types.hpp"
|
||||
#include "../misc/print.hpp"
|
||||
|
||||
struct SubBag { std::string type; u32 count; };
|
||||
|
||||
auto find_bag(const std::string & line, usize & index, SubBag & new_bag) -> bool {
|
||||
while (++index <= line.size() && (line[index] < '0' || line[index] > '9'));
|
||||
if (index >= line.size()) {
|
||||
return false;
|
||||
}
|
||||
auto end = line.find(" ", index);
|
||||
auto count = std::stoi(line.substr(index, end - index));
|
||||
index = end + 1;
|
||||
end = line.find("bag", index);
|
||||
new_bag = SubBag{ line.substr(index, end - index - 1), u32(count) };
|
||||
index = end;
|
||||
return true;
|
||||
}
|
||||
|
||||
struct Edges{ std::vector<std::string> from; std::vector<SubBag> to; };
|
||||
|
||||
auto count_to(const std::string & search, std::map<std::string, Edges> & map) -> usize {
|
||||
auto counted = 1;
|
||||
for (auto & bag : map[search].to) {
|
||||
counted += bag.count * count_to(bag.type, map);
|
||||
}
|
||||
return counted;
|
||||
}
|
||||
|
||||
auto day7() -> void {
|
||||
auto bag_graph = std::map<std::string, Edges>();
|
||||
{
|
||||
auto line = std::string();
|
||||
auto file = std::ifstream("inputs/day7.input");
|
||||
while (getline(file, line)) {
|
||||
auto find = line.find("bag") - 1;
|
||||
auto bag_type = line.substr(0, line.find("bag") - 1);
|
||||
if (!bag_graph.contains(bag_type)) {
|
||||
bag_graph.insert_or_assign(bag_type, Edges());
|
||||
}
|
||||
|
||||
auto new_bag = SubBag();
|
||||
while (find_bag(line, find, new_bag)) {
|
||||
if (!bag_graph.contains(new_bag.type)) {
|
||||
bag_graph.insert_or_assign(new_bag.type, Edges());
|
||||
}
|
||||
bag_graph[new_bag.type].from.push_back(bag_type);
|
||||
bag_graph[bag_type].to.push_back(new_bag);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto seen = std::set<std::string>();
|
||||
auto search = std::stack<std::string>();
|
||||
search.push("shiny gold");
|
||||
while (search.size() > 0) {
|
||||
auto current = search.top();
|
||||
search.pop();
|
||||
for (const auto & bag_type : bag_graph[current].from) {
|
||||
if (!seen.contains(bag_type)) {
|
||||
search.push(bag_type);
|
||||
seen.insert(bag_type);
|
||||
}
|
||||
}
|
||||
}
|
||||
print(seen.size());
|
||||
|
||||
print((count_to("shiny gold", bag_graph) - 1));
|
||||
}
|
111
2020/days/day8.cpp
Normal file
111
2020/days/day8.cpp
Normal file
|
@ -0,0 +1,111 @@
|
|||
/*******************************************************************************
|
||||
*
|
||||
* 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 "../misc/types.hpp"
|
||||
#include "../misc/print.hpp"
|
||||
|
||||
enum OpCode { Acc, Jmp, Nop };
|
||||
struct Op{ OpCode code; i32 num; bool executed; };
|
||||
auto extract_op(const std::string & string) -> Op {
|
||||
auto op = Op{ Nop, 0 };
|
||||
auto op_code_str = string.substr(0, 3);
|
||||
if (string.substr(0, 3) == "acc") { op.code = Acc; }
|
||||
else if (string.substr(0, 3) == "jmp") { op.code = Jmp; }
|
||||
else if (string.substr(0, 3) == "nop") { op.code = Nop; }
|
||||
|
||||
op.num = std::stoi(string.substr(string.find(" ") + 1, string.size() - 4));
|
||||
|
||||
return op;
|
||||
}
|
||||
|
||||
struct State{ u32 isp; i32 acc; };
|
||||
|
||||
auto execute(State & state, std::vector<Op> & ops) -> void {
|
||||
switch (ops[state.isp].code) {
|
||||
case Acc: ops[state.isp].executed = true; state.acc += ops[state.isp].num; ++state.isp; break;
|
||||
case Jmp: ops[state.isp].executed = true; state.isp += ops[state.isp].num; break;
|
||||
case Nop: ops[state.isp].executed = true; ++state.isp; break;
|
||||
}
|
||||
}
|
||||
|
||||
auto attempt_swap(State & state, std::vector<Op> & ops) -> bool {
|
||||
if (ops[state.isp].code == Jmp) {
|
||||
ops[state.isp].code = Nop;
|
||||
} else if (ops[state.isp].code == Nop) {
|
||||
ops[state.isp].code = Jmp;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto local_state = state;
|
||||
ops[state.isp].executed = false;
|
||||
while (local_state.isp < ops.size() && !ops[local_state.isp].executed) {
|
||||
execute(local_state, ops);
|
||||
}
|
||||
if (local_state.isp >= ops.size()) {
|
||||
state = local_state;
|
||||
return true;
|
||||
} else if (ops[state.isp].code == Jmp) {
|
||||
ops[state.isp].code = Nop;
|
||||
} else if (ops[state.isp].code == Nop) {
|
||||
ops[state.isp].code = Jmp;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
auto day8() -> void {
|
||||
auto ops = std::vector<Op>();
|
||||
{
|
||||
auto line = std::string();
|
||||
auto file = std::ifstream("inputs/day8.input");
|
||||
while (getline(file, line)) {
|
||||
ops.push_back(extract_op(line));
|
||||
}
|
||||
}
|
||||
|
||||
auto state = State{ 0, 0 };
|
||||
while (!ops[state.isp].executed) {
|
||||
execute(state, ops);
|
||||
}
|
||||
print(state.acc);
|
||||
|
||||
for (auto & op : ops) { op.executed = false; }
|
||||
|
||||
state = State{ 0, 0 };
|
||||
for (;;) {
|
||||
if (attempt_swap(state, ops)) {
|
||||
print(state.acc);
|
||||
break;
|
||||
}
|
||||
execute(state, ops);
|
||||
}
|
||||
}
|
87
2020/days/day9.cpp
Normal file
87
2020/days/day9.cpp
Normal file
|
@ -0,0 +1,87 @@
|
|||
/*******************************************************************************
|
||||
*
|
||||
* 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 "../misc/types.hpp"
|
||||
#include "../misc/print.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 day9() -> void {
|
||||
auto xmas = std::vector<u64>();
|
||||
auto line = std::string();
|
||||
auto file = std::ifstream("inputs/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)) {
|
||||
print((invalid = xmas[i]));
|
||||
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];
|
||||
}
|
||||
}
|
||||
print((min + max));
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue