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!
Ooh boy it's a little early in the season to already be stumped by an off-by-one bug. 1-BASED INDICES RYAN! 1-BASED! IT'S RIGHT THERE IN THE PROMPT!
Also, I started a little parsing module to help me with some of the repetitive tedia.
Day2.h:
/// Day 2: Password Philosophy /// /// Find the passwords that aren't compliant.#include <stdlib.h>
#include <stdint.h>
/// A Policied Password is a password that is accompanied by a policy/// consisting of two positive integers and a letter. These components/// can be used to validate the password.typedefstruct{inta;intb;charletter;charpassword[30];}PoliciedPassword;/// Part 1 calculates how many valid passwords there are./// A password is valid if the # of occurrences of 'letter' is between/// 'a' and 'b,' inclusive.////// passes: the list of policied passwords to check/// count: the number of passwords to checkintpart1(PoliciedPassword**passes,size_tcount);/// Part 2 calculates how many valid passwords there are./// A password is valid of either the 'a' index or 'b' index character /// (1-based!) is equal to 'letter,' but not both.intpart2(PoliciedPassword**passes,size_tcount);/// day2 runs both parts in sequence and outputs their results.intday2();
Day2.c:
/// Day 2: Password Philosophy /// /// Find the passwords that aren't compliant.#include "Day2.h"
#include "parsing.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
/// Parse the input file,staticPoliciedPassword**parse(size_t*lines){FILE*fp;fp=fopen("data/day2.txt","r");if(fp==NULL){printf("Couldn't open input file.\n");exit(EXIT_FAILURE);}*lines=count_lines(fp);PoliciedPassword**passes=malloc(sizeof(PoliciedPassword*)**lines);for(size_ti=0;i<*lines;i++){PoliciedPassword*p=malloc(sizeof(PoliciedPassword));fscanf(fp,"%d-%d %c: %s\n",&p->a,&p->b,&p->letter,p->password);passes[i]=p;}fclose(fp);returnpasses;}staticvoidfreePoliciedPasswordList(PoliciedPassword**passes,size_tcount){for(size_ti=0;i<count;i++){free(passes[i]);passes[i]=NULL;}free(passes);}intpart1(PoliciedPassword**passes,size_tcount){size_tvalid=0;for(size_ti=0;i<count;i++){PoliciedPassword*p=passes[i];uint8_tmatches=0;for(size_tj=0;p->password[j];j++){if(p->password[j]==p->letter)matches++;}if(p->a<=matches&&matches<=p->b)valid++;}returnvalid;}intpart2(PoliciedPassword**passes,size_tcount){size_tvalid=0;for(size_ti=0;i<count;i++){PoliciedPassword*p=passes[i];intmatches=0;if(p->password[p->a-1]==p->letter)matches++;if(p->password[p->b-1]==p->letter)matches++;if(matches==1)valid++;}returnvalid;}intday2(){size_tcount;PoliciedPassword**passes=parse(&count);printf("====== Day 2 ======\n");printf("Part 1: %d\n",part1(passes,count));printf("Part 2: %d\n",part2(passes,count));freePoliciedPasswordList(passes,count);returnEXIT_SUCCESS;}
parsing.h:
#ifndef AOC2020_PARSING_H
#define AOC2020_PARSING_H
#include <stdio.h>
#include <stdlib.h>
/// Counts the number of newline characters in a text file./// Assumes no newline at the end of the last line (so adds +1 more)size_tcount_lines(FILE*fp);#endif
Also a little tooling to help generate the daily files:
bin/newday:
#!/usr/bin/env bash# newday: Creates a new day of files for the Advent of Code Challenge## Date: 12/2/2020# Author: Ryan Palofunction usage(){echo"usage: newday NUMBER"echo
echo" NUMBER: The number of the day to create. Will be inserted"echo" into the templates and used for filenames."}function help(){echo"newday: Creates a new day of files for the Advent of Code challenge."echo
usage
echo}function make_day(){day="$1"echo"Creating Day ${day}.">&2
sed"s/{X}/$day/g""templates/DayX.c">"src/Day${day}.c"sed"s/{X}/$day/g""templates/DayX.h">"src/Day${day}.h"sed"s/{X}/$day/g""templates/TestDayX.c">"test/TestDay${day}.c"sed"s/{X}/$day/g"'templates/main.c'>'src/main.c'touch"data/day${day}.txt"echo"Complete.">&2
}function main(){if[["$#"-ne 1 ]];then
usage
exit 1
fi
if[["$1"=='-h']];then
help
exit 0
fi
if![["$1"=~ [:digit:]+ ]];then
echo"Input must be a number"exit 1
fi
make_day "$1"}
main "$@"
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.
Ooh boy it's a little early in the season to already be stumped by an off-by-one bug. 1-BASED INDICES RYAN! 1-BASED! IT'S RIGHT THERE IN THE PROMPT!
Also, I started a little parsing module to help me with some of the repetitive tedia.
Day2.h:
Day2.c:
parsing.h:
parsing.c:
Also a little tooling to help generate the daily files:
bin/newday: