GithubHelp home page GithubHelp logo

aoc-2018's Introduction

Advent of Code 2018

My solutions to the Advent of Code 2018.

Please note that this code has been written on a Linux system, using the Linux API. I've tried to catch all non-standard functions and check for them in the build, but I might have missed some. Thus, please make sure you're using a Linux system and a compiler with the GNU extensions enabled if you get missing header/symbol errors during build.

Common Module

The common/ module contains some usefull utilities for working with Advent of Code puzzles.

Tokenizer

The tokenizer utility (tokenizer.{c,h}) provides facilities to split a string among a common delimiter (\n in AoC inputs). It uses strtok_r under the hood to facilitate the actual splitting of the string.

The functions are, in short:

  • get_tokenizer: This function initializes a tokenizer for the string s with delimiter delim. This is the only way to instantiate a tokenizer. The input string is not changed.
  • n_tok Returns the next token or NULL if there are no more tokens. Note that the tokens returned by this function are only valid until free_tok or reset_tok are called on the producing tokenizer.
  • free_tok Free all internal memory and finally the tokenizer itself. Invalidates all produced tokens!
  • reset_tok Resets the tokenizer, so that the next token produced by n_tok will be the original input string's first token again
  • tok_count Returns the currently remaining number of tokens. Before the first call to n_tok, this retuns the total number of tokens in the input string. Is reset by reset_tok.

File Reading

The mm_files utility (mm_files.{c,h}) provides a facility to read a complete file into memory via the mmap function. That function is used to read the entire file. The content is then memcopied to a buffer and the file is unmapped again. The buffer is then returned to the caller.

UT supporting streams

The aoc_streams.h header provides STDOUT_STREAM and STDERR_STREAM defines for use in programs. If the UNITTESTS_ENABLED define is defined at compile time, those two defines will be set to exten FILE* variables named stdout_ut and stderr respectively. Those can then be defined in a test file and checked in UTs to make sure the program outputs what it needs to output.

Of course, for this to work the user needs to use fprintf and friends with STDERR_STREAM/STDOUT_STREAM as their file parameters instead of plain printf.

Error Handling

The aoc_err.{c,h} module provides facilities for the AoC day functions to communicate errors to the aoc_main function.

To this end, the set_aoc_err_msg function can be used to store an error message, which can then be retrieved with the get_latest_aoc_err_msg function.

Note that the get_latest_aoc_err_msg only every retrieves the latest message, as the buffer only holds a single message.

AoC Main

The aoc_main function (main.{c,h}) is the core of the AoC common code. It parses command line parameters and calls the day's AoC functions on them.

aoc_main accepts the argc and argv parameters from main, plus two functions taking a tokenizer as input and returning a dynamically allocated string containing the result.

It is called in the form aoc_main(argc, argv, func_part1, func_part2). Then it does the following:

  • Checks whether the command line parameters contain a "1" or "2" to indicate which function to call
  • Uses the mm_files module to load the AoC input file given in argv[2]
  • Initializes a tokenizer with the full file content an \n as a delimiter
  • Hands that tokenizer over to the func_part1 or func_part2 depending on whether argv[1] is "1" or "2"
  • Prints the function's result to stdout
  • Cleans up after itself

Days

Day 01

  • Directory: day_01/
  • Task: https://adventofcode.com/2018/day/1
  • Input: day_01/input.txt
  • Build: cd day_01 && mkdir build && cmake .. && make day_01
  • UT: cd day_01 && mkdir build && cmake -DUNITTESTS_ENABLED=ON .. && make check

Day 02

  • Directory: day_02/
  • Task: https://adventofcode.com/2018/day/2
  • Input: day_02/input.txt
  • Build: cd day_02 && mkdir build && cmake .. && make day_02
  • UT: cd day_02 && mkdir build && cmake -DUNITTESTS_ENABLED=ON .. && make check

Day 03

  • Directory: day_03/
  • Task: https://adventofcode.com/2018/day/3
  • Input: day_03/input.txt
  • Build: cd day_03 && mkdir build && cmake .. && make day_03
  • UT: cd day_03 && mkdir build && cmake -DUNITTESTS_ENABLED=ON .. && make check

aoc-2018's People

Contributors

mmeier86 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.