Ryan is an engineer in the Sacramento Area with a focus in Python, Ruby, and Rust. Bash/Python Exercism mentor. Coding, physics, calculus, music, woodworking. Looking for work!
A little late to the party, but I got it done before bed time 😁
Day6.h:
#ifndef AOC2020_DAY6_H
#define AOC2020_DAY6_H
/// Day 6: Custom Customs/// /// Search through lists of questionnaire answers./// Each questionnaire is 26 questions, so a yes to question 'b' would/// be represented by the presence of 'b' in the output line./// People are one to a line, and parties are separated by a blank line.#include <stdlib.h>
#include <stdio.h>
/// Return the number of letters that are present for any line in a groupinttally_anyone_yeses(FILE*fp);/// Return the number of letters that are present in every line in a groupinttally_everyone_yeses(FILE*fp);/// Run both intday6(void);#endif
Day6.c:
#include "Day6.h"
#include <stdio.h>
#include <string.h>
/// Number of letters in the alphabet#define LETTERS 26
/// Max number of chars per line (yes to all plus newline plus NULL)#define MAX_LINE_SIZE (LETTERS + 2)
inttally_anyone_yeses(FILE*fp){intcounts[LETTERS]={0};charline[MAX_LINE_SIZE]={0};// Log each occurrence of a character.while(!feof(fp)){fgets(line,MAX_LINE_SIZE,fp);if(line[0]=='\n')break;for(inti=0;line[i]!='\n'&&line[i];i++){intindex=line[i]-'a';counts[index]++;}memset(line,0,MAX_LINE_SIZE);}// Loop through and find the total count of those that appeared.inttotal=0;for(inti=0;i<LETTERS;i++){if(counts[i]>0)total++;}returntotal;}/// Run the part 1 code on the input file.staticintpart1(){FILE*fp;fp=fopen("data/day6.txt","r");if(fp==NULL){printf("Couldn't open input file.\n");exit(EXIT_FAILURE);}// Loop through and add up the total for each group.inttotal=0;while(!feof(fp)){total+=tally_anyone_yeses(fp);}fclose(fp);returntotal;}inttally_everyone_yeses(FILE*fp){intcounts[LETTERS]={0};charline[MAX_LINE_SIZE]={0};intpeople_count=0;// Loop through the group and add up everybody's answers.while(!feof(fp)){fgets(line,MAX_LINE_SIZE,fp);if(line[0]=='\n')break;people_count++;for(inti=0;line[i]!='\n'&&line[i];i++){intindex=line[i]-'a';counts[index]++;}memset(line,0,MAX_LINE_SIZE);}// Count the number of letters where everyone answered it yes// i.e. number of yeses == number of peopleinttotal=0;for(inti=0;i<LETTERS;i++){if(counts[i]==people_count)total++;}returntotal;}/// Run the part 2 code on the input file.intpart2(){FILE*fp;fp=fopen("data/day6.txt","r");if(fp==NULL){printf("Couldn't open input file.\n");exit(EXIT_FAILURE);}// Loop through and add up the total for each group.inttotal=0;while(!feof(fp)){total+=tally_everyone_yeses(fp);}fclose(fp);returntotal;}intday6(){printf("====== Day 6 ======\n");printf("Part 1: %d\n",part1());printf("Part 2: %d\n",part2());returnEXIT_SUCCESS;}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
A little late to the party, but I got it done before bed time 😁
Day6.h:
Day6.c: