Skip to content

Commit 1b3cc8d

Browse files
committed
Finished Day 8 part 1
1 parent 30336e0 commit 1b3cc8d

File tree

10 files changed

+431
-30
lines changed

10 files changed

+431
-30
lines changed

include/advent_of_code/day_7.hxx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
#include "spdlog/spdlog.h"
1313

1414
namespace AdventOfCode24::Day7 {
15-
long check_calibration_line(const std::string& line, bool include_concatenator, const char& delimiter=':');
16-
std::vector<long> process_file(const std::filesystem::path& input_file, bool include_concatenator);
15+
int64_t check_calibration_line(
16+
const std::string& line,
17+
bool include_concatenator,
18+
const char& delimiter = ':'
19+
);
20+
std::vector<int64_t> process_file(const std::filesystem::path& input_file, bool include_concatenator);
1721
};

include/advent_of_code/day_8.hxx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#pragma once
2+
3+
#include <fstream>
4+
#include <unordered_map>
5+
#include <unordered_set>
6+
#include <filesystem>
7+
#include <algorithm>
8+
#include <sstream>
9+
#include <string>
10+
#include <functional>
11+
#include <iostream>
12+
13+
#include "spdlog/spdlog.h"
14+
15+
typedef std::pair<int,int> coord;
16+
17+
// Need to create custom hashing for the std::pair
18+
template<>
19+
struct std::hash<coord> {
20+
std::size_t operator()(const coord& c) const {
21+
return std::hash<int>()(c.first) ^ (std::hash<int>()(c.second) << 1);
22+
}
23+
};
24+
25+
namespace AdventOfCode24::Day8 {
26+
typedef std::unordered_map<char, std::unordered_set<coord>> area_map;
27+
28+
class Map {
29+
public:
30+
area_map antennae;
31+
std::unordered_set<coord> antinodes;
32+
coord size;
33+
Map(){};
34+
Map(const Map& map) : antennae(map.antennae), antinodes(map.antinodes), size(map.size) {}
35+
};
36+
37+
Map get_communication_network(const std::filesystem::path& input_file);
38+
void calculate_antinode_positions(Map& communication_network, bool resonant_harmonics);
39+
void print_map(const Map& communication_network);
40+
};

solutions/data/day_8.txt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
...............e.........................i........
2+
...............................1.......i.0........
3+
..............................s.0......d..........
4+
........................i....B.I............d.....
5+
.............................s....................
6+
................J.................................
7+
.....................L.....0i.......4...d.........
8+
.N...e...........................s..R.....4.....I.
9+
........e.........v................1......R....I..
10+
.............G..............0.....1...............
11+
..2...N.............B......................4...R..
12+
..............2...................N..........4s...
13+
..p...................................1..b..I.....
14+
..................p...........B........b...R......
15+
....................................b.............
16+
........W.......C.....w...........................
17+
............7....u.............B..................
18+
...W.................u......................bw....
19+
.......p.2...........v......................9.....
20+
.E.....C....u................................9....
21+
E....Y................u.D........9...........J....
22+
.......2..........................................
23+
............................J.................c...
24+
.............7...K..D..............J..............
25+
.....C.Wq........t.................T..............
26+
............Yt......v.............................
27+
..W......................3...............w........
28+
..7.....j................T...D.....n......8.....c.
29+
.........E...............nTD......................
30+
...r....E..........Y............n.......P........c
31+
......K........G......L...........................
32+
......................G.....L....v................
33+
..............G...t......q.............l.8........
34+
......................q............l..............
35+
...6........r.............................w..c....
36+
..6.........3.......Qk........T...................
37+
......Y...............j.................n.........
38+
..K.....S.....r......j.....U......9.l......8......
39+
........................U......................P..
40+
.....................q............................
41+
.......K......5..N.....j.7.Q......................
42+
...................p..k...U..........L.Q..........
43+
.r......3...S.......k........y....8U....Q.......P.
44+
.......S....g..3..................................
45+
.....S..........gk................................
46+
................5...................yP............
47+
.......................g......yV..l...............
48+
.........6.5...............V......................
49+
..................6..5..V.........................
50+
.............g.......................y..........V.

solutions/day_7.cxx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ int main(int argc, char** argv) {
2222
throw std::runtime_error("File " + input_file.string() + " does not exist!");
2323
}
2424

25-
const std::vector<long> calibration_results{process_file(input_file, false)};
26-
const std::vector<long> calibration_results_w_concat{process_file(input_file, true)};
27-
const long total = std::accumulate(calibration_results.begin(), calibration_results.end(), 0L);
28-
const long total_w_cat = std::accumulate(calibration_results_w_concat.begin(), calibration_results_w_concat.end(), 0L);
25+
const std::vector<int64_t> calibration_results{process_file(input_file, false)};
26+
const std::vector<int64_t> calibration_results_w_concat{process_file(input_file, true)};
27+
const int64_t total = std::accumulate(calibration_results.begin(), calibration_results.end(), 0L);
28+
const int64_t total_w_cat = std::accumulate(calibration_results_w_concat.begin(), calibration_results_w_concat.end(), 0L);
2929

3030
spdlog::info("For file '" + input_file.string() + "' the total of successful calibration lines is " + std::to_string(total));
3131
spdlog::info("With concatenation operator '||' included, the total becomes: " + std::to_string(total_w_cat));

solutions/day_8.cxx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <filesystem>
2+
#include <iostream>
3+
#include <numeric>
4+
#include <string>
5+
#include <set>
6+
#include "spdlog/spdlog.h"
7+
8+
#include "advent_of_code/day_8.hxx"
9+
10+
using namespace AdventOfCode24::Day8;
11+
12+
int main(int argc, char** argv) {
13+
if(argc != 2) {
14+
spdlog::error("Expected input data file!");
15+
return 1;
16+
}
17+
18+
spdlog::set_level(spdlog::level::info);
19+
20+
const std::filesystem::path input_file{argv[1]};
21+
if(!std::filesystem::exists(input_file)) {
22+
throw std::runtime_error("File " + input_file.string() + " does not exist!");
23+
}
24+
25+
Map communication_network{get_communication_network(input_file)};
26+
Map communication_network_w_resonance{communication_network};
27+
calculate_antinode_positions(communication_network, false);
28+
calculate_antinode_positions(communication_network_w_resonance, true);
29+
30+
const size_t total_antinodes{communication_network.antinodes.size()};
31+
print_map(communication_network);
32+
spdlog::info("For file '" + input_file.string() + "' the total number of antinodes is " + std::to_string(total_antinodes));
33+
34+
const size_t total_antinodes_w_resonance{communication_network_w_resonance.antinodes.size()};
35+
print_map(communication_network_w_resonance);
36+
spdlog::info("Taking into account resonant harmonics this is " + std::to_string(total_antinodes_w_resonance));
37+
}

src/day_7.cxx

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <string>
77

88
namespace AdventOfCode24::Day7 {
9-
long check_calibration_line(const std::string& line, bool include_concatenator, const char& delimiter) {
9+
int64_t check_calibration_line(const std::string& line, bool include_concatenator, const char& delimiter) {
1010
auto divider_find = std::find(line.begin(), line.end(), delimiter);
1111

1212
if(divider_find == line.end()) {
@@ -20,47 +20,47 @@ namespace AdventOfCode24::Day7 {
2020
throw std::runtime_error("Target string is empty for line: " + line);
2121
}
2222

23-
long target{-1};
23+
int64_t target{-1};
2424

2525
try {
2626
target = std::stol(target_str);
2727
} catch(std::exception& e) {
28-
throw std::runtime_error("Could not convert '" + target_str + "' to long integer.");
28+
throw std::runtime_error("Could not convert '" + target_str + "' to int64_t integer.");
2929
}
3030

3131
if(target < 0) {
32-
throw std::runtime_error(target_str + " exceeds limit of long integer.");
32+
throw std::runtime_error(target_str + " exceeds limit of int64_t integer.");
3333
}
3434

3535
std::istringstream iss;
3636
iss.str(operands);
37-
long number{-1};
37+
int64_t number{-1};
3838
int n_numbers{0};
39-
std::vector<long> operations{0};
39+
std::vector<int64_t> operations{0};
4040

4141
while(iss >> number) {
42-
std::vector<long> results;
42+
std::vector<int64_t> results;
4343

4444
n_numbers++;
4545

46-
for(const long& n : operations) {
47-
const long multiply{((n==0) ? 1 : n) * number};
48-
const long add{n + number};
46+
for(const int64_t& n : operations) {
47+
const int64_t multiply{((n==0) ? 1 : n) * number};
48+
const int64_t add{n + number};
4949

5050
if(multiply < 0) {
51-
throw std::runtime_error(std::to_string(multiply) + " exceeds limit of long integer.");
51+
throw std::runtime_error(std::to_string(multiply) + " exceeds limit of int64_t integer.");
5252
}
5353

5454
if(add < 0) {
55-
throw std::runtime_error(std::to_string(add) + " exceeds limit of long integer.");
55+
throw std::runtime_error(std::to_string(add) + " exceeds limit of int64_t integer.");
5656
}
5757

5858
if(multiply <= target) results.push_back(multiply);
5959
if(add <= target) results.push_back(add);
6060

6161
if(include_concatenator) {
6262
const std::string concatenate_str{std::to_string(n) + std::to_string(number)};
63-
const long concatenate{std::stol(concatenate_str)};
63+
const int64_t concatenate{std::stol(concatenate_str)};
6464
if(concatenate <= target) results.push_back(concatenate);
6565
}
6666
}
@@ -85,29 +85,28 @@ namespace AdventOfCode24::Day7 {
8585
if(max_loc == operations.end()) {
8686
spdlog::warn("Line failed, operation result below target value after processing.");
8787
} else {
88-
const long max_reached{*max_loc};
88+
const int64_t max_reached{*max_loc};
8989
spdlog::warn("Line failed, operation result below target value after processing " + std::to_string(n_numbers) + ": " + std::to_string(max_reached) + " < " + std::to_string(target));
9090
}
9191
return 0;
9292
}
9393

94-
std::vector<long> process_file(const std::filesystem::path& input_file, bool include_concatenator) {
94+
std::vector<int64_t> process_file(const std::filesystem::path& input_file, bool include_concatenator) {
9595
std::ifstream read_in(input_file, std::ios::in);
9696
std::string line;
97-
std::vector<long> calibration_results;
97+
std::vector<int64_t> calibration_results;
9898
int n_lines{0};
9999

100100
while (std::getline(read_in, line)) {
101101
if(line.empty()) continue;
102102

103-
const long calibration{check_calibration_line(line, include_concatenator)};
103+
const int64_t calibration{check_calibration_line(line, include_concatenator)};
104104

105105
if(calibration < 0) {
106106
throw std::runtime_error("Tried to append negative value " + std::to_string(calibration) + " to total!");
107107
}
108108

109109
calibration_results.push_back(calibration);
110-
std::cout << calibration << std::endl;
111110

112111
n_lines++;
113112
}

0 commit comments

Comments
 (0)