Advent of Code

Check-in [02e370e22c]
Login

Check-in [02e370e22c]

Overview
Comment:201505 reworked my solution
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 02e370e22c626326f8b49035362483f90128bbb516bccebb82d598b4fad8de3b
User & Date: nnz on 2024-12-15 13:12:52
Other Links: manifest | tags
Context
2024-12-15
15:51
201506 1st star check-in: e9f61cfc4a user: nnz tags: trunk
13:12
201505 reworked my solution check-in: 02e370e22c user: nnz tags: trunk
12:24
201505 2nd star check-in: c2af59b62e user: nnz tags: trunk
Changes

Modified aoc2015.c from [1f1b4d89f8] to [0dc60fd2e9].

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
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "aocdailies.h"
#include "aocutils.h"

void aoc201505(char *data, size_t len) {
    (void)len; // unused argument
    unsigned lines = 0, nice = 0, nice2 = 0;
    char *line = strtok(data, "\n"), *curr;
    while (line) {
        lines++; // used in Part Two
        if (!strstr(line, "ab")) {
            if (!strstr(line, "cd")) {
                if (!strstr(line, "pq")) {
                    if (!strstr(line, "xy")) {
                        for (curr = line; curr[1]; curr++) {
                            if (curr[0] == curr[1]) break;
                        }


                        if (curr[0] == curr[1]) {
                            unsigned vowels = 0;
                            for (curr = line; *curr; curr++) {
                                if (strchr("aeiou", *curr)) vowels++;
                                if (vowels == 3) break;



                            }
                            nice += (vowels == 3);



                        }


                    }

                }
            }
        }
        line = strtok(NULL, "\n");
    }
    printf("There are %u nice strings.\n", nice);

    line = data;
    bool pair, hugged;
    while (lines--) {
        pair = hugged = false;
        for (curr = line; curr[3]; curr++) {
            char twostr[3] = {curr[0], curr[1], 0};
            if (strstr(curr+2, twostr)) pair = true;
            if (curr[0] == curr[2]) hugged = true;
            if (pair && hugged) break;
        }
        if (pair) {
            if (hugged) nice2++;
            else if (curr[0] == curr[2]) nice2++;
            else if (curr[1] == curr[3]) nice2++;
        }
        line += 1 + strlen(line); // go to next line, skip '\0' left from strtok
    }
    printf("There are %u nice strings under the new rules.\n", nice2);
}

void aoc201504(char *data, size_t len) {
    data[--len] = 0; // remove newline, adjust `len`
    char longkey[128];
    unsigned extra = 1;









|


|
|
|
<
<
|
<
|
>
>
|
<
|
|
|
>
>
>
|
|
>
>
>
|
>
>
|
>
|
<
<



<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







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
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "aocdailies.h"
#include "aocutils.h"

void aoc201505(char *data, size_t len) {
    (void)len; // unused argument
    unsigned nice = 0, nice2 = 0;
    char *line = strtok(data, "\n"), *curr;
    while (line) {
        unsigned vowels = 0;
        bool twice = false;
        bool hasab, hascd, haspq, hasxy;


        bool pair = false, hugged = false; // Part Two


        curr = line;
        if (strchr("aeiou", *curr)) vowels++;
        if (curr[0] == curr[1]) twice = true;

        for (curr = line + 1; *curr; curr++) {
            if (strchr("aeiou", *curr)) vowels++;
            if (curr[0] == curr[1]) twice = true;
            if (curr[-1] == curr[1]) hugged = true;
            char pp[3] = {curr[-1], curr[0], 0};
            if (strstr(curr + 1, pp)) pair = true;
        }
        hasab = (strstr(line, "ab") != NULL);
        hascd = (strstr(line, "cd") != NULL);
        haspq = (strstr(line, "pq") != NULL);
        hasxy = (strstr(line, "xy") != NULL);

        if ((vowels >= 3) && twice && !hasab && !hascd && !haspq && !hasxy) {
            nice++;
        }
        if (pair && hugged) nice2++;



        line = strtok(NULL, "\n");
    }
    printf("There are %u nice strings.\n", nice);


















    printf("There are %u nice strings under the new rules.\n", nice2);
}

void aoc201504(char *data, size_t len) {
    data[--len] = 0; // remove newline, adjust `len`
    char longkey[128];
    unsigned extra = 1;