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;
}
Algol 68
So this felt like an old-school programming challenge. Like the type of thing you'd give to a CS student back in the good old days. So I decided to code in the granddaddy of modern languages. I did this in Algol-68!
In fact, Algol 68's event driven input handler was quite nice for this task as was its easy ability to detect the ends of lines. of course, its lack of built-in data structures meant that I had to whip up my own dynamic array, but that's pretty easy too. If you read today's code, you'll see familiar threads that influenced C and most of the languages that followed it.
Algol 68 is meant to be a language for publication. It is really tough to write a compiler for this language, but it is easy to write self-documenting code. In fact, a lot of textbooks publish algorithms in pseudocode that is almost correct Algol 68 code! So I tried to write today's solutions with readability in mind. Of course, this venerable language is not without its flaws, but I do enjoy coding in it from time to time.
https://github.com/pngwen/advent-2023/blob/main/day-9a.a68
https://github.com/pngwen/advent-2023/blob/main/day-9b.a68