Advent of Code 2024 - The process
Access this event at: https://adventofcode.com/2024
— — Day 1: Historian Hysteria — —
For the first challenge, we are given a list with two columns of digits. To get to the solution we need to open and read the given file, create pairs with the consecutive smaller number of each column, get the difference between each pair of values and sum them.
- Open and read the given file
#include <iostream>
#include <fstream>
using namespace std;
int main () {
// Open a text file (input.txt)
ifstream file("input.txt");
// Confirm that the file was open correctly
if (file.is_open()) {
(...) // Process file
file.close(); // Close file
}
else
cout << "Error opening the file";
return 0;
}
2. Create pairs with the consecutive smaller number of each column
#include <vector>
#include <algorithm>
int main () {
// (...)
// Process file
int a, b;
// Create two int vectors to store the columns values
vector<int> col1, col2;
while (file >> a >> b) {
col1.push_back(a); // Add a value to the end of a vector
col2.push_back(b);
}
sort(col1.begin(), col1.end()); // Sort values so that the smallest ones
sort(col2.begin(), col2.end()); // are align in both columns
}
3. Get the difference between each pair of values and sum them
#include <vector>
int main () {
// (...)
// Process file
int diff_ab = 0;
size_t size (col1.size()); // Get the colum size
// Iterate through all elements of both columns and
// sum the difference between each pair values
for (int i = 0; i < size; i++) {
diff_ab += abs(col1[i] - col2[i]); // Return the absolute value
}
cout << diff_ab << endl;
return 0;
}
— — Day 2: Historian Hysteria — —
In the second challenge we are given a list with many rows of numbers (i.e. “20 21 24 25 27 29 27”). The objective here is to find how many of those rows are “safe”, that means, the ones that follow some specific rules:
- The levels are either all increasing or all decreasing.
- Any two adjacent levels differ by at least one and at most three.
- First we open the file (same as the previous challenge but this time I did it in C)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*
The levels are either all increasing or all decreasing.
Any two adjacent levels differ by at least one and at most three.
*/
int main()
{
// Open file
FILE *file;
file = fopen("input.txt", "rb");
// Confirm that the file was open
if (file != NULL) {
// Process file (...)
fclose(file);
return 0;
}
else { printf("The file could not be open!"); }
return 0;
}
2. Convert each line into arrays of integers
// (...)
// Process file (...)
if (file != NULL) {
char line[256]; // Enough to all sizes of lines
while(fgets(line, sizeof(line), file)) {
int len = 0;
// The len variable is sent as an address so that it will be
// updated with the location where the line ends
int *arr = strToArr(line, &len);
// (...) Process Arrays
free (arr); // Free dynamically allocated memory
}
// (...)
}
int *strToArr(char *str, int* len) {
// Alocate the necessary memory for the array
int *arr = (int*)malloc(strlen(str) * sizeof(int));
*len = 0;
// Parse a string based on a character
char* token = strtok(str, " ");
while (token != NULL){
arr[*len] = atoi(token); // Convert string of digits into a integer
(*len)++; // Advance the len pointer
token = strtok(NULL, " "); // Parse the string for the next number
}
return arr;
}
3. Check the array to see if it is “safe”
// (...)
// Process array(...)
int increase = 0, decrease = 0, safe = 0;
for (int i = 0; i < len - 1; i++) {
// Check if a pair of digists is increasing or decreasing
if (arr[i] > arr[i + 1] && (arr[i] - arr[i + 1]) <= 3) {
decrease++;
} else if(arr[i] < arr[i + 1] && (arr[i + 1] - arr[i]) <= 3) {
increase++;
} else { increase = decrease = 0; break; } // Break if digits are the same
// Check if there are as many increases or decreases as the array size
if (increase == len - 1 || decrease == len - 1) { safe++; increase = decrease = 0; break; }
// else break the loop and reeniciate variables
else if (increase > 0 && decrease > 0) {increase = decrease = 0; break;}
}
fclose(file);
printf("\nSAFE: %i", safe); // 526
return 0;
— — Day 3: Mull It Over — -
For this challenge we get a bunch of random information mixed up. The objective is to find all multiplication instances (i.e. mul(2*4)), multiply the values and then sum all the results. If there is invalid characters in the middle of the instruction the instruction is invalidated (i.e mul( 2 * 4 ) or mul(2!4)).