Giter VIP home page Giter VIP logo

advent-of-code-2022's Introduction

My Advent of Code Solutions

Why Bash?

Many people claim that Bash is not a good scripting language. Although, it is available on any Unix system and essential for many CI/CD pipelines. Since it is my favorite scripting language, I took the challenge and implemented most Advent of Code exercises in Bash.

As a matter of fact, Bash has some limitations e.g., no floating point numbers, no multi-dimensional arrays and no sets. Moreover, there are countless pitfalls, which can lead to terrible mistakes.

However, there are some aspects, where Bash really shines e.g., string manipulation, command substitution as well as builtins for reading files to arrays, and more. Additionally, tools like grep, sed, cut or paste enable concise Bash scripts where general purpose programming languages require multiple statements.

For example, the first exercise implemented in Java would be something like:

int max = 0, curr = 0;
for (var l : Files.readAllLines(Paths.get("input.txt"))) {
    if (l.isEmpty()) {
        max = Math.max(max, curr);
        curr = 0;
    } else {
        curr += Integer.parseInt(l);
    }
}
System.out.println(max);

The same can be achieved in Bash with the following code:

paste -sd + input.txt | sed -E 's/\+\+/\n/g' | bc | sort -r | head -n 1 

At the first glance, this seems to be a cryptic command. However, it's pretty straightforward if you break it down piece by piece.

paste -sd + input.txt |  # concatenate all lines into a single line, replace '\n' with '+'
  sed -E 's/\+\+/\n/g' | # occurrences of "++" means there was an empty line => split them again
  bc |                   # now we have lines like "1+2+3", bc sums up the values in each line
  sort -r |              # sort the resulting sums in descending order
  head -n 1              # pick the first one - the maximum value

Test-Driven Development (TDD)

Besides implementing the exercises, I intended to apply good engineering practices such as TDD. For some exercises, I used bash_unit to write assertions before writing code. You might know bats, which was the most popular test framework for Bash scripts. bash_unit is an amazing framework, which can even fake external commands to provide the desired output. Moreover, bash_unit tests are ordinary shell scripts, which makes it easy to write tests and maintain them.

Status

advent-of-code-2022's People

Contributors

gschauer avatar

Watchers

 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.