aboutsummaryrefslogtreecommitdiff
path: root/2024/src/day04.cpp
blob: b82e4d66e196b21dc5b8b518a29d91bd99ebde5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <fstream>
#include <iostream>
#include <map>
#include <string>
#include <tuple>
using namespace std;

map<tuple<size_t, size_t>, char>
read_file(string_view filename)
{
	fstream                          input{ filename };
	map<tuple<size_t, size_t>, char> data;

	size_t yPos = 0;
	for ( string line; getline(input, line); ) {
		for ( size_t xPos = 0; xPos != line.size(); ++xPos ) {
			data[{ xPos, yPos }] = line[xPos];
		}
		++yPos;
	}

	return data;
}

void
part1(map<tuple<size_t, size_t>, char> data, size_t size)
{
	static const tuple<size_t, size_t> dirs[] = {
		{ -1, -1 },
		{ 0, -1 },
		{ 1, -1 },
		{ 1, 0 },
		{ 1, 1 },
		{ 0, 1 },
		{ -1, 1 },
		{ -1, 0 },
	};
	long count = 0;
	for ( size_t y = 0; y != size; ++y ) {
		for ( size_t x = 0; x != size; ++x ) {
			for ( const auto& [dx, dy]: dirs ) {
				if ( data[{ x, y }] == 'X' &&
				     data[{ x + 1 * dx, y + 1 * dy }] == 'M' &&
				     data[{ x + 2 * dx, y + 2 * dy }] == 'A' &&
				     data[{ x + 3 * dx, y + 3 * dy }] == 'S' ) {
					++count;
				}
			}
		}
	}
	cout << count << endl;
}

void
part2(map<tuple<size_t, size_t>, char> data, size_t size)
{
	long count = 0;
	for ( size_t y = 0; y != size; ++y ) {
		for ( size_t x = 0; x != size; ++x ) {
			string str;
			str += data[{ x, y }];
			str += data[{ x - 1, y - 1 }];
			str += data[{ x - 1, y + 1 }];
			str += data[{ x + 1, y - 1 }];
			str += data[{ x + 1, y + 1 }];

			if ( str == "AMMSS" || str == "ASMSM" || str == "ASSMM" || str == "AMSMS" ) {
				++count;
			}
		}
	}
	cout << count << endl;
}

int
main()
{
	auto data = read_file("data/day04.txt");
	part1(data, 140);
	part2(data, 140);
}