Skip to content

Commit 273e6f0

Browse files
committed
day1 c++ 🤮
1 parent 9d29787 commit 273e6f0

File tree

4 files changed

+86
-10
lines changed

4 files changed

+86
-10
lines changed

2024/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99

1010
Puzzle | Stars | Languages
1111
---------------------------------------------------------------- | ----- | -----------
12-
[Day 1: Historian Hysteria](https://adventofcode.com/2024/day/1) | ⭐⭐ | [Rust](../2024/day1/day1.rs) [Python](../2024/day1/day1.py) [C](../2024/day1/day1.c) [Go](../2024/day1/day1.go) [Ruby](../2024/day1/day1.rb) [JS](../2024/day1/day1.js) [Bash](../2024/day1/day1.sh) [Lua](../2024/day1/day1.lua) [C#](../2024/day1/day1.cs)
12+
[Day 1: Historian Hysteria](https://adventofcode.com/2024/day/1) | ⭐⭐ | [Rust](../2024/day1/day1.rs) [Python](../2024/day1/day1.py) [C](../2024/day1/day1.c) [C++](../2024/day1/day1.cpp) [Go](../2024/day1/day1.go) [Ruby](../2024/day1/day1.rb) [JS](../2024/day1/day1.js) [Bash](../2024/day1/day1.sh) [Lua](../2024/day1/day1.lua) [C#](../2024/day1/day1.cs)
1313
[Day 2: Red-Nosed Reports](https://adventofcode.com/2024/day/2) | ⭐⭐ | [Rust](../2024/day2/day2.rs) [Python](../2024/day2/day2.py)
1414
[Day 3: Mull It Over](https://adventofcode.com/2024/day/3) | ⭐⭐ | [Rust](../2024/day3/day3.rs) [Python](../2024/day3/day3.py)

2024/day1/day1.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// c++ -Wall -Wextra -Werror -std=c++23 -O2 -o day1 day1.cpp
2+
3+
#include <vector>
4+
#include <iostream>
5+
#include <fstream>
6+
#include <sstream>
7+
#include <ranges>
8+
#include <map>
9+
#include <algorithm>
10+
#include <inttypes.h>
11+
12+
static void read_data(const char *filename, std::vector<int> &left, std::vector<int> &right)
13+
{
14+
std::ifstream f;
15+
std::string line;
16+
17+
f.open(filename);
18+
19+
if (!f.is_open())
20+
{
21+
throw std::logic_error("bad filename");
22+
}
23+
24+
while (!f.eof())
25+
{
26+
int a, b;
27+
f >> a >> b;
28+
29+
if (!f.fail())
30+
{
31+
left.push_back(a);
32+
right.push_back(b);
33+
}
34+
}
35+
36+
f.close();
37+
}
38+
39+
int main(int argc, char *argv[])
40+
{
41+
std::vector<int> left, right;
42+
43+
if (argc >= 2)
44+
read_data(argv[1], left, right);
45+
else
46+
read_data("input.txt", left, right);
47+
48+
std::sort(left.begin(), left.end());
49+
std::sort(right.begin(), right.end());
50+
51+
// part 1
52+
53+
int part1 = 0;
54+
for (auto [a, b] : std::views::zip(left, right)) // C++23
55+
{
56+
part1 += abs(a - b);
57+
}
58+
std::cout << part1 << std::endl;
59+
60+
// part 2
61+
62+
std::map<int, int> counter;
63+
for (auto b : right)
64+
{
65+
counter[b] += 1;
66+
}
67+
68+
int part2 = 0;
69+
for (auto a : left)
70+
{
71+
part2 += a * counter[a];
72+
}
73+
std::cout << part2 << std::endl;
74+
75+
return 0;
76+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Made for fun 😎 and to practice Rust. Many thanks to [Eric Wastl](https://twit
1212

1313
Puzzle | Stars | Languages
1414
---------------------------------------------------------------- | ----- | -----------
15-
[Day 1: Historian Hysteria](https://adventofcode.com/2024/day/1) | ⭐⭐ | [Rust](2024/day1/day1.rs) [Python](2024/day1/day1.py) [C](../2024/day1/day1.c) [Go](../2024/day1/day1.go)
15+
[Day 1: Historian Hysteria](https://adventofcode.com/2024/day/1) | ⭐⭐ | [Rust](2024/day1/day1.rs) [Python](2024/day1/day1.py) [C](2024/day1/day1.c) [C++](2024/day1/day1.cpp) [Go](2024/day1/day1.go)
1616
[Day 2: Red-Nosed Reports](https://adventofcode.com/2024/day/2) | ⭐⭐ | [Rust](2024/day2/day2.rs) [Python](2024/day2/day2.py)
1717
[Day 3: Mull It Over](https://adventofcode.com/2024/day/3) | ⭐⭐ | [Rust](2024/day3/day3.rs) [Python](2024/day3/day3.py)
1818

scripts/answers.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -662,14 +662,14 @@ def parse(_self, year, _day):
662662
files.extend(f"[Rust]({year}/{f.relative_to(year_dir)})" for f in sols if f.suffix == ".rs")
663663
files.extend(f"[Python]({year}/{f.relative_to(year_dir)})" for f in sols if f.suffix == ".py")
664664

665-
files.extend(f"[C](../{year}/{f.relative_to(year_dir)})" for f in sols if f.suffix == ".c")
666-
files.extend(f"[C++](../{year}/{f.relative_to(year_dir)})" for f in sols if f.suffix == ".cpp")
667-
files.extend(f"[Go](../{year}/{f.relative_to(year_dir)})" for f in sols if f.suffix == ".go")
668-
# files.extend(f"[Ruby](../{year}/{f.relative_to(year_dir)})" for f in sols if f.suffix == ".rb")
669-
# files.extend(f"[JS](../{year}/{f.relative_to(year_dir)})" for f in sols if f.suffix == ".js")
670-
# files.extend(f"[Bash](../{year}/{f.relative_to(year_dir)})" for f in sols if f.suffix == ".sh")
671-
# files.extend(f"[Lua](../{year}/{f.relative_to(year_dir)})" for f in sols if f.suffix == ".lua")
672-
# files.extend(f"[C#](../{year}/{f.relative_to(year_dir)})" for f in sols if f.suffix == ".cs")
665+
files.extend(f"[C]({year}/{f.relative_to(year_dir)})" for f in sols if f.suffix == ".c")
666+
files.extend(f"[C++]({year}/{f.relative_to(year_dir)})" for f in sols if f.suffix == ".cpp")
667+
files.extend(f"[Go]({year}/{f.relative_to(year_dir)})" for f in sols if f.suffix == ".go")
668+
# files.extend(f"[Ruby]({year}/{f.relative_to(year_dir)})" for f in sols if f.suffix == ".rb")
669+
# files.extend(f"[JS]({year}/{f.relative_to(year_dir)})" for f in sols if f.suffix == ".js")
670+
# files.extend(f"[Bash]({year}/{f.relative_to(year_dir)})" for f in sols if f.suffix == ".sh")
671+
# files.extend(f"[Lua]({year}/{f.relative_to(year_dir)})" for f in sols if f.suffix == ".lua")
672+
# files.extend(f"[C#]({year}/{f.relative_to(year_dir)})" for f in sols if f.suffix == ".cs")
673673

674674
files = " ".join(files)
675675

0 commit comments

Comments
 (0)