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!
I'm not ashamed to say that I set this one up and let it run. It seemed like it ran more slowly than I expected, but at this point, it is what it is, and I'm good with it.
#include "Day25.h"
/// Day 25: Combo Breaker////// "Break" into your hotel room by brute-forcing the shared key between/// key card and door electronics.#include <stdio.h>
#define PUB_KEY_SUBJECT 7 ///< The number used to generate public keys
/// Transfor a number (subject) based on a number of loops through a/// simple hashing algorithm.staticinttransform(longsubject,longloop_size){longlongvalue=1;for(longi=0;i<loop_size;i++){value*=subject;value%=20201227;}returnvalue;}/// A handshake consists of public keys from door and card that,/// when looped by the other entity, come out to the same number.typedefstruct{longdoor_key;longcard_key;longdoor_loop_size;longcard_loop_size;}RFIDHandshake;/// Parse the input file, which is just two lines with one integer/// per line.staticRFIDHandshakeparse(constchar*filename){FILE*fp;fp=fopen(filename,"r");if(fp==NULL){printf("Couldn't open input file.\n");exit(EXIT_FAILURE);}RFIDHandshakekey;fscanf(fp,"%ld\n%ld",&key.card_key,&key.door_key);fclose(fp);returnkey;}/// Get the loop size from a public key by guessing and checking each/// loop size until it matches.staticlongloop_size(longpub_key){for(longloop_size=0;loop_size<1000000;loop_size++){if(transform(PUB_KEY_SUBJECT,loop_size)==pub_key){returnloop_size;}}return-1;}/// Figure out what the shared final key is between door and card.longpart1(constchar*filename){RFIDHandshakekeys=parse(filename);keys.card_loop_size=loop_size(keys.card_key);if(keys.door_loop_size==-1){printf("Couldn't get door loop size.\n");exit(EXIT_FAILURE);}returntransform(keys.door_key,keys.card_loop_size);}intday25(){printf("====== Day 25 ======\n");printf("Part 1: %ld\n",part1("data/day25.txt"));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.
I'm not ashamed to say that I set this one up and let it run. It seemed like it ran more slowly than I expected, but at this point, it is what it is, and I'm good with it.