diff --git a/.editorconfig b/.editorconfig index fcd8ade1..935c9276 100644 --- a/.editorconfig +++ b/.editorconfig @@ -11,4 +11,3 @@ insert_final_newline = true [Makefile] indent_style = tab indent_size = 4 - diff --git a/.github/workflows/day-00-example.yaml b/.github/workflows/day-00-example.yaml new file mode 100644 index 00000000..cb0c3582 --- /dev/null +++ b/.github/workflows/day-00-example.yaml @@ -0,0 +1,17 @@ +name: day-00-example +on: + push: + paths: + - 'day-00-example/**' + +jobs: + test: + runs-on: ubuntu-latest + container: + image: jonasjso/adventofcode2020 + steps: + - uses: actions/checkout@v2 + - name: Print versions + run: ./scripts/print-versions.sh + - name: Run test + run: ./day-00-example/test.sh diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml deleted file mode 100644 index 39c7be86..00000000 --- a/.github/workflows/test.yaml +++ /dev/null @@ -1,28 +0,0 @@ -name: test -on: - push: - branches: - - main - paths-ignore: - - README.md - - pull_request: - branches: - - main - types: - - opened - - synchronize - - reopened - - ready_for_review - paths-ignore: - - README.md - -jobs: - test: - runs-on: ubuntu-latest - container: - image: jonasjso/adventofcode2020 - steps: - - uses: actions/checkout@v2 - - name: Print versions - run: ./scripts/print-versions.sh diff --git a/Dockerfile b/Dockerfile index f2055080..b6ebe443 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,30 @@ FROM ubuntu:latest LABEL github=https://github.com/Arxcis/adventofcode2020 -# Apt install all the things +# Configure TZ, so we don't get interactive prompt +ENV TZ=Europe/Kiev +RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone + RUN apt update && apt install -yqq\ + curl\ + unzip; + +# Tell ubuntu that we want to install node from nodesource +RUN curl -sL https://deb.nodesource.com/setup_15.x | bash - + +# Install all the things +RUN apt update && apt install -yqq\ + # python3 v3.8.6 python3\ - python-is-python3\ - ; + # golang 1.14 + golang\ + # nodejs v15.x + nodejs + + # deno v1.5.3 +RUN curl -fsSL https://deno.land/x/install/install.sh | sh\ + && mv -v /root/.deno/bin/* /bin/ +# @TODO Enable rust support rust v1.48 +#RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y\ + # && mv -v /root/.cargo/bin/* /bin/ diff --git a/Makefile b/Makefile index 318a7e52..c4945031 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ test: - echo "hello test" + ./day-00-example/test.sh dockerbuild: Dockerfile docker build . --tag jonasjso/adventofcode2020 @@ -7,6 +7,13 @@ dockerbuild: Dockerfile dockerpush: docker push jonasjso/adventofcode2020:latest +versions: + ./scripts/print-versions.sh -.PHONY: dockerbuild dockerpush test +workflows: + ./scripts/make-workflows.sh +scaffold: + ./scripts/make-scaffold.sh + +.PHONY: dockerbuild dockerpush test versions workflows scaffold diff --git a/README.md b/README.md index 650af238..8e1d50df 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,39 @@ # adventofcode2020 -Solving the 2020 edition of https://adventofcode.com/ in many languages, and test the solutions using Github CI +Solving the 2020 edition of https://adventofcode.com/ in many languages, and test the solutions using Github CI. + +## Contributing +1. Make a branch. Example: `git checkout -b jonas/day-03` +2. Write a program, in the language of your choice. Example: `vim day-03/cmd/super-optimized.py` +3. Test your program. + +```sh +cat day-03/input | python3 day-03/cmd/super-optimized.py | diff - day-03/output +``` + +4. Make sure your program is automatically tested by the Github CI, by adding a line of code to `day-03/test.sh`. +*Example line:* +```sh +$DIR/../scripts/test-py.sh $DIR "cmd/super-optimized.py" +``` + +5. Make a Pull Request to the `main` branch. +6. Get merged! + +**How are programs tested?** + +Every program gets the `input`-challenge delivered to `stdin`, and every program is expected to answer, by writing the `output`-answer to `stdout`. + +```sh +#!/bin/bash +cat input | | diff - output +``` + +**The CI does not support my favourite language. What do I do?** + +If we don't support the language you want to write in, don't worry. Make an issue on `Github`, and we will add it. + +## Status +![day-00-example](https://github.com/Arxcis/adventofcode2020/workflows/day-00-example/badge.svg) ## References - Last attempt https://github.com/Arxcis/adventofcode2018/ diff --git a/day-00-example/README.md b/day-00-example/README.md new file mode 100644 index 00000000..3dd13480 --- /dev/null +++ b/day-00-example/README.md @@ -0,0 +1,12 @@ +# day-00-example + +This example-folder sets the precedence, for how the rest of the folders should be laid out. + +## --- Part 1 --- + +Sum all numbers + +## --- Part 2 --- + +Sum all odd numbers + diff --git a/day-00-example/cmd/main.deno.ts b/day-00-example/cmd/main.deno.ts new file mode 100644 index 00000000..244eb37e --- /dev/null +++ b/day-00-example/cmd/main.deno.ts @@ -0,0 +1,16 @@ +import { readLines } from "https://deno.land/std@0.78.0/io/bufio.ts"; + +let sumAll = 0; +let sumOdd = 0; +for await (const line of readLines(Deno.stdin)) { + if (line === "") continue; + + const num = parseInt(line); + sumAll += num; + sumOdd += num % 2 === 0 ? 0 : num; +} + +// = Answer +console.log(`${sumAll}`); +console.log(`${sumOdd}`); +// ================ diff --git a/day-00-example/cmd/main.go b/day-00-example/cmd/main.go new file mode 100644 index 00000000..b5a23c06 --- /dev/null +++ b/day-00-example/cmd/main.go @@ -0,0 +1,27 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "strconv" +) + +func main() { + scanner := bufio.NewScanner(os.Stdin) + + var sumAll int64 = 0 + var sumOdd int64 = 0 + for scanner.Scan() { + line := scanner.Text() + num, _ := strconv.ParseInt(line, 10, 64) + sumAll += num + + if num%2 != 0 { + sumOdd += num + } + } + + fmt.Println(sumAll) + fmt.Println(sumOdd) +} diff --git a/day-00-example/cmd/main.node.mjs b/day-00-example/cmd/main.node.mjs new file mode 100644 index 00000000..c28e1295 --- /dev/null +++ b/day-00-example/cmd/main.node.mjs @@ -0,0 +1,19 @@ +import readline from "readline"; + +const lines = readline.createInterface({ + input: process.stdin, +}); + +let sumAll = 0; +let sumOdd = 0; +for await (const line of lines) { + const num = parseInt(line); + sumAll += num; + sumOdd += num % 2 === 0 ? 0 : num + +} + +// = Answer +console.log(`${sumAll}`) +console.log(`${sumOdd}`) +// ================ diff --git a/day-00-example/cmd/main.py b/day-00-example/cmd/main.py new file mode 100644 index 00000000..aea4ec6e --- /dev/null +++ b/day-00-example/cmd/main.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 +#-*- coding:utf-8 -*- + +import fileinput + +sum_all = 0 +sum_odd = 0 + +for line in fileinput.input(): + num = int(line) + sum_all += num + if num % 2 != 0: + sum_odd += num + +# = Answer +print(str(sum_all)) +print(str(sum_odd)) +# ================ diff --git a/day-00-example/input b/day-00-example/input new file mode 100644 index 00000000..cbfe526f --- /dev/null +++ b/day-00-example/input @@ -0,0 +1,8 @@ +1 +2 +3 +4 +8 +12 +3000 +28731 diff --git a/day-00-example/output b/day-00-example/output new file mode 100644 index 00000000..14a33409 --- /dev/null +++ b/day-00-example/output @@ -0,0 +1,2 @@ +31761 +28735 diff --git a/day-00-example/test.sh b/day-00-example/test.sh new file mode 100755 index 00000000..b80c7d46 --- /dev/null +++ b/day-00-example/test.sh @@ -0,0 +1,8 @@ +#!/bin/bash +DIR=$(dirname $(realpath $0)) + +# Run tests +$DIR/../scripts/test-deno.sh $DIR "cmd/main.deno.ts" +$DIR/../scripts/test-go.sh $DIR "cmd/main.go" +$DIR/../scripts/test-node.sh $DIR "cmd/main.node.mjs" +$DIR/../scripts/test-py.sh $DIR "cmd/main.py" \ No newline at end of file diff --git a/scripts/make-scaffold.sh b/scripts/make-scaffold.sh new file mode 100755 index 00000000..d6ef34c2 --- /dev/null +++ b/scripts/make-scaffold.sh @@ -0,0 +1,34 @@ +#!/bin/bash + +for i in {01,02} +do + mkdir -p "./day-$i/cmd" + cat > "./day-$i/cmd/.keep" << KEEP +KEEP + + cat > "./day-$i/README.md" << README +# day-$i +## --- Part 1 --- +## --- Part 2 --- +README + + cat > "./day-$i/input" << INPUT + +INPUT + + cat > "./day-$i/output" << OUTPUT + +OUTPUT + + cat > "./day-$i/test.sh" << 'TEST' +#!/bin/bash +set -e +DIR=$(dirname $(realpath $0)) + +# Run tests +# Example: $DIR/../scripts/test-deno.sh $DIR ./cmd/main.deno.ts +TEST + + chmod +x ./day-$i/test.sh +done + diff --git a/scripts/make-workflows.sh b/scripts/make-workflows.sh new file mode 100755 index 00000000..000234fc --- /dev/null +++ b/scripts/make-workflows.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +for i in {01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24} +do + cat > "./.github/workflows/day-$i.yaml" << WORKFLOW +name: day-${i} +on: +push: + paths: + - 'day-${i}/**' + +jobs: +test: + runs-on: ubuntu-latest + container: + image: jonasjso/adventofcode2020 + steps: + - uses: actions/checkout@v2 + - name: Print versions + run: ./scripts/print-versions.sh + - name: Run test + run: ./day-${i}/test.sh +WORKFLOW +done diff --git a/scripts/print-versions.sh b/scripts/print-versions.sh index 13696dc7..006a8306 100755 --- a/scripts/print-versions.sh +++ b/scripts/print-versions.sh @@ -1,6 +1,20 @@ #!/bin/bash -echo "" +echo "$ uname -a" uname -a +echo "" + +echo "$ deno --version" +deno --version +echo "" + +echo "$ go version" +go version +echo "" + +echo "$ node --version" +node --version +echo "" +echo "$ python3 --version" +python3 --version echo "" -python --version diff --git a/scripts/test-deno.sh b/scripts/test-deno.sh new file mode 100755 index 00000000..f988060d --- /dev/null +++ b/scripts/test-deno.sh @@ -0,0 +1,11 @@ +#!/bin/bash +set -e + +# Usage: ../test-deno.sh DIR CMD +# Example: ../test-deno.sh /adventofcode2020/day-03 cmd/main.deno.ts + +DIR=$1 +CMD=$2 + +cat "$DIR/input" | deno run "$DIR/$CMD" | diff - "$DIR/output" +echo "$DIR / deno run $CMD ✅" diff --git a/scripts/test-go.sh b/scripts/test-go.sh new file mode 100755 index 00000000..f7f4be1c --- /dev/null +++ b/scripts/test-go.sh @@ -0,0 +1,11 @@ +#!/bin/bash +set -e + +# Usage: ../test-go.sh DIR CMD +# Example: ../test-go.sh /adventofcode2020/day-03 cmd/main.go + +DIR=$1 +CMD=$2 + +cat "$DIR/input" | go run "$DIR/$CMD" | diff - "$DIR/output" +echo "$DIR / go run $CMD ✅" diff --git a/scripts/test-node.sh b/scripts/test-node.sh new file mode 100755 index 00000000..f894d230 --- /dev/null +++ b/scripts/test-node.sh @@ -0,0 +1,11 @@ +#!/bin/bash +set -e + +# Usage: ../test-node.sh DIR CMD +# Example: ../test-node.sh /adventofcode2020/day-03 cmd/main.node.mjs + +DIR=$1 +CMD=$2 + +cat "$DIR/input" | node "$DIR/$CMD" | diff - "$DIR/output" +echo "$DIR / node $CMD ✅" diff --git a/scripts/test-py.sh b/scripts/test-py.sh new file mode 100755 index 00000000..64000a57 --- /dev/null +++ b/scripts/test-py.sh @@ -0,0 +1,11 @@ +#!/bin/bash +set -e + +# Usage: ../test-py.sh DIR CMD +# Example: ../test-py.sh /adventofcode2020/day-03 cmd/main.py + +DIR=$1 +CMD=$2 + +cat "$DIR/input" | python3 "$DIR/$CMD" | diff - "$DIR/output" +echo "$DIR / python3 $CMD ✅"