A little refactoring for day 4.
This commit is contained in:
parent
83944d8d4c
commit
b19aa5edca
@ -3,7 +3,6 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdbool.h>
|
|
||||||
|
|
||||||
|
|
||||||
uint64_t parse_number(char *end);
|
uint64_t parse_number(char *end);
|
||||||
@ -43,12 +42,14 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
for (char *n_pos = pipe_pos + 4;; n_pos += 3) {
|
for (char *n_pos = pipe_pos + 4;; n_pos += 3) {
|
||||||
uint8_t n = parse_number(n_pos-1);
|
uint8_t n = parse_number(n_pos-1);
|
||||||
|
|
||||||
for (size_t i = 0; i < winning_numbers_len; ++i) {
|
for (size_t i = 0; i < winning_numbers_len; ++i) {
|
||||||
if (n != winning_numbers[i])
|
if (n != winning_numbers[i])
|
||||||
continue;
|
continue;
|
||||||
points = (points == 0) ? 1 : points * 2;
|
points = (points == 0) ? 1 : points * 2;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (*n_pos == '\n')
|
if (*n_pos == '\n')
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,6 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdbool.h>
|
|
||||||
|
|
||||||
|
|
||||||
#define STACK_SZ UINT8_MAX+1
|
#define STACK_SZ UINT8_MAX+1
|
||||||
@ -42,24 +41,25 @@ int main(int argc, char **argv) {
|
|||||||
winning_numbers = (uint8_t *) calloc(winning_numbers_len, sizeof(uint8_t));
|
winning_numbers = (uint8_t *) calloc(winning_numbers_len, sizeof(uint8_t));
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t i = 0, wl_pos = (size_t)colon_pos+4; i < winning_numbers_len; ++i, wl_pos += 3) {
|
for (size_t i = 0, wl_pos = (size_t)colon_pos+4; i < winning_numbers_len; ++i, wl_pos += 3)
|
||||||
winning_numbers[i] = parse_number((char *)(wl_pos-1));
|
winning_numbers[i] = parse_number((char *)(wl_pos-1));
|
||||||
}
|
|
||||||
|
|
||||||
uint64_t card_copies = 1 + stack[stack_pos++];
|
uint64_t card_copies = 1 + stack[stack_pos++];
|
||||||
|
|
||||||
total_copies += card_copies;
|
total_copies += card_copies;
|
||||||
|
|
||||||
size_t old_sp = stack_pos;
|
stackpointer_t old_sp = stack_pos;
|
||||||
|
|
||||||
for (char *n_pos = pipe_pos + 4;; n_pos += 3) {
|
for (char *n_pos = pipe_pos + 4;; n_pos += 3) {
|
||||||
uint8_t n = parse_number(n_pos-1);
|
uint8_t n = parse_number(n_pos-1);
|
||||||
|
|
||||||
for (size_t i = 0; i < winning_numbers_len; ++i) {
|
for (size_t i = 0; i < winning_numbers_len; ++i) {
|
||||||
if (n != winning_numbers[i])
|
if (n != winning_numbers[i])
|
||||||
continue;
|
continue;
|
||||||
stack[stack_pos++] += card_copies;
|
stack[stack_pos++] += card_copies;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (*n_pos == '\n')
|
if (*n_pos == '\n')
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user