Restructured project and updated day 3

This commit is contained in:
Gnarwhal 2020-12-11 09:24:22 -08:00
parent f7c94b2a17
commit 530e53c22c
29 changed files with 245 additions and 1304 deletions

View file

@ -1,12 +1,16 @@
day%.out: out/types.o day%.cpp
g++-10 -std=c++2a $(flags) $(@:out=cpp) out/types.o -o $@
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 $@
day%.cpp:
cp template.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 $@
out/types.o: types.hpp types.cpp
days/day%.cpp:
cp 'misc/day.cpp.template' $@
sed -i -e "s/current_day/$(shell basename $@ | cut -f 1 -d '.')/g" $@
out/types.o: misc/types.hpp misc/types.cpp
mkdir -p out/
g++-10 -std=c++2a $(flags) -c types.cpp -o $@
g++-10 -std=c++2a $(flags) -c misc/types.cpp -o $@
.PHONY: clean
clean:

View file

@ -1,200 +0,0 @@
1587
1407
1717
1596
1566
1752
1925
1847
1716
1726
1611
1628
1853
1864
1831
1942
1634
1964
1603
1676
1256
1906
1655
1790
1666
1470
1540
1544
1100
1447
1384
1464
1651
1572
907
1653
1265
1510
1639
1818
376
1378
1132
1750
1491
1788
1882
1779
1640
1586
1525
1458
1994
1782
1412
1033
1416
1813
1520
1968
715
1396
1745
1506
1024
1798
1870
1615
1957
1718
1349
1983
1387
1738
1588
1321
1160
1907
1861
1940
1475
2004
1852
1760
1608
1028
1820
1495
1811
1737
1417
1316
1087
1803
1595
1346
1971
1692
1678
1330
1480
1097
1898
1973
1567
1733
1336
1381
1327
1670
1436
1989
1334
89
1862
1715
1743
1967
1765
1402
1729
1749
1671
1196
1650
1089
1814
1783
1225
1823
1746
2009
1886
1748
1481
1739
1912
1663
1668
1314
1594
705
1449
1731
1487
1648
1466
1317
1979
1799
1926
1703
1656
1978
2005
1865
1982
1951
1892
1713
1744
1598
1606
1583
1895
1804
1430
1816
1364
1575
1918
1431
1812
1471
1797
928
1934
1156
94
1563
1909
1453
1392
1427
1819
1524
1695
1866
2008
1413
1698
1051
1707
1904
1681
1541
1621
1421
1809
1576

File diff suppressed because it is too large Load diff

View file

@ -30,7 +30,8 @@
#include <iostream>
#include <fstream>
#include "types.hpp"
#include "../misc/types.hpp"
#include "../misc/print.hpp"
// Find 2 numbers that sum to 2020
auto find_2020_x2(const std::vector<i32> & list) -> void {
@ -44,7 +45,7 @@ auto find_2020_x2(const std::vector<i32> & list) -> void {
--end;
}
}
std::cout << (list[begin] * list[end]) << std::endl;
print((list[begin] * list[end]));
}
// Find 3 numbers that sum to 2020
@ -53,13 +54,13 @@ auto find_2020_x3(const std::vector<i32> & list) -> void {
for (auto n1 = 1; n1 < list.size() - 1; ++n1) {
auto low = n0 + 1;
auto high = n1;
while (low < high) {
auto n2 = (low + high) / 2;
while (low < high - 1) {
auto sum = 0;
if ((sum = list[n0] + list[n1] + list[n2]) == 2020) {
std::cout << (list[n0] * list[n1] * list[n2]) << std::endl;
print((list[n0] * list[n1] * list[n2]));
return;
} else if (sum > 2020) {
} else if (sum < 2020) {
low = n2 + 1;
} else {
high = n2;
@ -69,11 +70,11 @@ auto find_2020_x3(const std::vector<i32> & list) -> void {
}
}
auto main(i32 argc, char * argv[]) -> i32 {
auto day1() -> void {
auto list = std::vector<i32>();
{
auto line = std::string();
auto file = std::ifstream("day1.input");
auto file = std::ifstream("inputs/day1.input");
while (getline(file, line)) {
list.push_back(std::stoi(line));
}
@ -82,6 +83,4 @@ auto main(i32 argc, char * argv[]) -> i32 {
find_2020_x2(list);
find_2020_x3(list);
return 0;
}

View file

@ -29,7 +29,8 @@
#include <iostream>
#include <fstream>
#include "types.hpp"
#include "../misc/types.hpp"
#include "../misc/print.hpp"
struct Pair {
u64 jolt;
@ -40,11 +41,11 @@ auto operator<(const Pair & left, const Pair & right) -> bool {
return left.jolt < right.jolt;
}
auto main(i32 argc, char * argv[]) -> i32 {
auto day10() -> void {
auto jolts = std::vector<Pair>{{ 0, 1 }};
{
auto line = std::string();
auto file = std::ifstream("day10.input");
auto file = std::ifstream("inputs/day10.input");
while (getline(file, line)) {
jolts.push_back({ (u64) std::stoll(line), 0 });
}
@ -59,7 +60,7 @@ auto main(i32 argc, char * argv[]) -> i32 {
else if (jolts[i + 1].jolt - jolts[i].jolt == 3) ++dif3;
}
std::cout << (dif1 * dif3) << std::endl;
print((dif1 * dif3));
for (auto i = i32(0); i < jolts.size() - 1; ++i) {
for (auto j = i + 1; j < jolts.size(); ++j) {
@ -70,7 +71,5 @@ auto main(i32 argc, char * argv[]) -> i32 {
}
}
}
std::cout << jolts[jolts.size() - 1].count << std::endl;
return 0;
print(jolts[jolts.size() - 1].count);
}

View file

@ -30,7 +30,8 @@
#include <iostream>
#include <fstream>
#include "types.hpp"
#include "../misc/types.hpp"
#include "../misc/print.hpp"
template <typename Lambda>
auto iterate_seats(const auto & seats, Lambda callback) {
@ -62,12 +63,12 @@ auto count_seats(const auto & seats) -> usize {
return occupied;
}
auto main(i32 argc, char * argv[]) -> i32 {
auto day11() -> void {
auto bit = u8(0b1);
auto seats = std::array<std::vector<std::string>, 2>();
{
auto line = std::string();
auto file = std::ifstream("day11.input");
auto file = std::ifstream("inputs/day11.input");
while (getline(file, line)) {
seats[bit].push_back(line);
}
@ -94,7 +95,7 @@ auto main(i32 argc, char * argv[]) -> i32 {
}
while (!seats_equal(seats));
std::cout << count_seats(seats[0]) << std::endl;
print(count_seats(seats[0]));
iterate_seats(seats[0], [&seats](i32 i, i32 j) {
seats[0][i][j] = seats[1][i][j] = 'L';
@ -129,7 +130,5 @@ auto main(i32 argc, char * argv[]) -> i32 {
});
} while (!seats_equal(seats));
std::cout << count_seats(seats[0]) << std::endl;
return 0;
print(count_seats(seats[0]));
}

View file

@ -29,7 +29,8 @@
#include <iostream>
#include <fstream>
#include "types.hpp"
#include "../misc/types.hpp"
#include "../misc/print.hpp"
struct Password {
u32 min;
@ -72,11 +73,11 @@ auto count_valid_toboggan(const std::vector<Password> & passwords) -> u32 {
return valid;
}
auto main(i32 argc, char * argv[]) -> i32 {
auto day2() -> void {
auto passwords = std::vector<Password>();
{
auto line = std::string();
auto file = std::ifstream("day2.input");
auto file = std::ifstream("inputs/day2.input");
while (getline(file, line)) {
auto index = usize(0);
auto password = Password{};
@ -88,8 +89,6 @@ auto main(i32 argc, char * argv[]) -> i32 {
}
}
std::cout << count_valid_sled(passwords) << std::endl;
std::cout << count_valid_toboggan(passwords) << std::endl;
return 0;
print(count_valid_sled(passwords) );
print(count_valid_toboggan(passwords));
}

View file

@ -29,40 +29,53 @@
#include <iostream>
#include <fstream>
#include "types.hpp"
#include "../misc/types.hpp"
#include "../misc/print.hpp"
auto read_field_and_count_trees(std::vector<std::string> & trees, usize horizontal_increment, usize vertical_increment) -> u64 {
auto tree_count = u64(0);
struct TreeVector{ usize horz; usize vert; };
struct Coordinate{ usize x_index; usize y_index; };
auto read_field_and_count_trees(const std::vector<TreeVector> & vectors) -> std::vector<u64> {
auto tree_counts = std::vector<u64>(vectors.size());
auto coords = std::vector<Coordinate>(vectors.size());
for (auto i = usize(0); i < coords.size(); ++i) {
coords[i] = { usize(-vectors[i].horz), usize(-1) };
}
auto x_index = usize(-horizontal_increment);
auto y_index = usize(-1);
auto line = std::string();
auto file = std::ifstream("day3.input");
auto file = std::ifstream("inputs/day3.input");
while (getline(file, line)) {
auto vertical_check = (++y_index % vertical_increment == 0);
tree_count += vertical_check * (line[(x_index += (vertical_check * horizontal_increment)) % line.size()] == '#');
for (auto i = usize(0); i < vectors.size(); ++i) {
auto & vector = vectors[i];
auto & coord = coords[i];
auto vertical_check = (++coord.y_index % vector.vert == 0);
tree_counts[i] += vertical_check * (line[(coord.x_index += (vertical_check * vector.horz)) % line.size()] == '#');
}
}
return tree_count;
return std::move(tree_counts);
}
auto main(i32 argc, char ** argv) -> i32 {
auto day3() -> void {
auto trees = std::vector<std::string>();
auto tree_counts = std::vector<u64>();
auto tree_counts = read_field_and_count_trees(
std::vector<TreeVector>{
{ 3, 1 },
{ 1, 1 },
{ 5, 1 },
{ 7, 1 },
{ 1, 2 },
}
);
tree_counts.push_back(read_field_and_count_trees(trees, 3, 1));
std::cout << tree_counts[0] << std::endl;
print(tree_counts[0]);
tree_counts.push_back(read_field_and_count_trees(trees, 1, 1));
tree_counts.push_back(read_field_and_count_trees(trees, 5, 1));
tree_counts.push_back(read_field_and_count_trees(trees, 7, 1));
tree_counts.push_back(read_field_and_count_trees(trees, 1, 2));
auto tree_count_product = u64(1);
for (auto tree_count : tree_counts) {
tree_count_product *= tree_count;
}
std::cout << tree_count_product << std::endl;
return 0;
print(tree_count_product);
}

View file

@ -29,7 +29,8 @@
#include <fstream>
#include <iostream>
#include "types.hpp"
#include "../misc/types.hpp"
#include "../misc/print.hpp"
struct KeyValue {
std::string key;
@ -46,13 +47,13 @@ auto try_num(const std::string & passport, usize index) -> bool {
return Min <= num && num <= Max;
};
auto main(i32 argc, char * argv[]) -> i32 {
auto day4() -> void {
auto valid_count1 = usize(0);
auto valid_count2 = usize(0);
auto passport = std::string();
auto line = std::string();
auto file = std::ifstream("day4.input");
auto file = std::ifstream("inputs/day4.input");
while (getline(file, line)) {
if (line == "") {
auto elements = std::vector<KeyValue>{
@ -108,7 +109,6 @@ auto main(i32 argc, char * argv[]) -> i32 {
}
}
std::cout << valid_count1 << ", " << valid_count2 << std::endl;
return 0;
print(valid_count1);
print(valid_count2);
}

View file

@ -30,12 +30,13 @@
#include <iostream>
#include <fstream>
#include "types.hpp"
#include "../misc/types.hpp"
#include "../misc/print.hpp"
auto main(i32 argc, char * argv[]) -> i32 {
auto day5() -> void {
auto seat = usize(0);
auto line = std::string();
auto file = std::ifstream("day5.input");
auto file = std::ifstream("inputs/day5.input");
auto seats = std::vector<usize>();
@ -49,16 +50,14 @@ auto main(i32 argc, char * argv[]) -> i32 {
}
seats.push_back(local_seat);
}
std::cout << seat << std::endl;
print(seat);
std::sort(seats.begin(), seats.end());
for (auto i = usize(0); i < seats.size() - 1; ++i) {
if (seats[i] + 2 == seats[i + 1]) {
std::cout << (seats[i] + 1) << std::endl;
print((seats[i] + 1));
break;
}
}
return 0;
}

View file

@ -29,12 +29,13 @@
#include <iostream>
#include <fstream>
#include "types.hpp"
#include "../misc/types.hpp"
#include "../misc/print.hpp"
auto main(i32 argc, char * argv[]) -> i32 {
auto day6() -> void {
{
auto line = std::string();
auto file = std::ifstream("day6.input");
auto file = std::ifstream("inputs/day6.input");
auto answers = std::vector<u32>(26);
@ -57,8 +58,7 @@ auto main(i32 argc, char * argv[]) -> i32 {
}
}
}
std::cout << sum_1 << ", " << sum_2 << std::endl;
print(sum_1);
print(sum_2);
}
return 0;
}

View file

@ -32,7 +32,8 @@
#include <iostream>
#include <fstream>
#include "types.hpp"
#include "../misc/types.hpp"
#include "../misc/print.hpp"
struct SubBag { std::string type; u32 count; };
@ -60,11 +61,11 @@ auto count_to(const std::string & search, std::map<std::string, Edges> & map) ->
return counted;
}
auto main(i32 argc, char * argv[]) -> i32 {
auto day7() -> void {
auto bag_graph = std::map<std::string, Edges>();
{
auto line = std::string();
auto file = std::ifstream("day7.input");
auto file = std::ifstream("inputs/day7.input");
while (getline(file, line)) {
auto find = line.find("bag") - 1;
auto bag_type = line.substr(0, line.find("bag") - 1);
@ -96,9 +97,7 @@ auto main(i32 argc, char * argv[]) -> i32 {
}
}
}
std::cout << seen.size() << std::endl;
print(seen.size());
std::cout << (count_to("shiny gold", bag_graph) - 1) << std::endl;
return 0;
print((count_to("shiny gold", bag_graph) - 1));
}

View file

@ -29,7 +29,8 @@
#include <iostream>
#include <fstream>
#include "types.hpp"
#include "../misc/types.hpp"
#include "../misc/print.hpp"
enum OpCode { Acc, Jmp, Nop };
struct Op{ OpCode code; i32 num; bool executed; };
@ -81,11 +82,11 @@ auto attempt_swap(State & state, std::vector<Op> & ops) -> bool {
return false;
}
auto main(i32 argc, char * argv[]) -> i32 {
auto day8() -> void {
auto ops = std::vector<Op>();
{
auto line = std::string();
auto file = std::ifstream("day8.input");
auto file = std::ifstream("inputs/day8.input");
while (getline(file, line)) {
ops.push_back(extract_op(line));
}
@ -95,18 +96,16 @@ auto main(i32 argc, char * argv[]) -> i32 {
while (!ops[state.isp].executed) {
execute(state, ops);
}
std::cout << state.acc << std::endl;
print(state.acc);
for (auto & op : ops) { op.executed = false; }
state = State{ 0, 0 };
for (;;) {
if (attempt_swap(state, ops)) {
std::cout << state.acc << std::endl;
print(state.acc);
break;
}
execute(state, ops);
}
return 0;
}

View file

@ -29,7 +29,8 @@
#include <iostream>
#include <fstream>
#include "types.hpp"
#include "../misc/types.hpp"
#include "../misc/print.hpp"
auto find_sum(usize index, std::vector<u64> xmas) {
for (auto i = index - 25; i < index - 1; ++i) {
@ -42,10 +43,10 @@ auto find_sum(usize index, std::vector<u64> xmas) {
return false;
}
auto main(i32 argc, char * argv[]) -> i32 {
auto day9() -> void {
auto xmas = std::vector<u64>();
auto line = std::string();
auto file = std::ifstream("day9.input");
auto file = std::ifstream("inputs/day9.input");
while (getline(file, line)) {
xmas.push_back(std::stoll(line));
}
@ -53,7 +54,7 @@ auto main(i32 argc, char * argv[]) -> i32 {
auto invalid = u64(0);
for (auto i = usize(25); i < xmas.size(); ++i) {
if (!find_sum(i, xmas)) {
std::cout << (invalid = xmas[i]) << std::endl;
print((invalid = xmas[i]));
break;
}
}
@ -82,7 +83,5 @@ auto main(i32 argc, char * argv[]) -> i32 {
max = xmas[i];
}
}
std::cout << (min + max) << std::endl;
return 0;
print((min + max));
}

View file

@ -29,16 +29,15 @@
#include <iostream>
#include <fstream>
#include "types.hpp"
#include "../misc/types.hpp"
#include "../misc/print.hpp"
auto main(i32 argc, char * argv[]) -> i32 {
auto current_day() -> void {
{
auto line = std::string();
auto file = std::ifstream("dayNUM.input");
auto file = std::ifstream("inputs/current_day.input");
while (getline(file, line)) {
}
}
return 0;
}

36
2020/misc/main.cpp Normal file
View file

@ -0,0 +1,36 @@
/*******************************************************************************
*
* 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 "types.hpp"
extern auto current_day() -> void;
auto main(i32 argc, char * argv[]) -> i32 {
current_day();
return 0;
}

66
2020/misc/main_test.cpp Normal file
View file

@ -0,0 +1,66 @@
/*******************************************************************************
*
* 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 <iostream>
#include <string>
#include <chrono>
#include "types.hpp"
extern auto current_day() -> void;
auto main(i32 argc, char * argv[]) -> i32 {
auto TEST_CYCLES = 1;
if (argc >= 2) {
TEST_CYCLES = std::stoi(argv[1]);
}
std::cout << "Starting test with " << TEST_CYCLES << " iterations..." << std::endl;
auto begin1 = std::chrono::high_resolution_clock::now();
for (auto i = usize(0); i < TEST_CYCLES; ++i) {
current_day();
}
auto end1 = std::chrono::high_resolution_clock::now();
if (argc >= 3) {
if (argv[2] == std::string("millis")) {
std::cout << "Tests completed in " << std::chrono::duration_cast<std::chrono::milliseconds>(end1 - begin1).count() << " milliseconds"<< std::endl;
} else if (argv[2] == std::string("nanos")) {
std::cout << "Tests completed in " << std::chrono::duration_cast<std::chrono::nanoseconds>(end1 - begin1).count() << " nanoseconds"<< std::endl;
} else if (argv[2] == std::string("micros")) {
std::cout << "Tests completed in " << std::chrono::duration_cast<std::chrono::microseconds>(end1 - begin1).count() << " microsenconds"<< std::endl;
} else {
std::cout << "Unkown time scale '" << argv[2] << "'" << std::endl;
}
} else {
std::cout << "Tests completed in " << std::chrono::duration_cast<std::chrono::microseconds>(end1 - begin1).count() << " microseconds"<< std::endl;
}
return 0;
}

31
2020/misc/print.hpp Normal file
View file

@ -0,0 +1,31 @@
/*******************************************************************************
*
* 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.
*
*******************************************************************************/
#ifndef TEST_BUILD
#define print(x) std::cout << (x) << std::endl
#else
#define print(x)
#endif