23
submitted 1 year ago* (last edited 1 year ago) by Ategon@programming.dev to c/advent_of_code@programming.dev

Day 6: Wait for It


Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ , pastebin, or github (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)

FAQ

you are viewing a single comment's thread
view the rest of the comments
[-] pngwen 2 points 1 year ago* (last edited 1 year ago)

C++

Yesterday, I decided to code in Tcl. That program is still running, i will go back to the day 5 post once it finishes :)

Today was super simple. My first attempt worked in both cases, where the hardest part was really switching my ints to long longs. Part 1 worked on first compile and part 2 I had to compile twice after I realized the data type needs. Still, that change was made by search and replace.

I guess today was meant to be a real time race to get first answer? This is like day 1 stuff! Still, I have kids and a job so I did not get to stay up until the problem was posted.

I used C++ because I thought something intense may be coming on the part 2 problem, and I was burned yesterday. It looks like I spent another fast language on nothing! I think I'll keep zig in the hole for the next number cruncher.

Oh, and yes my TCL program is still running...

My solutions can be found here:

// File: day-6a.cpp
// Purpose: Solution to part of day 6 of advent of code in C++
//          https://adventofcode.com/2023/day/6
// Author: Robert Lowe
// Date: 6 December 2023
#include <iostream>
#include <vector>
#include <string>
#include <sstream>

std::vector<int> parse_line()
{
    std::string line;
    std::size_t index;
    int num;
    std::vector<int> result;
    
    // set up the stream
    std::getline(std::cin, line);
    index = line.find(':');
    std::istringstream is(line.substr(index+1));

    while(is>>num) {
        result.push_back(num);
    }

    return result;
}

int count_wins(int t, int d) 
{
    int count=0;
    for(int i=1; i<t; i++) {
        if(t*i-i*i > d) {
            count++;
        }
    }
    return count;
}

int main()
{
    std::vector<int> time;
    std::vector<int> dist;
    int product=1;

    // get the times and distances
    time = parse_line();
    dist = parse_line();

    // count the total number of wins
    for(auto titr=time.begin(), ditr=dist.begin(); titr!=time.end(); titr++, ditr++) {
        product *= count_wins(*titr, *ditr);
    }

    std::cout << product << std::endl;
}
// File: day-6b.cpp
// Purpose: Solution to part 2 of day 6 of advent of code in C++
//          https://adventofcode.com/2023/day/6
// Author: Robert Lowe
// Date: 6 December 2023
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <cctype>

std::vector<long long> parse_line()
{
    std::string line;
    std::size_t index;
    long long num;
    std::vector<long long> result;
    
    // set up the stream
    std::getline(std::cin, line);
    line.erase(std::remove_if(line.begin(), line.end(), isspace), line.end());
    index = line.find(':');
    std::istringstream is(line.substr(index+1));

    while(is>>num) {
        result.push_back(num);
    }

    return result;
}

long long count_wins(long long t, long long d) 
{
    long long count=0;
    for(long long i=1; i<t; i++) {
        if(t*i-i*i > d) {
            count++;
        }
    }
    return count;
}

int main()
{
    std::vector<long long> time;
    std::vector<long long> dist;
    long long product=1;

    // get the times and distances
    time = parse_line();
    dist = parse_line();

    // count the total number of wins
    for(auto titr=time.begin(), ditr=dist.begin(); titr!=time.end(); titr++, ditr++) {
        product *= count_wins(*titr, *ditr);
    }

    std::cout << product << std::endl;
}
this post was submitted on 06 Dec 2023
23 points (96.0% liked)

Advent Of Code

920 readers
53 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2024

Solution Threads

M T W T F S S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 18 20 21 22
23 24 25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 1 year ago
MODERATORS