-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday3.c
89 lines (75 loc) · 2.37 KB
/
day3.c
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
82
83
84
85
86
87
88
89
// ----------------------------------------------------------------------------
// Advent of Code 2022 - Day 3
// Dale Whinham
//
// $ gcc day3.c -Wall -Wextra -Wpedantic -Werror -o day3
// $ ./day3
// ----------------------------------------------------------------------------
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INPUT_FILE "input/day3.txt"
#define MAX_ITEMS (26 * 2) // a-z, A-Z
#define ITEM_BITPOS(CHAR) (CHAR >= 'a' ? CHAR - 'a' : CHAR - 'A' + 26)
#define ITEM_BITMASK(CHAR) (1L << ITEM_BITPOS(CHAR))
#define ELVES_PER_GROUP 3
#define PART_TWO
int main()
{
FILE* f = fopen(INPUT_FILE, "r");
if (f == NULL)
return EXIT_FAILURE;
uint32_t priority_sum = 0;
#ifdef PART_TWO
uint64_t group_items = -1;
uint32_t group_bag = 0;
#endif
char buffer[512];
while (fgets(buffer, sizeof(buffer), f))
{
// Subtract 1 from string length to ignore '\n' character
size_t len = strlen(buffer) - 1;
#ifdef PART_TWO
uint64_t this_bag = 0;
for (size_t i = 0; i < len; ++i)
this_bag |= ITEM_BITMASK(buffer[i]);
// Bitwise AND to find common items
group_items &= this_bag;
// Done 3 bags
if (++group_bag == ELVES_PER_GROUP)
{
// Loop over each bit and compute priority sum
for (uint8_t i = 0; i < MAX_ITEMS; ++i)
{
if ((group_items >> i) & 1)
priority_sum += i + 1;
}
// Next group
group_bag = 0;
group_items = -1;
}
#else
uint64_t compartments[2] = { 0 };
// For each character, convert to bit position, separate compartments
for (size_t i = 0; i < len; ++i)
{
if (i < len / 2)
compartments[0] |= ITEM_BITMASK(buffer[i]);
else
compartments[1] |= ITEM_BITMASK(buffer[i]);
}
// Bitwise AND to find common items common to both compartments
uint64_t both_compartments = compartments[0] & compartments[1];
// Loop over each bit and compute priority sum
for (uint8_t i = 0; i < MAX_ITEMS; ++i)
{
if ((both_compartments >> i) & 1)
priority_sum += i + 1;
}
#endif
}
printf("Priority sum: %d\n", priority_sum);
fclose(f);
return EXIT_SUCCESS;
}