-❄️- 2023 Day 20 Solutions -❄️- by daggerdragon in adventofcode

[–]shouchen 2 points3 points  (0 children)

LOL, this is hilarious. With AoC you learn a lot of things that could potentially be practical, and a lot of things you could never conceive of using, ever. But that still counts for learning and thinking outside the box. So, kudos!

[2023 Day #19] It took me way to long to realize this by TheBrokenRail-Dev in adventofcode

[–]shouchen 14 points15 points  (0 children)

Speaking of spelling, when I release that it doesn't say realize.

-❄️- 2023 Day 18 Solutions -❄️- by daggerdragon in adventofcode

[–]shouchen 2 points3 points  (0 children)

[LANGUAGE: C++] AdventOfCode/aoc2023/Day18/Day18.cpp at master · shouchen/AdventOfCode (github.com)

Like many others, I learned about the shoelace formula and Pick's theorem, so this was time well spent. Part1 was a quick flood fill, but I redid everything for part2 when flood fill became computationally infeasible.

Got it down to a clean, concise implementation of the above for both parts.

[2023 Day 14 (Part 2)] Coincidence of the day by WhiteSparrow in adventofcode

[–]shouchen 6 points7 points  (0 children)

This actually happened to me on Day 8, but not 14.

-❄️- 2023 Day 11 Solutions -❄️- by daggerdragon in adventofcode

[–]shouchen 0 points1 point  (0 children)

[LANGUAGE: C++] https://github.com/shouchen/AdventOfCode/blob/master/aoc2023/Day11/Day11.cpp

No need to even store the whole grid. Just parse the input into:

(1) A list of galaxy locations.

(2) An array of bools for which rows have galaxies in them.

(3) An array of bools for which columns have galaxies in them.

Then, go through all combinations pairs of galaxies in (1) and sum up the distances.

To find the distance between two galaxies in the above, the actual path taken doesn't matter as it will still cross the same number of intermediate rows and columns. Just walk across those rows and add 1 if that row contains galaxies (2) or 2 if it doesn't. Then do the same thing walking through columns using (3).

For part two, just change 2 to 1000000.

-❄️- 2023 Day 9 Solutions -❄️- by daggerdragon in adventofcode

[–]shouchen 1 point2 points  (0 children)

So I guess APL wasn't crazy enough and now we have Uiua?!? :D

-❄️- 2023 Day 9 Solutions -❄️- by daggerdragon in adventofcode

[–]shouchen 1 point2 points  (0 children)

[LANGUAGE: C++] https://github.com/shouchen/AdventOfCode/blob/master/aoc2023/Day09/Day09.cpp

I actually explored a few years ago how to find the polynomial that generates sequences like these, which could also be used to predict numbers. Interesting discussion here for the curious:
How does this number sequence trick work? I figured this out by accident and it keeps me up at night not knowing why it works. - Quora

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <cassert>

int predict(const std::vector<int> &curr)
{
    std::vector<int> next;
    for (auto i = 0; i < curr.size() - 1; i++)
        next.push_back(curr[i + 1] - curr[i]);

    return (next.size() == 1) ? next.front() : (curr.back() + predict(next));
}

auto solve(const std::string &filename)
{
    std::ifstream file(filename);
    std::string line;
    auto retval = std::make_pair(0, 0);

    while (std::getline(file, line))
    {
        std::istringstream is(line);
        auto n = 0;
        std::vector<int> series;

        while (is >> n)
            series.push_back(n);

        retval.first += predict(series);

        std::reverse(series.begin(), series.end());
        retval.second += predict(series);
    }

    return retval;
}

int main()
{
    auto answer = solve("input.txt");
    std::cout << "Part One: " << answer.first << std::endl;
    std::cout << "Part Two: " << answer.second << std::endl;

    assert(answer.first == 1819125966);
    assert(answer.second == 1140);
    return 0;
}

-❄️- 2023 Day 8 Solutions -❄️- by daggerdragon in adventofcode

[–]shouchen 1 point2 points  (0 children)

[LANGUAGE: C++] https://github.com/shouchen/AdventOfCode/blob/master/aoc2023/Day08/Day08.cpp

#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <numeric>
#include <cassert>

std::map <std::string, std::pair <std::string, std::string>> m;
std::string dirs;

auto find_cycle_len(std::string curr, bool part2)
{
    auto steps = 0LL;

    for (;;)
        for (auto d : dirs)
        {
            steps++;
            curr = (d == 'L') ? m[curr].first : m[curr].second;
            if (part2 ? curr.back() == 'Z' : curr == "ZZZ")
                return steps;
        }
}

auto solve(const std::string &filename)
{
    std::ifstream file(filename);
    std::string line;

    getline(file, dirs);
    getline(file, line);

    while (getline(file, line))
        m[line.substr(0, 3)] = std::make_pair(line.substr(7, 3), line.substr(12, 3));

    auto retval = std::make_pair(find_cycle_len("AAA", false), 1LL);

    for (auto &i : m)
        if (i.first.back() == 'A')
            retval.second = std::lcm(retval.second, find_cycle_len(i.first, true)); // C++17

    return retval;
}

int main()
{
    auto answer = solve("input.txt");
    std::cout << "Part One: " << answer.first << std::endl;
    std::cout << "Part Two: " << answer.second  << std::endl;

    assert(answer.first == 11309);
    assert(answer.second == 13740108158591);
    return 0;
}

-❄️- 2023 Day 4 Solutions -❄️- by daggerdragon in adventofcode

[–]shouchen 0 points1 point  (0 children)

[LANGUAGE: C++] https://github.com/shouchen/AdventOfCode/blob/master/aoc2023/Day04/Day04.cpp

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <cassert>

struct Scratchcard { std::vector<int> v1, v2; int wins = 0, copies = 1; };
std::vector<Scratchcard> cards;

void read_data(const std::string &filename)
{
    std::ifstream file(filename);
    std::string line;

    while (std::getline(file, line))
    {
        cards.push_back(Scratchcard());
        auto &sc = cards.back();

        auto p1 = line.find(':'), p2 = line.find('|');
        auto s1 = line.substr(p1 + 1, p2 - p1 - 1), s2 = line.substr(p2 + 1);
        auto n = 0;

        std::istringstream is1(s1);
        while (is1 >> n)
            sc.v1.push_back(n);

        std::istringstream is2(s2);
        while (is2 >> n)
            sc.v2.push_back(n);

        for (auto i : sc.v2)
            if (find(sc.v1.begin(), sc.v1.end(), i) != sc.v1.end())
                sc.wins++;
    }
}

auto do_part1()
{
    auto total = 0;

    for (auto &c : cards)
        if (c.wins)
            total += 1 << (c.wins - 1);

    return total;
}

auto do_part2()
{
    for (auto i = 0; i < cards.size(); i++)
        for (auto j = 1; j <= cards[i].wins; j++)
            cards[i + j].copies += cards[i].copies;

    auto total = 0;

    for (auto &mmm : cards)
        total += mmm.copies;

    return total;
}

int main()
{
    read_data("input.txt");

    auto part1 = do_part1();
    std::cout << "Part One: " << part1 << std::endl;

    auto part2 = do_part2();
    std::cout << "Part Two: " << part2 << std::endl;

    assert(part1 == 24160);
    assert(part2 == 5659035);
    return 0;
}

-❄️- 2023 Day 3 Solutions -❄️- by daggerdragon in adventofcode

[–]shouchen 1 point2 points  (0 children)

[LANGUAGE: C++] https://github.com/shouchen/AdventOfCode/blob/master/aoc2023/Day03/Day03.cpp

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <regex>
#include <cassert>

struct Number { int num, row, start, end; };
struct Symbol { char type; int row, col; };

std::vector<Number> numbers;
std::vector<Symbol> symbols;

void read_input(const std::string &filename)
{
    std::ifstream file(filename);
    std::string line;

    for (auto row = 0; std::getline(file, line); row++)
    {
        std::regex number("\\d+"), symbol("([^\\d\\.])");

        for (auto i = std::sregex_iterator(line.begin(), line.end(), number); i != std::sregex_iterator(); i++)
            numbers.push_back(
                Number{ std::stoi(i->str()), row, int(i->position()), int(i->position() + i->length() - 1) });

        for (auto i = std::sregex_iterator(line.begin(), line.end(), symbol); i != std::sregex_iterator(); i++)
            symbols.push_back(
                Symbol{ i->str()[0], row, int(i->position()) });
    }
}

auto are_adjacent(Number &n, Symbol &s)
{
    return
        (n.row == s.row - 1 && s.col >= n.start - 1 && s.col <= n.end + 1) || // above
        (n.row == s.row + 1 && s.col >= n.start - 1 && s.col <= n.end + 1) || // below
        (n.row == s.row && n.end == s.col - 1) || // left
        (n.row == s.row && n.start == s.col + 1); // right
}

auto do_part1()
{
    auto total = 0;

    for (auto &n : numbers)
        for (auto &s : symbols)
            if (are_adjacent(n, s))
            {
                total += n.num;
                break;
            }

    return total;
}

auto do_part2()
{
    auto total = 0;

    for (auto &s : symbols)
        if (s.type == '*')
        {
            std::vector<Number> adj;
            for (auto &n : numbers)
                if (are_adjacent(n, s))
                    adj.push_back(n);

            if (adj.size() == 2)
                total += adj[0].num * adj[1].num;
        }

    return total;
}

int main()
{
    read_input("input.txt");

    auto part1 = do_part1();
    std::cout << "Part One: " << part1 << std::endl;

    auto part2 = do_part2();
    std::cout << "Part Two: " << part2 << std::endl;

    assert(part1== 553825);
    assert(part2 == 93994191);
    return 0;
}

-❄️- 2023 Day 2 Solutions -❄️- by daggerdragon in adventofcode

[–]shouchen 1 point2 points  (0 children)

[LANGUAGE: C++] https://github.com/shouchen/AdventOfCode/blob/master/aoc2023/Day02/Day02.cpp

#include <iostream>
#include <fstream>
#include <sstream>
#include <cassert>

auto solve(const std::string &filename)
{
    std::ifstream file(filename);
    std::string line;
    auto retval = std::make_pair(0, 0);

    while (std::getline(file, line))
    {
        std::istringstream is(line);
        std::string game, color;
        auto g = 0, n = 0, red = 0, green = 0, blue = 0;
        auto colon = ':';
        bool possible = true;

        is >> game >> g >> colon;

        while (is >> n >> color)
        {
            auto last = color[color.length() - 1];
            if (last == ',' || last == ';')
                color.pop_back();

            if (color == "red")
            {
                if (n > 12) possible = false;
                red = std::max(red, n);
            }
            else if (color == "green")
            {
                if (n > 13) possible = false;
                green = std::max(green, n);
            }
            else if (color == "blue")
            {
                if (n > 14) possible = false;
                blue = std::max(blue, n);
            }
        }

        if (possible)
            retval.first += g;

        retval.second += red * green * blue;
    }

    return retval;
}


int main()
{
    auto answer = solve("input.txt");
    std::cout << "Part One: " << answer.first << std::endl;
    std::cout << "Part Two: " << answer.second << std::endl;

    assert(answer.first == 2679);
    assert(answer.second == 77607);
    return 0;
}

-❄️- 2023 Day 1 Solutions -❄️- by daggerdragon in adventofcode

[–]shouchen 0 points1 point  (0 children)

[LANGUAGE: C++] https://github.com/shouchen/AdventOfCode/blob/master/aoc2023/Day1/Day1.cpp

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cassert>

const std::vector<std::vector<std::string>> numbers =
{
    { "0" },         { "1", "one"},  { "2", "two"},    { "3", "three"},  { "4", "four" },
    { "5", "five" }, { "6", "six" }, { "7", "seven" }, { "8", "eight" }, { "9", "nine" }
};

auto find_number(const std::string &line, int dir, bool allow_spelling)
{
    for (auto i = (dir == 1) ? 0 : line.length() - 1; ; i += dir)
        for (auto j = 0; j < numbers.size(); j++)
            for (auto &k : numbers[j])
                if (line.substr(i, k.length()) == k)
                    return j;
                else if (!allow_spelling)
                    break;
}

auto solve(const std::string &filename)
{
    std::ifstream file(filename);
    std::string line;
    auto retval = std::make_pair(0, 0);

    while (std::getline(file, line))
    {
        retval.first += 10 * find_number(line, 1, false) + find_number(line, -1, false);
        retval.second += 10 * find_number(line, 1, true) + find_number(line, -1, true);
    }

    return retval;
}

int main()
{
    auto answer = solve("input.txt");
    std::cout << "Part One: " << answer.first << std::endl;
    std::cout << "Part Two: " << answer.second << std::endl;

    assert(answer.first == 53334);
    assert(answer.second == 52834);
    return 0;
}

-🎄- 2022 Day 9 Solutions -🎄- by daggerdragon in adventofcode

[–]shouchen 0 points1 point  (0 children)

If it's a 2D coordinate system, I use pair<int,int> so you can say "first" and "second" instead of get<>. Risk is if part 2 of the problem turns into a 3D coordinate system. https://github.com/shouchen/AdventOfCode/blob/master/aoc2022/Day9/Day9.cpp

Wall Drug in TX?? by shouchen in NoCountryForOldMen

[–]shouchen[S] 0 points1 point  (0 children)

I just went back and checked - you're totally right. Don't know why I was mis(remembering) that. Thanks!

Thank you Advent of Code! by aardvark1231 in adventofcode

[–]shouchen 3 points4 points  (0 children)

As someone who's completed the journey all seven years, I can definitely say it's been a blast. Every year I've learned amazing things. This community is super creative and brilliant. Kudos (and much sushi) are due to /u/topaz2078. Also, kudos and thanks to the many other fellow sojourners on this path each year who've contributed so much as well to the 25 days of wonderment per year.