2020 day 19 part 1 (C) - won't work for part 2 !!
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# AOC daily Makefile - GNU make only.
|
||||
#
|
||||
# Copyright (C) 2022 Bruno Raoult ("br")
|
||||
# Copyright (C) 2021-2022 Bruno Raoult ("br")
|
||||
# Licensed under the GNU General Public License v3.0 or later.
|
||||
# Some rights reserved. See COPYING.
|
||||
#
|
||||
@@ -15,22 +15,22 @@ SHELL := /bin/bash
|
||||
|
||||
CC := gcc
|
||||
BEAR := bear
|
||||
CCLSFILE:= compile_commands.json
|
||||
|
||||
#LIB := aoc_$(shell uname -m)
|
||||
#INCDIR := ../include
|
||||
INCDIR := .
|
||||
#LIBDIR := ../lib
|
||||
#LDFLAGS := -L$(LIBDIR)
|
||||
LIB := aoc_$(shell uname -m)
|
||||
INCDIR := ../include
|
||||
LIBDIR := ../lib
|
||||
LDFLAGS := -L$(LIBDIR)
|
||||
#LDLIB := -l$(LIB) -lm
|
||||
#LDLIB := -l$(LIB)
|
||||
LDLIB := -l$(LIB)
|
||||
|
||||
#export LD_LIBRARY_PATH = $(LIBDIR)
|
||||
export LD_LIBRARY_PATH = $(LIBDIR)
|
||||
|
||||
CFLAGS += -std=gnu11
|
||||
CFLAGS += -O2
|
||||
CFLAGS += -g
|
||||
# for gprof
|
||||
#CFLAGS += -pg
|
||||
# CFLAGS += -pg
|
||||
CFLAGS += -Wall
|
||||
CFLAGS += -Wextra
|
||||
CFLAGS += -march=native
|
||||
@@ -50,11 +50,9 @@ VALGRINDFLAGS := --leak-check=full --show-leak-kinds=all --track-origins=yes \
|
||||
TIME := \time -f "\ttime: %E real, %U user, %S sys\n\tcontext-switch:\t%c+%w, page-faults: %F+%R\n"
|
||||
export PATH := .:$(PATH)
|
||||
|
||||
.PHONY: clean all compile assembly memcheck memcheck1 memcheck2 ex1 ex2 bear org
|
||||
.PHONY: clean all compile assembly memcheck memcheck1 memcheck2 ex1 ex2 ccls
|
||||
|
||||
org: README.org
|
||||
|
||||
all: org ex1 ex2
|
||||
all: README.org ccls ex1 ex2
|
||||
|
||||
memcheck: memcheck1 memcheck2
|
||||
|
||||
@@ -71,19 +69,21 @@ cpp: aoc-c.i
|
||||
assembly: aoc-c.s
|
||||
|
||||
ex1: aoc-c
|
||||
@$(TIME) ex1.bash < $(INPUT) 2>&1
|
||||
@$(TIME) aoc-c -p 1 < $(INPUT) 2>&1
|
||||
@$(TIME) ex1.bash -p 1 < $(INPUT)
|
||||
@$(TIME) aoc-c -p 1 < $(INPUT)
|
||||
|
||||
ex2: aoc-c
|
||||
@$(TIME) ex2.bash < $(INPUT) 2>&1
|
||||
@$(TIME) aoc-c -p 2 < $(INPUT) 2>&1
|
||||
@$(TIME) ex2.bash -p 2 < $(INPUT)
|
||||
@$(TIME) aoc-c -p 2 < $(INPUT)
|
||||
|
||||
ccls: $(CCLSFILE)
|
||||
|
||||
clean:
|
||||
@rm -f aoc-c core* vgcore* gmon.out aoc-c.s aoc-c.i README.html compile_commands.json
|
||||
|
||||
.c:
|
||||
@echo compiling $<
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) -I $(INCDIR) $< $(LDLIB) -o $@
|
||||
@$(CC) $(CFLAGS) $(LDFLAGS) -I $(INCDIR) $< $(LDLIB) -o $@
|
||||
|
||||
# generate pre-processed file (.i) and assembler (.s)
|
||||
%.i: %.c
|
||||
@@ -94,6 +94,15 @@ clean:
|
||||
@echo generating $@
|
||||
@$(CC) -S -fverbose-asm $(CFLAGS) -I $(INCDIR) $< -o $@
|
||||
|
||||
# generate README.org from README.html (must cleanup !)
|
||||
%.org: %.html
|
||||
@echo generating $@. Cleanup before commit !
|
||||
@pandoc $< -o $@
|
||||
|
||||
# generate compile_commands.json
|
||||
$(CCLSFILE): aoc-c.c Makefile
|
||||
$(BEAR) -- make clean compile
|
||||
|
||||
bear: clean
|
||||
@$(BEAR) -- make compile
|
||||
@touch .ccls-root
|
||||
|
@@ -1,8 +0,0 @@
|
||||
ex1.bash : res=285
|
||||
time: 0:00.56 real, 0.55 user, 0.00 sys
|
||||
context-switch: 12+1, page-faults: 0+5967
|
||||
|
||||
ex2.bash : res=412
|
||||
time: 0:03.37 real, 3.34 user, 0.03 sys
|
||||
context-switch: 19+1, page-faults: 0+11909
|
||||
|
@@ -1,145 +0,0 @@
|
||||
--- Day 19: Monster Messages ---
|
||||
|
||||
You land in an airport surrounded by dense forest. As you walk to your high-speed train, the Elves at the Mythical Information Bureau contact you again. They think their satellite has collected an image of a sea monster! Unfortunately, the connection to the satellite is having problems, and many of the messages sent back from the satellite have been corrupted.
|
||||
|
||||
They sent you a list of the rules valid messages should obey and a list of received messages they've collected so far (your puzzle input).
|
||||
|
||||
The rules for valid messages (the top part of your puzzle input) are numbered and build upon each other. For example:
|
||||
|
||||
0: 1 2
|
||||
1: "a"
|
||||
2: 1 3 | 3 1
|
||||
3: "b"
|
||||
|
||||
Some rules, like 3: "b", simply match a single character (in this case, b).
|
||||
|
||||
The remaining rules list the sub-rules that must be followed; for example, the rule 0: 1 2 means that to match rule 0, the text being checked must match rule 1, and the text after the part that matched rule 1 must then match rule 2.
|
||||
|
||||
Some of the rules have multiple lists of sub-rules separated by a pipe (|). This means that at least one list of sub-rules must match. (The ones that match might be different each time the rule is encountered.) For example, the rule 2: 1 3 | 3 1 means that to match rule 2, the text being checked must match rule 1 followed by rule 3 or it must match rule 3 followed by rule 1.
|
||||
|
||||
Fortunately, there are no loops in the rules, so the list of possible matches will be finite. Since rule 1 matches a and rule 3 matches b, rule 2 matches either ab or ba. Therefore, rule 0 matches aab or aba.
|
||||
|
||||
Here's a more interesting example:
|
||||
|
||||
0: 4 1 5
|
||||
1: 2 3 | 3 2
|
||||
2: 4 4 | 5 5
|
||||
3: 4 5 | 5 4
|
||||
4: "a"
|
||||
5: "b"
|
||||
|
||||
Here, because rule 4 matches a and rule 5 matches b, rule 2 matches two letters that are the same (aa or bb), and rule 3 matches two letters that are different (ab or ba).
|
||||
|
||||
Since rule 1 matches rules 2 and 3 once each in either order, it must match two pairs of letters, one pair with matching letters and one pair with different letters. This leaves eight possibilities: aaab, aaba, bbab, bbba, abaa, abbb, baaa, or babb.
|
||||
|
||||
Rule 0, therefore, matches a (rule 4), then any of the eight options from rule 1, then b (rule 5): aaaabb, aaabab, abbabb, abbbab, aabaab, aabbbb, abaaab, or ababbb.
|
||||
|
||||
The received messages (the bottom part of your puzzle input) need to be checked against the rules so you can determine which are valid and which are corrupted. Including the rules and the messages together, this might look like:
|
||||
|
||||
0: 4 1 5
|
||||
1: 2 3 | 3 2
|
||||
2: 4 4 | 5 5
|
||||
3: 4 5 | 5 4
|
||||
4: "a"
|
||||
5: "b"
|
||||
|
||||
ababbb
|
||||
bababa
|
||||
abbbab
|
||||
aaabbb
|
||||
aaaabbb
|
||||
|
||||
Your goal is to determine the number of messages that completely match rule 0. In the above example, ababbb and abbbab match, but bababa, aaabbb, and aaaabbb do not, producing the answer 2. The whole message must match all of rule 0; there can't be extra unmatched characters in the message. (For example, aaaabbb might appear to match rule 0 above, but it has an extra unmatched b on the end.)
|
||||
|
||||
How many messages completely match rule 0?
|
||||
|
||||
Your puzzle answer was 285.
|
||||
--- Part Two ---
|
||||
|
||||
As you look over the list of messages, you realize your matching rules aren't quite right. To fix them, completely replace rules 8: 42 and 11: 42 31 with the following:
|
||||
|
||||
8: 42 | 42 8
|
||||
11: 42 31 | 42 11 31
|
||||
|
||||
This small change has a big impact: now, the rules do contain loops, and the list of messages they could hypothetically match is infinite. You'll need to determine how these changes affect which messages are valid.
|
||||
|
||||
Fortunately, many of the rules are unaffected by this change; it might help to start by looking at which rules always match the same set of values and how those rules (especially rules 42 and 31) are used by the new versions of rules 8 and 11.
|
||||
|
||||
(Remember, you only need to handle the rules you have; building a solution that could handle any hypothetical combination of rules would be significantly more difficult.)
|
||||
|
||||
For example:
|
||||
|
||||
42: 9 14 | 10 1
|
||||
9: 14 27 | 1 26
|
||||
10: 23 14 | 28 1
|
||||
1: "a"
|
||||
11: 42 31
|
||||
5: 1 14 | 15 1
|
||||
19: 14 1 | 14 14
|
||||
12: 24 14 | 19 1
|
||||
16: 15 1 | 14 14
|
||||
31: 14 17 | 1 13
|
||||
6: 14 14 | 1 14
|
||||
2: 1 24 | 14 4
|
||||
0: 8 11
|
||||
13: 14 3 | 1 12
|
||||
15: 1 | 14
|
||||
17: 14 2 | 1 7
|
||||
23: 25 1 | 22 14
|
||||
28: 16 1
|
||||
4: 1 1
|
||||
20: 14 14 | 1 15
|
||||
3: 5 14 | 16 1
|
||||
27: 1 6 | 14 18
|
||||
14: "b"
|
||||
21: 14 1 | 1 14
|
||||
25: 1 1 | 1 14
|
||||
22: 14 14
|
||||
8: 42
|
||||
26: 14 22 | 1 20
|
||||
18: 15 15
|
||||
7: 14 5 | 1 21
|
||||
24: 14 1
|
||||
|
||||
abbbbbabbbaaaababbaabbbbabababbbabbbbbbabaaaa
|
||||
bbabbbbaabaabba
|
||||
babbbbaabbbbbabbbbbbaabaaabaaa
|
||||
aaabbbbbbaaaabaababaabababbabaaabbababababaaa
|
||||
bbbbbbbaaaabbbbaaabbabaaa
|
||||
bbbababbbbaaaaaaaabbababaaababaabab
|
||||
ababaaaaaabaaab
|
||||
ababaaaaabbbaba
|
||||
baabbaaaabbaaaababbaababb
|
||||
abbbbabbbbaaaababbbbbbaaaababb
|
||||
aaaaabbaabaaaaababaa
|
||||
aaaabbaaaabbaaa
|
||||
aaaabbaabbaaaaaaabbbabbbaaabbaabaaa
|
||||
babaaabbbaaabaababbaabababaaab
|
||||
aabbbbbaabbbaaaaaabbbbbababaaaaabbaaabba
|
||||
|
||||
Without updating rules 8 and 11, these rules only match three messages: bbabbbbaabaabba, ababaaaaaabaaab, and ababaaaaabbbaba.
|
||||
|
||||
However, after updating rules 8 and 11, a total of 12 messages match:
|
||||
|
||||
bbabbbbaabaabba
|
||||
babbbbaabbbbbabbbbbbaabaaabaaa
|
||||
aaabbbbbbaaaabaababaabababbabaaabbababababaaa
|
||||
bbbbbbbaaaabbbbaaabbabaaa
|
||||
bbbababbbbaaaaaaaabbababaaababaabab
|
||||
ababaaaaaabaaab
|
||||
ababaaaaabbbaba
|
||||
baabbaaaabbaaaababbaababb
|
||||
abbbbabbbbaaaababbbbbbaaaababb
|
||||
aaaaabbaabaaaaababaa
|
||||
aaaabbaabbaaaaaaabbbabbbaaabbaabaaa
|
||||
aabbbbbaabbbaaaaaabbbbbababaaaaabbaaabba
|
||||
|
||||
After updating rules 8 and 11, how many messages completely match rule 0?
|
||||
|
||||
Your puzzle answer was 412.
|
||||
|
||||
Both parts of this puzzle are complete! They provide two gold stars: **
|
||||
|
||||
At this point, you should return to your Advent calendar and try another puzzle.
|
||||
|
||||
If you still want to see it, you can get your puzzle input.
|
210
2020/day19/README.org
Normal file
210
2020/day19/README.org
Normal file
@@ -0,0 +1,210 @@
|
||||
** --- Day 19: Monster Messages ---
|
||||
You land in an airport surrounded by dense forest. As you walk to your
|
||||
high-speed train, the Elves at the Mythical Information Bureau contact
|
||||
you again. They think their satellite has collected an image of a /sea
|
||||
monster/! Unfortunately, the connection to the satellite is having
|
||||
problems, and many of the messages sent back from the satellite have
|
||||
been corrupted.
|
||||
|
||||
They sent you a list of /the rules valid messages should obey/ and a
|
||||
list of /received messages/ they've collected so far (your puzzle
|
||||
input).
|
||||
|
||||
The /rules for valid messages/ (the top part of your puzzle input) are
|
||||
numbered and build upon each other. For example:
|
||||
|
||||
#+BEGIN_EXAMPLE
|
||||
0: 1 2
|
||||
1: "a"
|
||||
2: 1 3 | 3 1
|
||||
3: "b"
|
||||
#+END_EXAMPLE
|
||||
|
||||
Some rules, like =3: "b"=, simply match a single character (in this
|
||||
case, =b=).
|
||||
|
||||
The remaining rules list the sub-rules that must be followed; for
|
||||
example, the rule =0: 1 2= means that to match rule =0=, the text being
|
||||
checked must match rule =1=, and the text after the part that matched
|
||||
rule =1= must then match rule =2=.
|
||||
|
||||
Some of the rules have multiple lists of sub-rules separated by a pipe
|
||||
(=|=). This means that /at least one/ list of sub-rules must match. (The
|
||||
ones that match might be different each time the rule is encountered.)
|
||||
For example, the rule =2: 1 3 | 3 1= means that to match rule =2=, the
|
||||
text being checked must match rule =1= followed by rule =3= /or/ it must
|
||||
match rule =3= followed by rule =1=.
|
||||
|
||||
Fortunately, there are no loops in the rules, so the list of possible
|
||||
matches will be finite. Since rule =1= matches =a= and rule =3= matches
|
||||
=b=, rule =2= matches either =ab= or =ba=. Therefore, rule =0= matches
|
||||
=aab= or =aba=.
|
||||
|
||||
Here's a more interesting example:
|
||||
|
||||
#+BEGIN_EXAMPLE
|
||||
0: 4 1 5
|
||||
1: 2 3 | 3 2
|
||||
2: 4 4 | 5 5
|
||||
3: 4 5 | 5 4
|
||||
4: "a"
|
||||
5: "b"
|
||||
#+END_EXAMPLE
|
||||
|
||||
Here, because rule =4= matches =a= and rule =5= matches =b=, rule =2=
|
||||
matches two letters that are the same (=aa= or =bb=), and rule =3=
|
||||
matches two letters that are different (=ab= or =ba=).
|
||||
|
||||
Since rule =1= matches rules =2= and =3= once each in either order, it
|
||||
must match two pairs of letters, one pair with matching letters and one
|
||||
pair with different letters. This leaves eight possibilities: =aaab=,
|
||||
=aaba=, =bbab=, =bbba=, =abaa=, =abbb=, =baaa=, or =babb=.
|
||||
|
||||
Rule =0=, therefore, matches =a= (rule =4=), then any of the eight
|
||||
options from rule =1=, then =b= (rule =5=): =aaaabb=, =aaabab=,
|
||||
=abbabb=, =abbbab=, =aabaab=, =aabbbb=, =abaaab=, or =ababbb=.
|
||||
|
||||
The /received messages/ (the bottom part of your puzzle input) need to
|
||||
be checked against the rules so you can determine which are valid and
|
||||
which are corrupted. Including the rules and the messages together, this
|
||||
might look like:
|
||||
|
||||
#+BEGIN_EXAMPLE
|
||||
0: 4 1 5
|
||||
1: 2 3 | 3 2
|
||||
2: 4 4 | 5 5
|
||||
3: 4 5 | 5 4
|
||||
4: "a"
|
||||
5: "b"
|
||||
|
||||
ababbb
|
||||
bababa
|
||||
abbbab
|
||||
aaabbb
|
||||
aaaabbb
|
||||
#+END_EXAMPLE
|
||||
|
||||
Your goal is to determine /the number of messages that completely match
|
||||
rule =0=/. In the above example, =ababbb= and =abbbab= match, but
|
||||
=bababa=, =aaabbb=, and =aaaabbb= do not, producing the answer /=2=/.
|
||||
The whole message must match all of rule =0=; there can't be extra
|
||||
unmatched characters in the message. (For example, =aaaabbb= might
|
||||
appear to match rule =0= above, but it has an extra unmatched =b= on the
|
||||
end.)
|
||||
|
||||
/How many messages completely match rule =0=?/
|
||||
|
||||
Your puzzle answer was =285=.
|
||||
|
||||
** --- Part Two ---
|
||||
As you look over the list of messages, you realize your matching rules
|
||||
aren't quite right. To fix them, completely replace rules =8: 42= and
|
||||
=11: 42 31= with the following:
|
||||
|
||||
#+BEGIN_EXAMPLE
|
||||
8: 42 | 42 8
|
||||
11: 42 31 | 42 11 31
|
||||
#+END_EXAMPLE
|
||||
|
||||
This small change has a big impact: now, the rules /do/ contain loops,
|
||||
and the list of messages they could hypothetically match is infinite.
|
||||
You'll need to determine how these changes affect which messages are
|
||||
valid.
|
||||
|
||||
Fortunately, many of the rules are unaffected by this change; it might
|
||||
help to start by looking at which rules always match the same set of
|
||||
values and how /those/ rules (especially rules =42= and =31=) are used
|
||||
by the new versions of rules =8= and =11=.
|
||||
|
||||
(Remember, /you only need to handle the rules you have/; building a
|
||||
solution that could handle any hypothetical combination of rules would
|
||||
be [[https://en.wikipedia.org/wiki/Formal_grammar][significantly more
|
||||
difficult]].)
|
||||
|
||||
For example:
|
||||
|
||||
#+BEGIN_EXAMPLE
|
||||
42: 9 14 | 10 1
|
||||
9: 14 27 | 1 26
|
||||
10: 23 14 | 28 1
|
||||
1: "a"
|
||||
11: 42 31
|
||||
5: 1 14 | 15 1
|
||||
19: 14 1 | 14 14
|
||||
12: 24 14 | 19 1
|
||||
16: 15 1 | 14 14
|
||||
31: 14 17 | 1 13
|
||||
6: 14 14 | 1 14
|
||||
2: 1 24 | 14 4
|
||||
0: 8 11
|
||||
13: 14 3 | 1 12
|
||||
15: 1 | 14
|
||||
17: 14 2 | 1 7
|
||||
23: 25 1 | 22 14
|
||||
28: 16 1
|
||||
4: 1 1
|
||||
20: 14 14 | 1 15
|
||||
3: 5 14 | 16 1
|
||||
27: 1 6 | 14 18
|
||||
14: "b"
|
||||
21: 14 1 | 1 14
|
||||
25: 1 1 | 1 14
|
||||
22: 14 14
|
||||
8: 42
|
||||
26: 14 22 | 1 20
|
||||
18: 15 15
|
||||
7: 14 5 | 1 21
|
||||
24: 14 1
|
||||
|
||||
abbbbbabbbaaaababbaabbbbabababbbabbbbbbabaaaa
|
||||
bbabbbbaabaabba
|
||||
babbbbaabbbbbabbbbbbaabaaabaaa
|
||||
aaabbbbbbaaaabaababaabababbabaaabbababababaaa
|
||||
bbbbbbbaaaabbbbaaabbabaaa
|
||||
bbbababbbbaaaaaaaabbababaaababaabab
|
||||
ababaaaaaabaaab
|
||||
ababaaaaabbbaba
|
||||
baabbaaaabbaaaababbaababb
|
||||
abbbbabbbbaaaababbbbbbaaaababb
|
||||
aaaaabbaabaaaaababaa
|
||||
aaaabbaaaabbaaa
|
||||
aaaabbaabbaaaaaaabbbabbbaaabbaabaaa
|
||||
babaaabbbaaabaababbaabababaaab
|
||||
aabbbbbaabbbaaaaaabbbbbababaaaaabbaaabba
|
||||
#+END_EXAMPLE
|
||||
|
||||
Without updating rules =8= and =11=, these rules only match three
|
||||
messages: =bbabbbbaabaabba=, =ababaaaaaabaaab=, and =ababaaaaabbbaba=.
|
||||
|
||||
However, after updating rules =8= and =11=, a total of /=12=/ messages
|
||||
match:
|
||||
|
||||
- =bbabbbbaabaabba=
|
||||
- =babbbbaabbbbbabbbbbbaabaaabaaa=
|
||||
- =aaabbbbbbaaaabaababaabababbabaaabbababababaaa=
|
||||
- =bbbbbbbaaaabbbbaaabbabaaa=
|
||||
- =bbbababbbbaaaaaaaabbababaaababaabab=
|
||||
- =ababaaaaaabaaab=
|
||||
- =ababaaaaabbbaba=
|
||||
- =baabbaaaabbaaaababbaababb=
|
||||
- =abbbbabbbbaaaababbbbbbaaaababb=
|
||||
- =aaaaabbaabaaaaababaa=
|
||||
- =aaaabbaabbaaaaaaabbbabbbaaabbaabaaa=
|
||||
- =aabbbbbaabbbaaaaaabbbbbababaaaaabbaaabba=
|
||||
|
||||
/After updating rules =8= and =11=, how many messages completely match
|
||||
rule =0=?/
|
||||
|
||||
Your puzzle answer was =412=.
|
||||
|
||||
Both parts of this puzzle are complete! They provide two gold stars: **
|
||||
|
||||
At this point, all that is left is for you to [[/2020][admire your
|
||||
Advent calendar]].
|
||||
|
||||
If you still want to see it, you can [[file:19/input][get your puzzle
|
||||
input]].
|
||||
|
||||
You can also [Shareon
|
||||
[[https://twitter.com/intent/tweet?text=I%27ve+completed+%22Monster+Messages%22+%2D+Day+19+%2D+Advent+of+Code+2020&url=https%3A%2F%2Fadventofcode%2Ecom%2F2020%2Fday%2F19&related=ericwastl&hashtags=AdventOfCode][Twitter]]
|
||||
[[javascript:void(0);][Mastodon]]] this puzzle.
|
224
2020/day19/aoc-c.c
Normal file
224
2020/day19/aoc-c.c
Normal file
@@ -0,0 +1,224 @@
|
||||
/* aoc-c.c: Advent2020, day 23, part 1
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#include "list.h"
|
||||
#include "debug.h"
|
||||
|
||||
#define MAX_RULES 256
|
||||
#define MAX_MSG 512
|
||||
/* to simplify code, we consider here that :
|
||||
* - a rule has no more than 3 sub-rules
|
||||
* - there are at most 2 possible sub-rules per rule
|
||||
*/
|
||||
struct subrule {
|
||||
int rule[2][3];
|
||||
};
|
||||
static struct rule {
|
||||
enum type {
|
||||
SUB,
|
||||
CHR
|
||||
} type;
|
||||
struct subrule sub;
|
||||
int str;
|
||||
} rules[MAX_RULES] = {
|
||||
[0 ... (MAX_RULES-1)] = {
|
||||
.sub.rule = {{-1, -1, -1}, {-1, -1, -1}}
|
||||
}
|
||||
};
|
||||
|
||||
static char *mesg[MAX_MSG];
|
||||
static int nrules, nmesg;
|
||||
|
||||
static void printall()
|
||||
{
|
||||
for (int i = 0; i < nrules; ++i) {
|
||||
printf("rule %3d : ", i);
|
||||
if (rules[i].type == CHR) {
|
||||
printf("[%c]\n", rules[i].str);
|
||||
continue;
|
||||
}
|
||||
for (int j = 0; j < 3 && rules[i].sub.rule[0][j] != -1; ++j)
|
||||
printf("%d ", rules[i].sub.rule[0][j]);
|
||||
for (int j = 0; j < 3 && rules[i].sub.rule[1][j] != -1; ++j)
|
||||
printf("%s%d ", j? "": "| ", rules[i].sub.rule[1][j]);
|
||||
printf("\n");
|
||||
}
|
||||
for (int i = 0; i < nmesg; ++i) {
|
||||
printf("%3d: %s\n", i, mesg[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* parse - parse input.
|
||||
*/
|
||||
static void parse()
|
||||
{
|
||||
size_t alloc;
|
||||
ssize_t len;
|
||||
char *buf = NULL, *tok;
|
||||
int rule;
|
||||
while ((len = getline(&buf, &alloc, stdin)) > 0) {
|
||||
int or = 0, sub = 0;
|
||||
|
||||
buf[--len] = 0;
|
||||
if (len == 0)
|
||||
continue;
|
||||
if (isalpha(*buf)) { /* message */
|
||||
mesg[nmesg++] = strdup(buf);
|
||||
} else { /* rule */
|
||||
if (!(tok = strtok(buf, ": "))) /* rule number */
|
||||
continue;
|
||||
nrules++;
|
||||
rule = atoi(tok);
|
||||
while ((tok = strtok(NULL, ":\" "))) {
|
||||
switch (*tok) {
|
||||
case 'a': /* final rule */
|
||||
case 'b':
|
||||
rules[rule].type = CHR;
|
||||
rules[rule].str = *tok;
|
||||
goto nextline;
|
||||
case '|': /* second ruleset */
|
||||
or++;
|
||||
sub = 0;
|
||||
break;
|
||||
default:
|
||||
rules[rule].type = SUB;
|
||||
rules[rule].sub.rule[or][sub] = atoi(tok);
|
||||
sub++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
nextline: ;
|
||||
}
|
||||
free(buf);
|
||||
}
|
||||
|
||||
static int match(char *str, int *pos, int rule, int depth)
|
||||
{
|
||||
struct rule *r = rules+rule;
|
||||
int found = 0, postmp = *pos;
|
||||
char *space = " ";
|
||||
log_f(3, "%.*sstr=%s pos=%d rule=%d\n", depth * 2, space, str, *pos, rule);
|
||||
/* check for no char left ? */
|
||||
//if (!str[*pos]) {
|
||||
// printf("No char left !\n");
|
||||
// found = 0;
|
||||
// goto end;
|
||||
//}
|
||||
switch (r->type) {
|
||||
case SUB:
|
||||
found = 1;
|
||||
log_f(3, "%.*sLEFT\n", depth * 2, space);
|
||||
for (int sub = 0; sub < 3 && r->sub.rule[0][sub] >= 0; ++sub) {
|
||||
if (!match(str, pos, r->sub.rule[0][sub], depth + 1)) {
|
||||
*pos = postmp;
|
||||
found = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (found || r->sub.rule[1][0] == -1)
|
||||
goto end;
|
||||
log_f(3, "%.*sRIGHT\n", depth * 2, space);
|
||||
found = 1;
|
||||
for (int sub = 0; sub < 3 && r->sub.rule[1][sub] >= 0; ++sub) {
|
||||
if (!match(str, pos, r->sub.rule[1][sub], depth + 1)) {
|
||||
*pos = postmp;
|
||||
found = 0;
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
goto end;
|
||||
case CHR:
|
||||
if (rules[rule].str == str[*pos]) {
|
||||
(*pos)++;
|
||||
found = 1;
|
||||
goto end;
|
||||
}
|
||||
found = 0;
|
||||
goto end;
|
||||
}
|
||||
end:
|
||||
/* check for exact length */
|
||||
if (depth == 0 && str[*pos]) {
|
||||
log_f(3, "chars remaining !\n");
|
||||
found = 0;
|
||||
}
|
||||
log_f(3, "%.*sstr=%s pos=%d rule=%d ret=%s\n", depth * 2, space, str+*pos, *pos, rule, found? "ok":
|
||||
"NOK");
|
||||
return found; /* not reached */
|
||||
}
|
||||
|
||||
static long part1()
|
||||
{
|
||||
int ok = 0, pos;
|
||||
for (int msg = 0; msg < nmesg; ++msg) {
|
||||
pos = 0;
|
||||
if (match(mesg[msg], &pos, 0, 0)) {
|
||||
printf("%s: ok\n", mesg[msg]);
|
||||
ok++;
|
||||
} else {
|
||||
printf("%s: NOK\n", mesg[msg]);
|
||||
}
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
static long part2()
|
||||
{
|
||||
static const struct subrule new[2] = {
|
||||
{{{42, -1, -1}, {42, 8, -1}}},
|
||||
{{{42, 31, -1}, {42, 11, 31}}}
|
||||
};
|
||||
rules[8].sub = new[0];
|
||||
rules[11].sub = new[1];
|
||||
|
||||
int ok = 0, pos;
|
||||
for (int msg = 0; msg < nmesg; ++msg) {
|
||||
pos = 0;
|
||||
if (match(mesg[msg], &pos, 0, 0)) {
|
||||
printf("%s: ok\n", mesg[msg]);
|
||||
ok++;
|
||||
} else {
|
||||
printf("%s: NOK\n", mesg[msg]);
|
||||
}
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int usage(char *prg)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s [-d debug_level] [-p part]\n", prg);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main(ac, av)
|
||||
int ac;
|
||||
char **av;
|
||||
{
|
||||
int opt, part = 1;
|
||||
|
||||
while ((opt = getopt(ac, av, "d:p:")) != -1) {
|
||||
switch (opt) {
|
||||
case 'd':
|
||||
debug_level_set(atoi(optarg));
|
||||
break;
|
||||
case 'p': /* 1 or 2 */
|
||||
part = atoi(optarg);
|
||||
if (part < 1 || part > 2)
|
||||
default:
|
||||
return usage(*av);
|
||||
}
|
||||
}
|
||||
|
||||
parse();
|
||||
printf("%s : res=%ld\n", *av, part == 1? part1(): part2());
|
||||
printall();
|
||||
exit (0);
|
||||
}
|
@@ -1,46 +0,0 @@
|
||||
/* ex1-c: Advent2020 game, day 19/tasks 1 & 2
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
struct rule {
|
||||
struct rule *left;
|
||||
struct rule *right;
|
||||
|
||||
};
|
||||
int main(ac, av)
|
||||
int ac;
|
||||
char **av;
|
||||
{
|
||||
char line[1024];
|
||||
long res=0, tmp;
|
||||
|
||||
if (ac != 2) {
|
||||
fprintf(stderr, "usage: %s [1|2]\n", *av);
|
||||
exit (1);
|
||||
}
|
||||
if (**(av+1) == '2')
|
||||
prio=&prio_2;
|
||||
|
||||
while (fgets(line, sizeof line, stdin)) {
|
||||
//gets(line, sizeof line, stdin);
|
||||
//NPUSH(10);
|
||||
//NPUSH(100);
|
||||
//NPUSH(1000);
|
||||
//print();
|
||||
//printf("TOP=%ld\n", NTOP());
|
||||
//NPOP();
|
||||
//print();
|
||||
|
||||
saveptr=line;
|
||||
//printf("%s", line);
|
||||
tmp=eval_expr();
|
||||
//printf("%s : res=%ld\n", line, tmp);
|
||||
res+=tmp;
|
||||
}
|
||||
printf("%s : res=%ld\n", *av, res);
|
||||
exit (0);
|
||||
}
|
Reference in New Issue
Block a user