C: 2022 day 2

This commit is contained in:
2022-12-07 15:37:00 +01:00
parent b8f6763a3b
commit 01cdce6608
4 changed files with 89 additions and 3 deletions

View File

@@ -19,3 +19,25 @@ aoc.bash: res=198551
aoc-c: res=198551 aoc-c: res=198551
time: 0:00.00 real, 0.00 user, 0.00 sys time: 0:00.00 real, 0.00 user, 0.00 sys
context-switch: 0+1, page-faults: 0+87 context-switch: 0+1, page-faults: 0+87
=========================================
================= day02 =================
=========================================
+++++++++++++++++ part 1
aoc.bash: res=11841
time: 0:00.05 real, 0.05 user, 0.00 sys
context-switch: 7+1, page-faults: 0+273
aoc-c: res=11841
time: 0:00.00 real, 0.00 user, 0.00 sys
context-switch: 0+1, page-faults: 0+87
+++++++++++++++++ part 2
aoc.bash: res=13022
time: 0:00.05 real, 0.03 user, 0.01 sys
context-switch: 9+1, page-faults: 0+272
aoc-c: res=13022
time: 0:00.00 real, 0.00 user, 0.00 sys
context-switch: 0+1, page-faults: 0+87

View File

@@ -71,11 +71,11 @@ assembly: aoc-c.s
part1: aoc-c part1: aoc-c
@$(TIME) aoc.bash -p 1 < $(INPUT) 2>&1 @$(TIME) aoc.bash -p 1 < $(INPUT) 2>&1
@#$(TIME) aoc-c -p 1 < $(INPUT) @$(TIME) aoc-c -p 1 < $(INPUT)
part2: aoc-c part2: aoc-c
@$(TIME) aoc.bash -p 2 < $(INPUT) 2>&1 @$(TIME) aoc.bash -p 2 < $(INPUT) 2>&1
@#$(TIME) aoc-c -p 2 < $(INPUT) @$(TIME) aoc-c -p 2 < $(INPUT)
ccls: $(CCLSFILE) ccls: $(CCLSFILE)

64
2022/day02/aoc-c.c Normal file
View File

@@ -0,0 +1,64 @@
/* aoc-c.c: Advent of Code 2022, day 2
*
* 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 <unistd.h>
#include "plist.h"
#include "debug.h"
#include "pool.h"
#include "aoc.h"
/* we will use the following convention, for input line "a x":
* position in array=(a-'A' * 3) + x - 'X'
*/
enum {
A = 0, B, C,
X = 0, Y, Z
};
#define pos(x, y) ((x) * 3 + (y))
int outcome[2][9] = { /* shape is known */
{
[pos(A, X)] = 3 + 1, [pos(A, Y)] = 6 + 2, [pos(A, Z)] = 0 + 3,
[pos(B, X)] = 0 + 1, [pos(B, Y)] = 3 + 2, [pos(B, Z)] = 6 + 3,
[pos(C, X)] = 6 + 1, [pos(C, Y)] = 0 + 2, [pos(C, Z)] = 3 + 3
},
{ /* result is known */
[pos(A, X)] = 0 + 3, [pos(A, Y)] = 3 + 1, [pos(A, Z)] = 6 + 2,
[pos(B, X)] = 0 + 1, [pos(B, Y)] = 3 + 2, [pos(B, Z)] = 6 + 3,
[pos(C, X)] = 0 + 2, [pos(C, Y)] = 3 + 3, [pos(C, Z)] = 6 + 1
}
};
static int *parse(int *res)
{
size_t alloc = 0;
char *buf = NULL;
ssize_t buflen;
while ((buflen = getline(&buf, &alloc, stdin)) > 0) {
for (int i = 0; i < 2; ++i)
res[i] += outcome[i][pos(buf[0] - 'A', buf[2] - 'X')];
}
free(buf);
return res;
}
int main(int ac, char **av)
{
int res[2] = { 0, 0 };
printf("%s: res=%d\n", *av, parse(res)[parseargs(ac, av) - 1]);
exit(0);
}

View File

@@ -1,6 +1,6 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# #
# aoc.bash: Advent of Code 2022, day 1 # aoc.bash: Advent of Code 2022, day 2
# #
# Copyright (C) 2022 Bruno Raoult ("br") # Copyright (C) 2022 Bruno Raoult ("br")
# Licensed under the GNU General Public License v3.0 or later. # Licensed under the GNU General Public License v3.0 or later.