2022 day 6: C (parts 1 and 2)
This commit is contained in:
@@ -117,7 +117,15 @@ aoc.bash: res=1658
|
|||||||
time: 0:00.06 real, 0.06 user, 0.00 sys
|
time: 0:00.06 real, 0.06 user, 0.00 sys
|
||||||
context-switch: 1+1, page-faults: 0+266
|
context-switch: 1+1, page-faults: 0+266
|
||||||
|
|
||||||
|
aoc-c: res=1658
|
||||||
|
time: 0:00.00 real, 0.00 user, 0.00 sys
|
||||||
|
context-switch: 0+1, page-faults: 0+87
|
||||||
|
|
||||||
+++++++++++++++++ part 2
|
+++++++++++++++++ part 2
|
||||||
aoc.bash: res=2260
|
aoc.bash: res=2260
|
||||||
time: 0:00.09 real, 0.09 user, 0.00 sys
|
time: 0:00.09 real, 0.09 user, 0.00 sys
|
||||||
context-switch: 1+1, page-faults: 0+265
|
context-switch: 1+1, page-faults: 0+265
|
||||||
|
|
||||||
|
aoc-c: res=2260
|
||||||
|
time: 0:00.00 real, 0.00 user, 0.00 sys
|
||||||
|
context-switch: 0+1, page-faults: 0+87
|
||||||
|
60
2022/day06/aoc-c.c
Normal file
60
2022/day06/aoc-c.c
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
/* aoc-c.c: Advent of Code 2022, day 6
|
||||||
|
*
|
||||||
|
* Copyright (C) 2022 Bruno Raoult ("br")
|
||||||
|
* Licensed under the GNU General Public License v3.0 or later.
|
||||||
|
* Some rights reserved. See COPYING.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along with this
|
||||||
|
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "br.h"
|
||||||
|
#include "aoc.h"
|
||||||
|
|
||||||
|
struct msg {
|
||||||
|
char *data;
|
||||||
|
size_t len;
|
||||||
|
};
|
||||||
|
|
||||||
|
static struct msg *parse(struct msg *msg)
|
||||||
|
{
|
||||||
|
size_t alloc = 0;
|
||||||
|
|
||||||
|
msg->data=NULL;
|
||||||
|
msg->len = getline(&msg->data, &alloc, stdin);
|
||||||
|
msg->data[--msg->len] = 0;
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int solve(struct msg *msg, int marklen)
|
||||||
|
{
|
||||||
|
char *pcur = msg->data, *pnext = pcur;
|
||||||
|
int len = msg->len, lmark = 0;
|
||||||
|
|
||||||
|
for (; pnext < msg->data + len && lmark < marklen; lmark = ++pnext - pcur) {
|
||||||
|
for (int j = 0; j < lmark; ++j) { /* compare with prev marker chars */
|
||||||
|
if (*(pcur+j) == *pnext) { /* check new char with cur marker */
|
||||||
|
pcur += j + 1; /* move marker to after dup char */
|
||||||
|
goto nextchar;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
nextchar: ;
|
||||||
|
}
|
||||||
|
return pnext - msg->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int ac, char **av)
|
||||||
|
{
|
||||||
|
int part = parseargs(ac, av);
|
||||||
|
struct msg msg;
|
||||||
|
|
||||||
|
printf("%s: res=%d\n", *av, solve(parse(&msg), part == 1? 4:14));
|
||||||
|
free(msg.data);
|
||||||
|
exit(0);
|
||||||
|
}
|
@@ -32,7 +32,7 @@ solve() {
|
|||||||
for ((j = 0; j < lcur; ++j)); do # compare with previous ones
|
for ((j = 0; j < lcur; ++j)); do # compare with previous ones
|
||||||
if [[ $next == "${cur:j:1}" ]]; then # duplicate
|
if [[ $next == "${cur:j:1}" ]]; then # duplicate
|
||||||
cur="${cur:j+1}$next" # keep str after dup + new char
|
cur="${cur:j+1}$next" # keep str after dup + new char
|
||||||
(( lcur = lcur - j ))
|
(( lcur -= j ))
|
||||||
continue 2
|
continue 2
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
Reference in New Issue
Block a user