From a70e623f2a54ecee300b835f7605e2038bb44da6 Mon Sep 17 00:00:00 2001 From: Gnarwhal Date: Tue, 1 Dec 2020 00:00:04 -0800 Subject: [PATCH] Initial setup and day1 --- .gitignore | 2 + 2020/Makefile | 10 +++ 2020/day1.cpp | 79 +++++++++++++++++++ 2020/day1.input | 200 ++++++++++++++++++++++++++++++++++++++++++++++++ 2020/types.cpp | 47 ++++++++++++ 2020/types.hpp | 70 +++++++++++++++++ LICENSE | 21 +++++ README.md | 3 + 8 files changed, 432 insertions(+) create mode 100644 .gitignore create mode 100644 2020/Makefile create mode 100644 2020/day1.cpp create mode 100644 2020/day1.input create mode 100644 2020/types.cpp create mode 100644 2020/types.hpp create mode 100644 LICENSE create mode 100644 README.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1a8f3a8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +out/ +*.out diff --git a/2020/Makefile b/2020/Makefile new file mode 100644 index 0000000..fe418f1 --- /dev/null +++ b/2020/Makefile @@ -0,0 +1,10 @@ +day1.out: out/types.o day1.cpp day1.input + g++ $(flags) day1.cpp out/types.o -o $@ + +out/types.o: types.hpp types.cpp + mkdir -p out/ + g++ $(flags) -c types.cpp -o $@ + +.PHONY: clean +clean: + rm out/* day*.out diff --git a/2020/day1.cpp b/2020/day1.cpp new file mode 100644 index 0000000..a9a028c --- /dev/null +++ b/2020/day1.cpp @@ -0,0 +1,79 @@ +/******************************************************************************* + * + * 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 + +#include "types.hpp" + +// Find 2 numbers that sum to 2020 +auto find_2020_x2(const std::vector & list) -> void { + auto begin = 0; + auto end = list.size() - 1; + auto sum = 0; + while ((sum = list[begin] + list[end]) != 2020) { + if (sum < 2020) { + ++begin; + } else { + --end; + } + } + std::cout << (list[begin] * list[end]) << std::endl; +} + +// Find 3 numbers that sum to 2020 +auto find_2020_x3(const std::vector & list) -> void { + for (auto n0 = 0; n0 < list.size() - 2; ++n0) { + for (auto n1 = 1; n1 < list.size() - 1; ++n1) { + for (auto n2 = 2; n2 < list.size(); ++n2) { + if (list[n0] + list[n1] + list[n2] == 2020) { + std::cout << (list[n0] * list[n1] * list[n2]) << std::endl; + return; + } + } + } + } +} + +i32 main(i32 argc, char * argv[]) { + auto list = std::vector(); + { + auto line = std::string(); + auto file = std::ifstream("day1.input"); + while (getline(file, line)) { + list.push_back(std::stoi(line)); + } + } + std::sort(list.begin(), list.end()); + + find_2020_x2(list); + find_2020_x3(list); + + return 0; +} diff --git a/2020/day1.input b/2020/day1.input new file mode 100644 index 0000000..3bea509 --- /dev/null +++ b/2020/day1.input @@ -0,0 +1,200 @@ +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 diff --git a/2020/types.cpp b/2020/types.cpp new file mode 100644 index 0000000..cc4d3da --- /dev/null +++ b/2020/types.cpp @@ -0,0 +1,47 @@ +/******************************************************************************* + * + * 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" + +constexpr auto operator "" _u8 (unsigned long long int num) noexcept -> u8 { return u8(num); } +constexpr auto operator "" _i8 (unsigned long long int num) noexcept -> i8 { return u8(num); } +constexpr auto operator "" _u16 (unsigned long long int num) noexcept -> u16 { return u16(num); } +constexpr auto operator "" _i16 (unsigned long long int num) noexcept -> i16 { return i16(num); } +constexpr auto operator "" _u32 (unsigned long long int num) noexcept -> u32 { return u32(num); } +constexpr auto operator "" _i32 (unsigned long long int num) noexcept -> i32 { return i32(num); } +constexpr auto operator "" _u64 (unsigned long long int num) noexcept -> u64 { return u64(num); } +constexpr auto operator "" _i64 (unsigned long long int num) noexcept -> i64 { return i64(num); } +constexpr auto operator "" _usize(unsigned long long int num) noexcept -> usize { return usize(num); } + +constexpr auto operator "" _c8 (char c) noexcept -> c8 { return c8(c); } +constexpr auto operator "" _c16(char c) noexcept -> c16 { return c16(c); } +constexpr auto operator "" _c32(char c) noexcept -> c32 { return c32(c); } +constexpr auto operator "" _c8 (unsigned long long int c) noexcept -> c8 { return c8(c); } +constexpr auto operator "" _c16(unsigned long long int c) noexcept -> c16 { return c16(c); } +constexpr auto operator "" _c32(unsigned long long int c) noexcept -> c32 { return c32(c); } + +constexpr auto operator "" _f32(long double num) noexcept -> f32 { return (f32) num; } +constexpr auto operator "" _f64(long double num) noexcept -> f64 { return (f64) num; } diff --git a/2020/types.hpp b/2020/types.hpp new file mode 100644 index 0000000..bc94dab --- /dev/null +++ b/2020/types.hpp @@ -0,0 +1,70 @@ +/******************************************************************************* + * + * 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 GNARWHAL_REDWOOD_TYPES +#define GNARWHAL_REDWOOD_TYPES + +#include +#include + +using u8 = std::uint8_t; +using i8 = std::int8_t; +using u16 = std::uint16_t; +using i16 = std::int16_t; +using u32 = std::uint32_t; +using i32 = std::int32_t; +using u64 = std::uint64_t; +using i64 = std::int64_t; +using usize = std::size_t; + +constexpr auto operator "" _u8 (unsigned long long int num) noexcept -> u8; +constexpr auto operator "" _i8 (unsigned long long int num) noexcept -> i8; +constexpr auto operator "" _u16 (unsigned long long int num) noexcept -> u16; +constexpr auto operator "" _i16 (unsigned long long int num) noexcept -> i16; +constexpr auto operator "" _u32 (unsigned long long int num) noexcept -> u32; +constexpr auto operator "" _i32 (unsigned long long int num) noexcept -> i32; +constexpr auto operator "" _u64 (unsigned long long int num) noexcept -> u64; +constexpr auto operator "" _i64 (unsigned long long int num) noexcept -> i64; +constexpr auto operator "" _usize(unsigned long long int num) noexcept -> usize; + +using c8 = u8; +using c16 = u16; +using c32 = u32; + +constexpr auto operator "" _c8 (char c) noexcept -> c8; +constexpr auto operator "" _c16(char c) noexcept -> c16; +constexpr auto operator "" _c32(char c) noexcept -> c32; +constexpr auto operator "" _c8 (unsigned long long int c) noexcept -> c8; +constexpr auto operator "" _c16(unsigned long long int c) noexcept -> c16; +constexpr auto operator "" _c32(unsigned long long int c) noexcept -> c32; + +using f32 = float; +using f64 = double; + +constexpr auto operator "" _f32(long double num) noexcept -> f32; +constexpr auto operator "" _f64(long double num) noexcept -> f64; + +#endif // GNARWHAL_REDWOOD_TYPES diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..4ee42b8 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +Mit License + +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. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..02a3830 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Advent of Code + +https://adventofcode.com/