commit 0aefd410a18acf826af5187cdb038fd9b4f25fc5
parent 6c5fe99d41a3d63975deb08911ebb0e162684a43
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date: Sun, 1 Dec 2024 09:40:19 +0100
Started 2024 + day 1
Diffstat:
10 files changed, 156 insertions(+), 0 deletions(-)
diff --git a/2024/.gitignore b/2024/.gitignore
@@ -0,0 +1,2 @@
+*/a
+*/b
diff --git a/2024/01/Makefile b/2024/01/Makefile
@@ -0,0 +1,24 @@
+CC=g++ -std=c++20 -g -Wall
+
+a:
+ ${CC} -o a.out day01a.cpp
+
+b:
+ ${CC} -o b.out day01b.cpp
+
+clean:
+ rm -f a b
+
+atest: a
+ ./a.out
+
+btest: b
+ ./b.out
+
+arun: a
+ ./a.out < input
+
+brun: b
+ ./b.out < input
+
+.PHONY: a b clean atest btest arun brun
diff --git a/2024/01/b.out b/2024/01/b.out
Binary files differ.
diff --git a/2024/01/day01a.cpp b/2024/01/day01a.cpp
@@ -0,0 +1,24 @@
+#include <iostream>
+#include <algorithm>
+#include <vector>
+using namespace std;
+
+int main() {
+ int x, y, tot;
+ vector<int> v, w;
+
+ while (cin >> x >> y) {
+ v.push_back(x);
+ w.push_back(y);
+ }
+
+ sort(v.begin(), v.end());
+ sort(w.begin(), w.end());
+
+ tot = 0;
+ for (unsigned i = 0; i < v.size(); i++)
+ tot += abs(v[i] - w[i]);
+
+ cout << tot << endl;
+ return 0;
+}
diff --git a/2024/01/day01b.cpp b/2024/01/day01b.cpp
@@ -0,0 +1,21 @@
+#include <iostream>
+#include <algorithm>
+#include <vector>
+using namespace std;
+
+int main() {
+ int x, y, tot;
+ vector<int> v, w;
+
+ while (cin >> x >> y) {
+ v.push_back(x);
+ w.push_back(y);
+ }
+
+ tot = 0;
+ for (unsigned i = 0; i < v.size(); i++)
+ tot += v[i] * count(w.begin(), w.end(), v[i]);
+
+ cout << tot << endl;
+ return 0;
+}
diff --git a/2024/README.md b/2024/README.md
@@ -0,0 +1,41 @@
+# Advent of Code 2024
+
+This is my second year trying the AoC. This time I decided to take this
+as an excuse to improve my C++ skills, so I'll use that instead of C.
+
+As last year, my solutions are not commented, but feel free to send
+me an email at sebastiano@tronto.net if you want to know how I solved
+each problem!
+
+## Template
+
+This time I am trying to use a decent template for each day. Here are the
+instructions in case you want to use it.
+
+To start a new day run
+
+```
+$ ./init N
+```
+
+For example, to start day 7 you should run:
+
+```
+$ ./init.sh 7
+```
+
+and the script is going to create a folder `07` and copy some template
+files there. Then you can copy the `input` file into that folder, edit
+`dayNNa.cpp` and once your code is ready you can run
+
+```
+$ make arun
+```
+
+To run the first part with the downloaded input file. You can also use
+`make atest` to take the input from standard input, so you can copy-paste
+the test case into your terminal; press Ctrl-D to enter and EOF and
+terminate the input.
+
+For second part you can edit the file `dayNNb.cpp` and run `make btest`
+and `make brun`.
diff --git a/2024/init.sh b/2024/init.sh
@@ -0,0 +1,7 @@
+#!/bin/sh
+
+nn="$(printf '%02d' $1)"
+mkdir -p $nn
+sed "s/DAY/$nn/g" template.make > $nn/Makefile
+cp template.cpp $nn/day${nn}a.cpp
+cp template.cpp $nn/day${nn}b.cpp
diff --git a/2024/learned.txt b/2024/learned.txt
@@ -0,0 +1,3 @@
+List of things I learned (or refreshed) with this year's AoC.
+
+* Day 1: std::count()
diff --git a/2024/template.cpp b/2024/template.cpp
@@ -0,0 +1,10 @@
+#include <iostream>
+#include <vector>
+#include <algorithm>
+using namespace std;
+
+int main() {
+ while (cin >> TODO)
+ ;
+ return 0;
+}
diff --git a/2024/template.make b/2024/template.make
@@ -0,0 +1,24 @@
+CC=g++ -std=c++20 -g -Wall
+
+a:
+ ${CC} -o a.out dayDAYa.cpp
+
+b:
+ ${CC} -o b.out dayDAYb.cpp
+
+clean:
+ rm -f a b
+
+atest: a
+ ./a.out
+
+btest: b
+ ./b.out
+
+arun: a
+ ./a.out < input
+
+brun: b
+ ./b.out < input
+
+.PHONY: a b clean atest btest arun brun