Completed day 2

This commit is contained in:
Gnarwhal 2020-12-01 22:24:15 -08:00
parent 0a65e04354
commit 74f6c3970b
Signed by: Gnarwhal
GPG key ID: 0989A73D8C421174
6 changed files with 2100 additions and 1 deletions

View file

@ -1,6 +1,9 @@
day1.out: out/types.o day1.cpp day1.input
g++ $(flags) day1.cpp out/types.o -o $@
day2.out: out/types.o day2.cpp day2.input
g++ $(flags) day2.cpp out/types.o -o $@
out/types.o: types.hpp types.cpp
mkdir -p out/
g++ $(flags) -c types.cpp -o $@

BIN
2020/day1

Binary file not shown.

View file

@ -69,7 +69,7 @@ auto find_2020_x3(const std::vector<i32> & list) -> void {
}
}
i32 main(i32 argc, char * argv[]) {
auto main(i32 argc, char * argv[]) -> i32 {
auto list = std::vector<i32>();
{
auto line = std::string();

95
2020/day2.cpp Normal file
View file

@ -0,0 +1,95 @@
/*******************************************************************************
*
* 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 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 main(i32 argc, char * argv[]) -> i32 {
auto passwords = std::vector<Password>();
{
auto line = std::string();
auto file = std::ifstream("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);
}
}
std::cout << count_valid_sled(passwords) << std::endl;
std::cout << count_valid_toboggan(passwords) << std::endl;
return 0;
}

1000
2020/day2.input Normal file

File diff suppressed because it is too large Load diff

1001
2020/day2.output Normal file

File diff suppressed because it is too large Load diff