Completed day 13 p1 and day 14
This commit is contained in:
parent
a9d41ba4fd
commit
a359db13b2
2 changed files with 218 additions and 0 deletions
106
2020/days/day13.cpp
Normal file
106
2020/days/day13.cpp
Normal file
|
@ -0,0 +1,106 @@
|
|||
/*******************************************************************************
|
||||
*
|
||||
* 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 id; usize index; };
|
||||
|
||||
auto find_least(u64 new_base, u64 new_loop, u64 req_base, u64 req_loop, u64 iter) -> u64 {
|
||||
while ((new_base + ++iter * new_loop - req_base) % req_loop != 0);
|
||||
return new_base + iter * new_loop;
|
||||
}
|
||||
|
||||
auto day13() -> void {
|
||||
auto ready = usize(0);
|
||||
auto buses = std::vector<Pair>();
|
||||
{
|
||||
auto line = std::string();
|
||||
auto file = std::ifstream("inputs/day13.input");
|
||||
getline(file, line);
|
||||
ready = std::stoi(line);
|
||||
getline(file, line);
|
||||
auto start = usize(0);
|
||||
auto end = usize(0);
|
||||
auto index = usize(0);
|
||||
auto id = std::string();
|
||||
while ((end = line.find(',', start)) != std::string::npos) {
|
||||
id = line.substr(start, end - start);
|
||||
if (id != "x") {
|
||||
buses.push_back({ u64(std::stoi(line.substr(start, end - start))), index });
|
||||
}
|
||||
start = end + 1;
|
||||
++index;
|
||||
}
|
||||
end = line.size();
|
||||
id = line.substr(start, end - start);
|
||||
if (id != "x") {
|
||||
buses.push_back({ u64(std::stoi(line.substr(start, end - start))), index });
|
||||
}
|
||||
}
|
||||
|
||||
auto id = buses[0].id;
|
||||
auto minutes = buses[0].id - (ready % buses[0].id);
|
||||
for (auto i = usize(1); i < buses.size(); ++i) {
|
||||
auto local_minutes = buses[i].id - (ready % buses[i].id);
|
||||
if (local_minutes < minutes) {
|
||||
id = buses[i].id;
|
||||
minutes = local_minutes;
|
||||
}
|
||||
}
|
||||
print((id * minutes));
|
||||
|
||||
auto req_base = usize(0);
|
||||
auto req_loop = usize(1);
|
||||
for (auto i = usize(1); i < buses.size(); ++i) {
|
||||
auto base_loop = usize(-1);
|
||||
while ((++base_loop * buses[0].id + buses[i].index) % buses[i].id != 0);
|
||||
|
||||
auto rep_loop = usize(0);
|
||||
while ((++rep_loop * buses[0].id) % buses[i].id != 0);
|
||||
|
||||
req_base = find_least(base_loop, rep_loop, req_base, req_loop, -1);
|
||||
if (i < buses.size() - 1) {
|
||||
req_loop = find_least(0, rep_loop, 0, req_loop, 0);
|
||||
}
|
||||
/*
|
||||
required_base (rqb), required_loop (rql)
|
||||
|
||||
base_loop (bl), repeat_loop (rl)
|
||||
|
||||
rqb + a0 * rql = bl + a1 * rl < minimize and store in required loop
|
||||
a0 = (bl + a1 * rl - rqb) / rql
|
||||
|
||||
b0 * rql = b1 * rl < minimize and store in required loop
|
||||
*/
|
||||
}
|
||||
print((req_base * buses[0].id));
|
||||
}
|
112
2020/days/day14.cpp
Normal file
112
2020/days/day14.cpp
Normal file
|
@ -0,0 +1,112 @@
|
|||
/*******************************************************************************
|
||||
*
|
||||
* 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 <map>
|
||||
#include <string>
|
||||
#include <bitset>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
#include "../misc/types.hpp"
|
||||
#include "../misc/print.hpp"
|
||||
|
||||
struct Op{ u8 op; u64 num1; u64 num2; };
|
||||
|
||||
auto set_memory(std::map<u64, u64> & map, u64 addr, const std::vector<bool> & maskX, usize index, u64 value) -> void {
|
||||
while (++index < maskX.size() && !maskX[index]);
|
||||
if (index == maskX.size()) {
|
||||
map.insert_or_assign(addr, value);
|
||||
} else {
|
||||
set_memory(map, addr, maskX, index, value);
|
||||
set_memory(map, addr ^ (u64(1) << (maskX.size() - index - 1)), maskX, index, value);
|
||||
}
|
||||
}
|
||||
|
||||
auto day14() -> void {
|
||||
auto ops = std::vector<Op>();
|
||||
{
|
||||
auto line = std::string();
|
||||
auto file = std::ifstream("inputs/day14.input");
|
||||
while (getline(file, line)) {
|
||||
if (line.find("mask") != std::string::npos) {
|
||||
auto mask0 = u64(-1);
|
||||
auto mask1 = u64( 0);
|
||||
for (auto i = usize(7); i < line.size(); ++i) {
|
||||
if (line[i] == '0') {
|
||||
mask0 ^= (u64(1) << (42 - i));
|
||||
} else if (line[i] == '1') {
|
||||
mask1 ^= (u64(1) << (42 - i));
|
||||
}
|
||||
}
|
||||
ops.push_back({ 0, mask0, mask1 });
|
||||
} else {
|
||||
auto value = line.find("=") + 2;
|
||||
ops.push_back({
|
||||
1,
|
||||
u64(std::stoll(line.substr(4, line.find("]") - 4))),
|
||||
u64(std::stoll(line.substr(value, line.size() - value)))
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
auto mask0 = u64(-1);
|
||||
auto mask1 = u64( 0);
|
||||
auto memory = std::map<u64, u64>();
|
||||
for (auto op : ops) {
|
||||
if (op.op == 0) {
|
||||
mask0 = op.num1;
|
||||
mask1 = op.num2;
|
||||
} else {
|
||||
memory.insert_or_assign(op.num1, (op.num2 & mask0) | mask1);
|
||||
}
|
||||
}
|
||||
auto sum = u64(0);
|
||||
for(auto mem : memory) {
|
||||
sum += mem.second;
|
||||
}
|
||||
print(sum);
|
||||
|
||||
mask1 = u64(0);
|
||||
memory = std::map<u64, u64>();
|
||||
auto maskX = std::vector<bool>(36);
|
||||
for (auto op : ops) {
|
||||
if (op.op == 0) {
|
||||
mask1 = op.num2;
|
||||
for (auto i = usize(0); i < maskX.size(); ++i) {
|
||||
auto bit = u64(1) << u64(35 - i);
|
||||
maskX[i] = (op.num1 & bit) && !(op.num2 & bit);
|
||||
}
|
||||
} else {
|
||||
set_memory(memory, op.num1 | mask1, maskX, -1, op.num2);
|
||||
}
|
||||
}
|
||||
sum = u64(0);
|
||||
for(auto mem : memory) {
|
||||
sum += mem.second;
|
||||
}
|
||||
print(sum);
|
||||
}
|
Loading…
Reference in a new issue