day 10 part 1 (with tons of debug information ;-)
This commit is contained in:
@@ -24,7 +24,7 @@ LDLIB := -l$(LIB)
|
|||||||
export LD_LIBRARY_PATH = $(LIBDIR)
|
export LD_LIBRARY_PATH = $(LIBDIR)
|
||||||
|
|
||||||
CFLAGS += -std=gnu99
|
CFLAGS += -std=gnu99
|
||||||
#CFLAGS += -O2
|
CFLAGS += -O2
|
||||||
CFLAGS += -g
|
CFLAGS += -g
|
||||||
CFLAGS += -Wall
|
CFLAGS += -Wall
|
||||||
CFLAGS += -Wextra
|
CFLAGS += -Wextra
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
-- Day 10: Syntax Scoring ---
|
--- Day 10: Syntax Scoring ---
|
||||||
|
|
||||||
You ask the submarine to determine the best route out of the deep-sea cave, but it only replies:
|
You ask the submarine to determine the best route out of the deep-sea cave, but it only replies:
|
||||||
|
|
||||||
@@ -54,3 +54,49 @@ Did you know that syntax checkers actually have contests to see who can get the
|
|||||||
In the above example, an illegal ) was found twice (2*3 = 6 points), an illegal ] was found once (57 points), an illegal } was found once (1197 points), and an illegal > was found once (25137 points). So, the total syntax error score for this file is 6+57+1197+25137 = 26397 points!
|
In the above example, an illegal ) was found twice (2*3 = 6 points), an illegal ] was found once (57 points), an illegal } was found once (1197 points), and an illegal > was found once (25137 points). So, the total syntax error score for this file is 6+57+1197+25137 = 26397 points!
|
||||||
|
|
||||||
Find the first illegal character in each corrupted line of the navigation subsystem. What is the total syntax error score for those errors?
|
Find the first illegal character in each corrupted line of the navigation subsystem. What is the total syntax error score for those errors?
|
||||||
|
|
||||||
|
Your puzzle answer was 367059.
|
||||||
|
|
||||||
|
The first half of this puzzle is complete! It provides one gold star: *
|
||||||
|
--- Part Two ---
|
||||||
|
|
||||||
|
Now, discard the corrupted lines. The remaining lines are incomplete.
|
||||||
|
|
||||||
|
Incomplete lines don't have any incorrect characters - instead, they're missing some closing characters at the end of the line. To repair the navigation subsystem, you just need to figure out the sequence of closing characters that complete all open chunks in the line.
|
||||||
|
|
||||||
|
You can only use closing characters (), ], }, or >), and you must add them in the correct order so that only legal pairs are formed and all chunks end up closed.
|
||||||
|
|
||||||
|
In the example above, there are five incomplete lines:
|
||||||
|
|
||||||
|
[({(<(())[]>[[{[]{<()<>> - Complete by adding }}]])})].
|
||||||
|
[(()[<>])]({[<{<<[]>>( - Complete by adding )}>]}).
|
||||||
|
(((({<>}<{<{<>}{[]{[]{} - Complete by adding }}>}>)))).
|
||||||
|
{<[[]]>}<{[{[{[]{()[[[] - Complete by adding ]]}}]}]}>.
|
||||||
|
<{([{{}}[<[[[<>{}]]]>[]] - Complete by adding ])}>.
|
||||||
|
|
||||||
|
Did you know that autocomplete tools also have contests? It's true! The score is determined by considering the completion string character-by-character. Start with a total score of 0. Then, for each character, multiply the total score by 5 and then increase the total score by the point value given for the character in the following table:
|
||||||
|
|
||||||
|
): 1 point.
|
||||||
|
]: 2 points.
|
||||||
|
}: 3 points.
|
||||||
|
>: 4 points.
|
||||||
|
|
||||||
|
So, the last completion string above - ])}> - would be scored as follows:
|
||||||
|
|
||||||
|
Start with a total score of 0.
|
||||||
|
Multiply the total score by 5 to get 0, then add the value of ] (2) to get a new total score of 2.
|
||||||
|
Multiply the total score by 5 to get 10, then add the value of ) (1) to get a new total score of 11.
|
||||||
|
Multiply the total score by 5 to get 55, then add the value of } (3) to get a new total score of 58.
|
||||||
|
Multiply the total score by 5 to get 290, then add the value of > (4) to get a new total score of 294.
|
||||||
|
|
||||||
|
The five lines' completion strings have total scores as follows:
|
||||||
|
|
||||||
|
}}]])})] - 288957 total points.
|
||||||
|
)}>]}) - 5566 total points.
|
||||||
|
}}>}>)))) - 1480781 total points.
|
||||||
|
]]}}]}]}> - 995444 total points.
|
||||||
|
])}> - 294 total points.
|
||||||
|
|
||||||
|
Autocomplete tools are an odd bunch: the winner is found by sorting all of the scores and then taking the middle score. (There will always be an odd number of scores to consider.) In this example, the middle score is 288957 because there are the same number of scores smaller and larger than it.
|
||||||
|
|
||||||
|
Find the completion string for each incomplete line, score the completion strings, and sort the scores. What is the middle score?
|
||||||
|
142
2021/day10/aoc-c.c
Normal file
142
2021/day10/aoc-c.c
Normal file
@@ -0,0 +1,142 @@
|
|||||||
|
/* aoc-c: Advent2021 game, day 6 parts 1 & 2
|
||||||
|
*
|
||||||
|
* Copyright (C) 2021 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 <string.h>
|
||||||
|
#include <malloc.h>
|
||||||
|
|
||||||
|
#include "debug.h"
|
||||||
|
#include "bits.h"
|
||||||
|
#include "list.h"
|
||||||
|
#include "pool.h"
|
||||||
|
|
||||||
|
static char stack[1024];
|
||||||
|
static size_t nstack;
|
||||||
|
|
||||||
|
inline static int push(char c)
|
||||||
|
{
|
||||||
|
stack[nstack++] = c;
|
||||||
|
printf("push(%c)\n", c);
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline static int pop()
|
||||||
|
{
|
||||||
|
int c;
|
||||||
|
if (!nstack)
|
||||||
|
return 0;
|
||||||
|
c = stack[--nstack];
|
||||||
|
printf("pop(%c)\n", c);
|
||||||
|
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct match {
|
||||||
|
char open;
|
||||||
|
char close;
|
||||||
|
int value;
|
||||||
|
} syntax[] = {
|
||||||
|
{ '(', ')', 3 },
|
||||||
|
{ '[', ']', 57 },
|
||||||
|
{ '{', '}', 1197 },
|
||||||
|
{ '<', '>', 25137 },
|
||||||
|
};
|
||||||
|
|
||||||
|
inline static int match(char c)
|
||||||
|
{
|
||||||
|
int co;
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
if (!(co = pop()))
|
||||||
|
return 0;
|
||||||
|
for (i = 0; i < sizeof(syntax); ++i)
|
||||||
|
if (syntax[i].close == c && co != syntax[i].open)
|
||||||
|
return syntax[i].value;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static u64 part1()
|
||||||
|
{
|
||||||
|
char *buf = NULL, *p;
|
||||||
|
size_t alloc;
|
||||||
|
ssize_t len;
|
||||||
|
u64 res = 0;
|
||||||
|
|
||||||
|
while ((len = getline(&buf, &alloc, stdin)) > 0) {
|
||||||
|
nstack = 0;
|
||||||
|
buf[len - 1] = 0;
|
||||||
|
p = buf;
|
||||||
|
while (*p) {
|
||||||
|
switch (*p) {
|
||||||
|
case '{':
|
||||||
|
case '(':
|
||||||
|
case '[':
|
||||||
|
case '<':
|
||||||
|
push(*p);
|
||||||
|
break;
|
||||||
|
case '}':
|
||||||
|
case ')':
|
||||||
|
case ']':
|
||||||
|
case '>':
|
||||||
|
res += match(*p);
|
||||||
|
}
|
||||||
|
p++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
static u64 part2()
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static u64 doit(int part)
|
||||||
|
{
|
||||||
|
return part == 1? part1(): part2();
|
||||||
|
}
|
||||||
|
|
||||||
|
static int usage(char *prg)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Usage: %s [-d debug_level] [-p part]\n", prg);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int ac, char **av)
|
||||||
|
{
|
||||||
|
int opt;
|
||||||
|
u32 exercise = 1;
|
||||||
|
u64 res;
|
||||||
|
|
||||||
|
while ((opt = getopt(ac, av, "d:p:")) != -1) {
|
||||||
|
switch (opt) {
|
||||||
|
case 'd':
|
||||||
|
debug_level_set(atoi(optarg));
|
||||||
|
break;
|
||||||
|
case 'p': /* 1 or 2 */
|
||||||
|
exercise = atoi(optarg);
|
||||||
|
if (exercise < 1 || exercise > 2)
|
||||||
|
return usage(*av);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return usage(*av);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (optind < ac)
|
||||||
|
return usage(*av);
|
||||||
|
|
||||||
|
res = doit(exercise);
|
||||||
|
printf("%s : res=%lu\n", *av, res);
|
||||||
|
exit (0);
|
||||||
|
}
|
Reference in New Issue
Block a user