Completed day 15
This commit is contained in:
parent
c152831c7f
commit
4a791a4559
3 changed files with 74 additions and 2 deletions
|
@ -1,8 +1,8 @@
|
||||||
day%.out: out/types.o misc/main.cpp days/day%.cpp
|
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
|
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:
|
days/day%.cpp:
|
||||||
cp 'misc/day.cpp.template' $@
|
cp 'misc/day.cpp.template' $@
|
||||||
|
|
71
2020/days/day15.cpp
Normal file
71
2020/days/day15.cpp
Normal file
|
@ -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 <unordered_map>
|
||||||
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
#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<u64, u64>();
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
1
2020/inputs/day15.input
Normal file
1
2020/inputs/day15.input
Normal file
|
@ -0,0 +1 @@
|
||||||
|
0,13,1,8,6,15
|
Loading…
Reference in a new issue