-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc15.c
121 lines (111 loc) · 3.13 KB
/
aoc15.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct{
int value;
char key[64];
struct HashEntry* next;
}HashEntry;
HashEntry entries[ 10000 ];
typedef struct {
HashEntry* slots[256];
}HashMap;
int get_hash( char* word, int word_len ) {
int value = 0;
for( int idx=0; idx<word_len; idx++) {
value += word[ idx ];
value *= 17;
value = value &0x00ff;
}
return value;
}
void hashmap_init(HashMap *hash_map ) {
for( int idx =0; idx<256;idx ++ ) hash_map->slots[idx ] = NULL;
}
void hashmap_set(HashMap *hash_map, HashEntry *entry ) {
int index = get_hash(entry->key, strlen(entry->key));
HashEntry **next_ptr = &hash_map->slots[index];
for (;;) {
if (*next_ptr == NULL) {
*next_ptr = entry;
entry->next = NULL;
return;
} else if (strcmp((*next_ptr)->key, entry->key) == 0) {
entry->next = (*next_ptr)->next;
*next_ptr = entry;
return;
}
next_ptr = &(*next_ptr)->next;
}
}
void hashmap_delete( HashMap *hash_map, char* key ) {
int index = get_hash( key, strlen( key ) );
HashEntry **next_ptr = &hash_map->slots[ index ];
for( ;; ) {
if( *next_ptr == NULL ) {
return;
} else if( strcmp( (*next_ptr)->key, key ) == 0 ) {
(*next_ptr) = (*next_ptr)->next;
return;
}
next_ptr = &(*next_ptr)->next;
}
}
int main(void) {
FILE *fp = fopen("input15.txt", "r");
char word[256];
int pos = 0;
int hash_sum = 0;
for (;;) {
int code = fgetc(fp);
if (code == EOF || code == ',') {
hash_sum += get_hash( word, pos);
pos = 0;
if( code == EOF) {
break;
}
} else {
word[pos++] = code;
}
}
fclose( fp );
printf( "(Part 1 ) hash sum = %d", hash_sum); //
HashMap hash_map;
hashmap_init( &hash_map);
int used_entries = 0;
fp = fopen("input15.txt", "r");
for (;;) {
int code = fgetc(fp);
if (code == EOF || code == ',') {
word[ pos ] = 0;
char *instruction = strchr( word, '=');
if( instruction ) {
*instruction = 0;
strcpy( entries[ used_entries ].key, word );
entries[ used_entries ].value = strtol( instruction+1, NULL, 10);
hashmap_set( &hash_map, &entries[ used_entries++ ]);
} else {
char *instruction = strchr( word, '-');
*instruction = 0;
hashmap_delete( &hash_map, word);
}
pos = 0;
if( code == EOF) {
break;
}
} else {
word[pos++] = code;
}
}
int total = 0;
for( int box=0; box<256; box ++ ) {
int slot_no = 1;
HashEntry* curr = hash_map.slots[ box ];
while( curr != NULL ) {
total += (box+1)*slot_no*curr->value;
curr = curr->next;
slot_no +=1;
}
}
printf( "(Part 2) Focusing power = %d", total); //
}