advent2015

advent of code 2015 (c++)
git clone git://bsandro.tech/advent2015
Log | Files | Refs

commit f9bce4673dd2ad4de61dad9dc7243695b7cd8f40
parent 1cdcb262ac6836d10ba27344b28be3e614d077e9
Author: bsandro <email@bsandro.tech>
Date:   Mon,  2 Dec 2024 01:01:15 +0200

day 04 p1+p2

Diffstat:
Aday04/Makefile | 28++++++++++++++++++++++++++++
Aday04/main.cpp | 43+++++++++++++++++++++++++++++++++++++++++++
2 files changed, 71 insertions(+), 0 deletions(-)

diff --git a/day04/Makefile b/day04/Makefile @@ -0,0 +1,28 @@ +NAME=$(shell basename ${PWD}) +SRC=$(wildcard *.cpp) +DEPS:=$(wildcard *.hpp) +OBJ:=$(SRC:.cpp=.o) +CXXFLAGS=-O2 -std=c++17 -Werror -Wall -Wextra -I. +LDFLAGS=-lstdc++ -lcrypto + +all: $(NAME) + +.PHONY: clean run + +clean: + rm -f $(OBJ) $(NAME) + +%.o : %.c $(DEPS) + @$(CC) $(CFLAGS) -c $< -o $@ + +$(NAME): $(OBJ) + @$(CC) $(OBJ) -o $@ $(LDFLAGS) + +run: $(NAME) + @./$(NAME) yzbqklnj + +test1: $(NAME) + @./$(NAME) abcdef + +test2: $(NAME) + @./$(NAME) pqrstuv diff --git a/day04/main.cpp b/day04/main.cpp @@ -0,0 +1,43 @@ +#include <iostream> +#include <filesystem> +#include <fstream> +#include <sstream> +#include <vector> +#include <cstring> +#include <cstdio> +#include <tuple> +#include <algorithm> +#include <openssl/md5.h> +#include <openssl/evp.h> + +std::string md5(const std::string &str){ + unsigned char hash[MD5_DIGEST_LENGTH]; + EVP_Q_digest(NULL, "MD5", NULL, str.c_str(), str.size(), hash, NULL); + std::stringstream ss; + for(int i = 0; i < MD5_DIGEST_LENGTH; i++){ + ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(hash[i]); + } + return ss.str(); +} + +int solve(const std::string input, const std::string prefix) { + for (int i=0; i<INT_MAX; ++i) { + if (md5(input+std::to_string(i)).compare(0, prefix.size(), prefix)==0) { + return i; + } + } + return 0; +} + +int main(int argc, char **argv) { + if (argc<2) { + std::cerr << "No argument string provided" << std::endl; + return -1; + } + const std::string input = argv[1]; + std::cout << "AoC 2015 day 04 " << input << std::endl; + std::cout << "part1: " << solve(input, "00000") << std::endl; + std::cout << "part2: " << solve(input, "000000") << std::endl; + + return 0; +}