day 8 part 1 (C) + init part 2
This commit is contained in:
@@ -1 +1,10 @@
|
||||
acedgfb cdfbe gcdfa fbcad dab cefabd cdfgeb eafb cagedb ab | cdfeb fcadb cdfeb cdbaf
|
||||
be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb | fdgacbe cefdb cefbgd gcbe
|
||||
edbfga begcd cbg gc gcadebf fbgde acbgfd abcde gfcbed gfec | fcgedb cgb dgebacf gc
|
||||
fgaebd cg bdaec gdafb agbcfd gdcbef bgcad gfac gcb cdgabef | cg cg fdcagb cbg
|
||||
fbegcd cbd adcefb dageb afcb bc aefdc ecdab fgdeca fcdbega | efabcd cedba gadfec cb
|
||||
aecbfdg fbg gf bafeg dbefa fcge gcbea fcaegb dgceab fcbdga | gecf egdcabf bgf bfgea
|
||||
fgeab ca afcebg bdacfeg cfaedg gcfdb baec bfadeg bafgc acf | gebdcfa ecba ca fadegcb
|
||||
dbcfg fgd bdegcaf fgec aegbdf ecdfab fbedc dacgb gdcebf gf | cefg dcbef fcge gbcadfe
|
||||
bdfegc cbegaf gecbf dfcage bdacg ed bedf ced adcbefg gebcd | ed bcgafe cdgba cbgef
|
||||
egadfb cdbfeg cegd fecab cgb gbdefca cg fgcdab egfdb bfceg | gbdfcae bgc cg cgb
|
||||
gcafb gcf dcaebfg ecagb gf abcdeg gaef cafbge fdbac fegbdc | fgae cfgab fg bagce
|
||||
|
@@ -1,20 +0,0 @@
|
||||
be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb |
|
||||
fdgacbe cefdb cefbgd gcbe
|
||||
edbfga begcd cbg gc gcadebf fbgde acbgfd abcde gfcbed gfec |
|
||||
fcgedb cgb dgebacf gc
|
||||
fgaebd cg bdaec gdafb agbcfd gdcbef bgcad gfac gcb cdgabef |
|
||||
cg cg fdcagb cbg
|
||||
fbegcd cbd adcefb dageb afcb bc aefdc ecdab fgdeca fcdbega |
|
||||
efabcd cedba gadfec cb
|
||||
aecbfdg fbg gf bafeg dbefa fcge gcbea fcaegb dgceab fcbdga |
|
||||
gecf egdcabf bgf bfgea
|
||||
fgeab ca afcebg bdacfeg cfaedg gcfdb baec bfadeg bafgc acf |
|
||||
gebdcfa ecba ca fadegcb
|
||||
dbcfg fgd bdegcaf fgec aegbdf ecdfab fbedc dacgb gdcebf gf |
|
||||
cefg dcbef fcge gbcadfe
|
||||
bdfegc cbegaf gecbf dfcage bdacg ed bedf ced adcbefg gebcd |
|
||||
ed bcgafe cdgba cbgef
|
||||
egadfb cdbfeg cegd fecab cgb gbdefca cg fgcdab egfdb bfceg |
|
||||
gbdfcae bgc cg cgb
|
||||
gcafb gcf dcaebfg ecagb gf abcdeg gaef cafbge fdbac fegbdc |
|
||||
fgae cfgab fg bagce
|
@@ -24,7 +24,7 @@ LDLIB := -l$(LIB)
|
||||
export LD_LIBRARY_PATH = $(LIBDIR)
|
||||
|
||||
CFLAGS += -std=gnu99
|
||||
CFLAGS += -O2
|
||||
#CFLAGS += -O2
|
||||
CFLAGS += -g
|
||||
CFLAGS += -Wall
|
||||
CFLAGS += -Wextra
|
||||
|
@@ -69,3 +69,62 @@ fgae cfgab fg bagce
|
||||
Because the digits 1, 4, 7, and 8 each use a unique number of segments, you should be able to tell which combinations of signals correspond to those digits. Counting only digits in the output values (the part after | on each line), in the above example, there are 26 instances of digits that use a unique number of segments (highlighted above).
|
||||
|
||||
In the output values, how many times do digits 1, 4, 7, or 8 appear?
|
||||
|
||||
Your puzzle answer was 543.
|
||||
|
||||
The first half of this puzzle is complete! It provides one gold star: *
|
||||
--- Part Two ---
|
||||
|
||||
Through a little deduction, you should now be able to determine the remaining digits. Consider again the first example above:
|
||||
|
||||
acedgfb cdfbe gcdfa fbcad dab cefabd cdfgeb eafb cagedb ab |
|
||||
cdfeb fcadb cdfeb cdbaf
|
||||
|
||||
After some careful analysis, the mapping between signal wires and segments only make sense in the following configuration:
|
||||
|
||||
dddd
|
||||
e a
|
||||
e a
|
||||
ffff
|
||||
g b
|
||||
g b
|
||||
cccc
|
||||
|
||||
So, the unique signal patterns would correspond to the following digits:
|
||||
|
||||
acedgfb: 8
|
||||
cdfbe: 5
|
||||
gcdfa: 2
|
||||
fbcad: 3
|
||||
dab: 7
|
||||
cefabd: 9
|
||||
cdfgeb: 6
|
||||
eafb: 4
|
||||
cagedb: 0
|
||||
ab: 1
|
||||
|
||||
Then, the four digits of the output value can be decoded:
|
||||
|
||||
cdfeb: 5
|
||||
fcadb: 3
|
||||
cdfeb: 5
|
||||
cdbaf: 3
|
||||
|
||||
Therefore, the output value for this entry is 5353.
|
||||
|
||||
Following this same process for each entry in the second, larger example above, the output value of each entry can be determined:
|
||||
|
||||
fdgacbe cefdb cefbgd gcbe: 8394
|
||||
fcgedb cgb dgebacf gc: 9781
|
||||
cg cg fdcagb cbg: 1197
|
||||
efabcd cedba gadfec cb: 9361
|
||||
gecf egdcabf bgf bfgea: 4873
|
||||
gebdcfa ecba ca fadegcb: 8418
|
||||
cefg dcbef fcge gbcadfe: 4548
|
||||
ed bcgafe cdgba cbgef: 1625
|
||||
gbdfcae bgc cg cgb: 8717
|
||||
fgae cfgab fg bagce: 4315
|
||||
|
||||
Adding all of the output values in this larger example produces 61229.
|
||||
|
||||
For each entry, determine all of the wire/segment connections and decode the four-digit output values. What do you get if you add up all of the output values?
|
||||
|
140
2021/day08/aoc-c.c
Normal file
140
2021/day08/aoc-c.c
Normal file
@@ -0,0 +1,140 @@
|
||||
/* 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"
|
||||
|
||||
typedef struct {
|
||||
int len;
|
||||
char *code;
|
||||
} token;
|
||||
|
||||
typedef struct {
|
||||
token unique[10];
|
||||
token output[4];
|
||||
} code;
|
||||
|
||||
//#ifdef DEBUG
|
||||
static void print_code(code *code)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
//printf("crabs=%d max=%d\n", ncrabs, crab_max);
|
||||
printf("unique: ");
|
||||
for (i = 0; i < 10; ++i)
|
||||
printf("[%d]%s ", code->unique[i].len, code->unique[i].code);
|
||||
printf("\n");
|
||||
printf("output: ");
|
||||
for (i = 0; i < 4; ++i)
|
||||
printf("[%d]%s ", code->output[i].len, code->output[i].code);
|
||||
printf("\n");
|
||||
}
|
||||
//#endif
|
||||
|
||||
static code *read_code()
|
||||
{
|
||||
int i = 0;
|
||||
static char *buf = NULL;
|
||||
char *token;
|
||||
size_t alloc = 0;
|
||||
static code code;
|
||||
|
||||
if (getline(&buf, &alloc, stdin) < 0)
|
||||
return NULL;
|
||||
|
||||
/* read unique segment data
|
||||
*/
|
||||
token = strtok(buf, " \n");
|
||||
while (token) {
|
||||
if (*token == '|')
|
||||
break;
|
||||
code.unique[i].code = token;
|
||||
code.unique[i].len = strlen(token);
|
||||
i++;
|
||||
token = strtok(NULL, " \n");
|
||||
}
|
||||
//printf("cont = %c\n", *token);
|
||||
//print_code(&code);
|
||||
i = 0;
|
||||
while ((token = strtok(NULL, " \n"))) {
|
||||
//printf("output %d = [%s]\n", i, token);
|
||||
code.output[i].code = token;
|
||||
code.output[i].len = strlen(token);
|
||||
i++;
|
||||
}
|
||||
if (i != 4)
|
||||
printf("output = %d\n", i);
|
||||
|
||||
free(buf);
|
||||
return &code;
|
||||
}
|
||||
|
||||
static u64 doit(int part)
|
||||
{
|
||||
code *code;
|
||||
int res = 0;
|
||||
|
||||
while ((code = read_code())) {
|
||||
if (part == 1) {
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
int len = code->output[i].len;
|
||||
/* digits: 1 4 7 8 */
|
||||
if (len == 2 || len == 4 || len == 3 || len == 7) {
|
||||
printf("%d : %s\n", len, code->output[i].code);
|
||||
res++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
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