Skip to content

Instantly share code, notes, and snippets.

@etiago
Last active May 9, 2021 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save etiago/b700a81971accfc0e983e3b2be332554 to your computer and use it in GitHub Desktop.
Save etiago/b700a81971accfc0e983e3b2be332554 to your computer and use it in GitHub Desktop.
Puzzle 4
#ifndef EXPERIMENTS_PASSPORT_H
#define EXPERIMENTS_PASSPORT_H
#include <boost/optional.hpp>
#include <string>
#include <utility>
using namespace std;
struct Passport {
Passport() {
}
boost::optional<uint16_t> byr;
boost::optional<uint16_t> iyr;
boost::optional<uint16_t> eyr;
boost::optional<wstring> hgt;
boost::optional<wstring> hcl;
boost::optional<wstring> ecl;
boost::optional<uint64_t> pid;
boost::optional<uint64_t> cid;
};
#endif // EXPERIMENTS_PASSPORT_H
ecl:gry pid:860033327 eyr:2020 hcl:#fffffd
byr:1937 iyr:2017 cid:147 hgt:183cm
iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884
hcl:#cfa07d byr:1929
hcl:#ae17e1 iyr:2013
eyr:2024
ecl:brn pid:760753108 byr:1931
hgt:179cm
hcl:#cfa07d eyr:2025 pid:166559648
iyr:2011 ecl:brn hgt:59in
#include "puzzle4.h"
void puzzle4::setFieldInPassport(Passport& passport, wstring fieldName, wstring fieldValue) {
if (fieldName == L"ecl") {
passport.ecl = fieldValue;
} else if (fieldName == L"pid") {
passport.pid = stoi(fieldValue);
} else if (fieldName == L"eyr") {
passport.eyr = stoi(fieldValue);
} else if (fieldName == L"hcl") {
passport.hcl = fieldValue;
} else if (fieldName == L"byr") {
passport.byr = stoi(fieldValue);
} else if (fieldName == L"iyr") {
passport.iyr = stoi(fieldValue);
} else if (fieldName == L"cid") {
passport.cid = stoi(fieldValue);
} else if (fieldName == L"hgt") {
passport.hgt = fieldValue;
}
}
Passport puzzle4::getPassportForString(wstring passportLine) {
unsigned long space = 0;
auto passport = Passport();
while (space != string::npos) {
auto nextSpace = passportLine.find(L" ", space + 1);
auto fieldValueSlice = passportLine.substr(space == 0 ? 0 : space + 1, nextSpace - space);
auto separator = fieldValueSlice.find(L":");
auto fieldName = fieldValueSlice.substr(0, separator);
auto fieldValue = fieldValueSlice.substr(separator + 1);
puzzle4::setFieldInPassport(passport, fieldName, fieldValue);
space = nextSpace;
}
return passport;
}
std::vector<Passport> puzzle4::getInput() {
auto passports = vector<Passport>();
wfstream puzzleInput;
puzzleInput.open("/home/tiago/personal-git/side-projects/cpp-adventofcode/puzzle-inputs/puzzle4-example.txt", ios::in);
if (!puzzleInput.is_open()) {
throw std::runtime_error("Could not open the puzzle input.");
}
wstring fullLine;
wstring line;
while (getline(puzzleInput, line)) {
if (line.size() > 0) {
if (!fullLine.empty()) {
fullLine += L" ";
}
fullLine += line;
} else {
auto passport = puzzle4::getPassportForString(fullLine);
wcout << "Line: " << fullLine << "\n";
wcout << "Byr: " << passport.byr.get() << "\n";
fullLine.clear();
}
}
puzzleInput.close();
if (!fullLine.empty()) {
auto passport = puzzle4::getPassportForString(fullLine);
wcout << "Line: " << fullLine << "\n";
wcout << "Byr: " << passport.byr.get() << "\n";
fullLine.clear();
}
return std::vector<Passport>();
}
void puzzle4::runPart1() {
}
#ifndef EXPERIMENTS_PUZZLE4_H
#define EXPERIMENTS_PUZZLE4_H
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include "model/Passport.h"
using namespace std;
class puzzle4 {
public:
static void setFieldInPassport(Passport& passport, wstring fieldName, wstring fieldValue);
static Passport getPassportForString(wstring passportLine);
static vector<Passport> getInput();
void runPart1();
};
#endif // EXPERIMENTS_PUZZLE4_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment