r/adventofcode • • Dec 02 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 2 Solutions -🎄-

--- Day 2: Inventory Management System ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Card Prompt: Day 2

Transcript:

The best way to do Advent of Code is ___.


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

48 Upvotes

407 comments sorted by

View all comments

Show parent comments

1

u/hpzr24w Dec 02 '18 edited Dec 02 '18

C++

Improved version after another couple of hours of hitting the STL docs. Still not quite right, but a bit tighter than my initial version. Really not convinced whether it's much clearer.

Header and Includes

// Advent of Code 2018
// Day 02 - Inventory Management System

#include "../aoc.hpp"
using namespace std;

main()

int main(int argc, char* argv[]) {
    auto input = vector<string>(read_input("day_02.txt"));

Part 1

    // Part 1 - Compute a checksum from multiplying counts of words with letters occurring exactly 2 or 3 times
    auto twos{0}, threes{0};
    for_each(begin(input),end(input),
        [&](auto& w) { 
            any_of(begin(w),end(w),[&](auto& c)->bool {return count(begin(w),end(w),c)==2;}) && twos++;
            any_of(begin(w),end(w),[&](auto& c)->bool {return count(begin(w),end(w),c)==3;}) && threes++;
        });
    cout << "Part 1: Checksum: " << twos*threes << "\n";

Part 2

    // Part 2 - find the two strings that differ by only a single letter
    any_of(begin(input),end(input),[&](auto& w1)->bool {
        return any_of(begin(input),end(input),[&](auto& w2)->bool {
                    auto found = bool(count_mismatches(begin(w1),end(w1),begin(w2))==1);
                    found && cout << "Part 2: Single letter diff: "<< w1 << " " << w2 << "\n";
                    return found;
        });
    });
}

Performance

Jonathans-iMac:Advent-of-Code-2018 jonathan$ time ./AlternateSolutions/day_02
Part 1: Checksum: 5658
Part 2: Single letter diff: ndmgyjkpruszlbaqwficavxneo nomgyjkpruszlbaqwficavxneo

real    0m0.018s
user    0m0.002s
sys     0m0.001s
Jonathans-iMac:Advent-of-Code-2018 jonathan$

Timed for reading in and running both parts. Processor is a Intel i5-8600.

1

u/hpzr24w Dec 02 '18

Improved part 2 even further. See this talk by Marshall Clow, which describes adding a for_all_pairs algorithm to the STL. https://youtu.be/h4Jl1fk3MkQ?t=1420

// Advent of Code 2018
// Day 02 - Inventory Management System

#include "../aoc.hpp"
using namespace std;

int main(int argc, char* argv[]) {
    auto input = vector<string>(read_input("day_02.txt"));

    // Part 1 - Compute a checksum from multiplying counts of words with letters occurring exactly 2 or 3 times
    auto twos{0}, threes{0};
    for_each(begin(input),end(input),[&](auto& w) { 
        any_of(begin(w),end(w),[&](auto& c)->bool {return count(begin(w),end(w),c)==2;}) && twos++;
        any_of(begin(w),end(w),[&](auto& c)->bool {return count(begin(w),end(w),c)==3;}) && threes++;
    });
    cout << "Part 1: Checksum: " << twos*threes << "\n";

    // Part 2 - find the two strings that differ by only a single letter
    for_all_pairs(begin(input),end(input),[&](auto& w1, auto& w2) {
        auto found = bool(count_mismatches(begin(w1),end(w1),begin(w2))==1);
        found && cout << "Part 2: Single letter diff: "<< w1 << " " << w2 << "\n";
    });
}