Overview
| Comment: | reworked operators insertion logic |
|---|---|
| Downloads: | Tarball | ZIP archive |
| Timelines: | family | ancestors | descendants | both | trunk |
| Files: | files | file ages | folders |
| SHA3-256: |
9237d89a9aa66ba1e499075f64d60789 |
| User & Date: | nnz on 2024-12-07 13:50:22.019 |
| Other Links: | manifest | tags |
Context
|
2024-12-07
| ||
| 13:52 | removed dead code check-in: 278fc62fcc user: nnz tags: trunk | |
| 13:50 | reworked operators insertion logic check-in: 9237d89a9a user: nnz tags: trunk | |
| 10:39 | 202407 2nd star check-in: 5a3d05d86e user: nnz tags: trunk | |
Changes
Modified aoc2024.c
from [d3378fca96]
to [98d4a70597].
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <ctype.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "aocdailies.h"
#include "aocutils.h"
/* === aoc202407 =======================================================
Part one looks easy
===================================================================== */
static unsigned long long concat(unsigned long long a, unsigned long long b) {
unsigned long long r = a, t = b;
while (b) {
r *= 10;
b /= 10;
}
return r + t;
}
| > | > | > > > | < < | < < < < < < | | | | | | > > > > > > > > > | 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 |
#include <ctype.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "aocdailies.h"
#include "aocutils.h"
/* === aoc202407 =======================================================
Part one looks easy
Part two also easy
===================================================================== */
static unsigned long long concat(unsigned long long a, unsigned long long b) {
unsigned long long r = a, t = b;
while (b) {
r *= 10;
b /= 10;
}
return r + t;
}
static int operatorsrequired(unsigned long long v, unsigned long long *a, size_t n, int minop) {
// assumes no unsigned long long overflowing
if (n == 1) {
if (v == *a) return minop;
return 0;
}
unsigned long long tmp = a[1];
a[1] = a[0] + tmp;
int p = operatorsrequired(v, a+1, n-1, (minop > 2)?3:2);
a[1] = tmp;
if (p) return p;
a[1] = a[0] * tmp;
p = operatorsrequired(v, a+1, n-1, (minop > 2)?3:2);
a[1] = tmp;
if (p) return p;
a[1] = concat(a[0], tmp);
p = operatorsrequired(v, a+1, n-1, 3);
a[1] = tmp;
return p;
}
void aoc202407(char *data, size_t len) {
(void)len; // unused argument
unsigned long long calibrationtotal = 0, calibration3total = 0;
char *line = strtok(data, "\n");
unsigned long long number[50], nnumber;
while (line) {
char *err;
unsigned long long testvalue = strtoull(line, &err, 10);
err += 1; // skip ':'
nnumber = 0;
while (*err == ' ') {
number[nnumber++] = strtoul(err, &err, 10);
}
switch (operatorsrequired(testvalue, number, nnumber, 0)) {
default: break;
case 2: calibrationtotal += testvalue;
__attribute__((fallthrough));
case 3: calibration3total += testvalue;
break;
}
#if 0
if (canbetrue(testvalue, number, nnumber)) {
calibrationtotal += testvalue;
}
if (canbetrue3(testvalue, number, nnumber)) {
calibration3total += testvalue;
}
#endif
line = strtok(NULL, "\n");
}
printf("The calibration total is {%llu}.\n", calibrationtotal);
printf("The calibration total with three operators is {%llu}.\n", calibration3total);
}
/* === aoc202406 =======================================================
|
| ︙ | ︙ | |||
105 106 107 108 109 110 111 |
data[linearize2d(size, (unsigned)row_pos, (unsigned)col_pos)] = 'X'; // initial place visited
while (validpos(size, row_pos + deltarow, col_pos + deltacol)) {
switch (data[linearize2d(size, (unsigned)(row_pos+deltarow), (unsigned)(col_pos+deltacol))]) {
default: exit(EXIT_FAILURE); // does not happen
case '#': rightturn(&deltarow, &deltacol); break;
case '.': visited++;
data[linearize2d(size, (unsigned)(row_pos+deltarow), (unsigned)(col_pos+deltacol))] = 'X';
| | | 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
data[linearize2d(size, (unsigned)row_pos, (unsigned)col_pos)] = 'X'; // initial place visited
while (validpos(size, row_pos + deltarow, col_pos + deltacol)) {
switch (data[linearize2d(size, (unsigned)(row_pos+deltarow), (unsigned)(col_pos+deltacol))]) {
default: exit(EXIT_FAILURE); // does not happen
case '#': rightturn(&deltarow, &deltacol); break;
case '.': visited++;
data[linearize2d(size, (unsigned)(row_pos+deltarow), (unsigned)(col_pos+deltacol))] = 'X';
__attribute__((fallthrough));
case 'X': row_pos += deltarow;
col_pos += deltacol;
break;
}
}
printf("The guard visits %u positions before leaving the area.\n", visited);
// Part Two
|
| ︙ | ︙ | |||
132 133 134 135 136 137 138 |
switch (data[linearize2d(size, (unsigned)(row_pos+deltarow), (unsigned)(col_pos+deltacol))]) {
default: puts("stepped on default"); exit(EXIT_FAILURE);
case '#': if (data[linearize2d(size, (unsigned)(row_pos), (unsigned)(col_pos))] == '4') looping = true;
rightturn(&deltarow, &deltacol);
break;
case '4': looping = true; break;
case '.': data[linearize2d(size, (unsigned)(row_pos+deltarow), (unsigned)(col_pos+deltacol))] = '0';
| | | 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
switch (data[linearize2d(size, (unsigned)(row_pos+deltarow), (unsigned)(col_pos+deltacol))]) {
default: puts("stepped on default"); exit(EXIT_FAILURE);
case '#': if (data[linearize2d(size, (unsigned)(row_pos), (unsigned)(col_pos))] == '4') looping = true;
rightturn(&deltarow, &deltacol);
break;
case '4': looping = true; break;
case '.': data[linearize2d(size, (unsigned)(row_pos+deltarow), (unsigned)(col_pos+deltacol))] = '0';
__attribute__((fallthrough));
case '1':
case '2':
case '3': data[linearize2d(size, (unsigned)(row_pos+deltarow), (unsigned)(col_pos+deltacol))]++;
row_pos += deltarow;
col_pos += deltacol;
break;
}
|
| ︙ | ︙ |