diff --git a/2020/Makefile b/2020/Makefile index 3860638..ea0c262 100644 --- a/2020/Makefile +++ b/2020/Makefile @@ -1,8 +1,8 @@ day%.out: out/types.o misc/main.cpp days/day%.cpp - g++-10 -std=c++2a $(flags) -Dcurrent_day=$(@:.out=) days/$(@:out=cpp) misc/main.cpp out/types.o -o $@ + g++-10 -O3 -std=c++2a $(flags) -Dcurrent_day=$(@:.out=) days/$(@:out=cpp) misc/main.cpp out/types.o -o $@ day%_test.out: out/types.o misc/main_test.cpp days/day%.cpp - g++-10 -std=c++2a $(flags) -DTEST_BUILD -Dcurrent_day=$(@:_test.out=) days/$(@:_test.out=.cpp) misc/main_test.cpp out/types.o -o $@ + g++-10 -O3 -std=c++2a $(flags) -DTEST_BUILD -Dcurrent_day=$(@:_test.out=) days/$(@:_test.out=.cpp) misc/main_test.cpp out/types.o -o $@ days/day%.cpp: cp 'misc/day.cpp.template' $@ diff --git a/2020/days/day15.cpp b/2020/days/day15.cpp new file mode 100644 index 0000000..c33fb2a --- /dev/null +++ b/2020/days/day15.cpp @@ -0,0 +1,71 @@ +/******************************************************************************* + * + * 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 +#include +#include +#include + +#include "../misc/types.hpp" +#include "../misc/print.hpp" + +struct Pair{ u64 num; usize last; }; + +auto day15() -> void { + auto last = usize(0); + auto index = usize(0); + auto sequence = std::unordered_map(); + { + auto line = std::string(); + auto file = std::ifstream("inputs/day15.input"); + getline(file, line); + auto start = usize(0); + auto end = usize(0); + auto num = std::string(); + while ((end = line.find(',', start)) != std::string::npos) { + num = line.substr(start, end - start); + sequence.insert_or_assign(u64(std::stoll(num)), ++index); + start = end + 1; + } + end = line.size(); + num = line.substr(start, end - start); + sequence.insert_or_assign(u64(std::stoll(num)), ++index); + } + while (++index < 30000000) { + if (index == 2020) { + print(last); + } + if (sequence.contains(last)) { + auto new_last = index - sequence[last]; + sequence[last] = index; + last = new_last; + } else { + sequence.insert_or_assign(last, index); + last = 0; + } + } + print(last); +} diff --git a/2020/inputs/day15.input b/2020/inputs/day15.input new file mode 100644 index 0000000..3576b75 --- /dev/null +++ b/2020/inputs/day15.input @@ -0,0 +1 @@ +0,13,1,8,6,15