diff --git a/2022/RESULTS.txt b/2022/RESULTS.txt index 903e97d..3ccae53 100644 --- a/2022/RESULTS.txt +++ b/2022/RESULTS.txt @@ -19,3 +19,25 @@ aoc.bash: res=198551 aoc-c: res=198551 time: 0:00.00 real, 0.00 user, 0.00 sys 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 diff --git a/2022/day02/Makefile b/2022/day02/Makefile index 43045c1..e0add74 100644 --- a/2022/day02/Makefile +++ b/2022/day02/Makefile @@ -71,11 +71,11 @@ assembly: aoc-c.s part1: aoc-c @$(TIME) aoc.bash -p 1 < $(INPUT) 2>&1 - @#$(TIME) aoc-c -p 1 < $(INPUT) + @$(TIME) aoc-c -p 1 < $(INPUT) part2: aoc-c @$(TIME) aoc.bash -p 2 < $(INPUT) 2>&1 - @#$(TIME) aoc-c -p 2 < $(INPUT) + @$(TIME) aoc-c -p 2 < $(INPUT) ccls: $(CCLSFILE) diff --git a/2022/day02/aoc-c.c b/2022/day02/aoc-c.c new file mode 100644 index 0000000..4b348fe --- /dev/null +++ b/2022/day02/aoc-c.c @@ -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 . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include +#include +#include + +#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); +} diff --git a/2022/day02/aoc.bash b/2022/day02/aoc.bash index 71c8fda..476d08f 100755 --- a/2022/day02/aoc.bash +++ b/2022/day02/aoc.bash @@ -1,6 +1,6 @@ #!/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") # Licensed under the GNU General Public License v3.0 or later.