From 404a2e41c27fa1022e7294de5cf1e6dcfb03250b Mon Sep 17 00:00:00 2001 From: Jonas Johan Solsvik Date: Sat, 5 Dec 2020 19:14:10 +0100 Subject: [PATCH 1/9] add deno language with example --- Dockerfile | 7 +++++-- README.md | 1 + days/day-00-example/solutions/example.ts | 14 ++++++++++++++ days/day-00-example/test.sh | 5 +++-- languages/deno.sh | 18 ++++++++++++++++++ scripts/print-versions.sh | 4 ++++ 6 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 days/day-00-example/solutions/example.ts create mode 100755 languages/deno.sh diff --git a/Dockerfile b/Dockerfile index 0817ba32..f465882d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,7 +8,7 @@ RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone # 2. Build things from source first, so get build deps installed RUN apt-get update && apt-get install -yqq --no-install-recommends\ # build-essential includes `make`, `gcc` and `g++` - build-essential git wget ca-certificates curl + build-essential git wget ca-certificates curl unzip # 3. Install zig compiler, from ziglang.org RUN cd /opt && wget https://ziglang.org/download/0.7.0/zig-linux-x86_64-0.7.0.tar.xz\ @@ -25,7 +25,10 @@ RUN git clone https://github.com/polyml/polyml.git /tmp/polyml && cd /tmp/polyml # 5. Tell apt to install node.js from nodesource.com, to get v15.x instead of v12.x RUN curl -sL https://deb.nodesource.com/setup_15.x | bash - -# 6. Install all other compilers, from apt-get +# 6. Install deno.js run-time from deno.land +RUN curl -fsSL https://deno.land/x/install/install.sh | sh && cp ~/.deno/bin/deno /bin/ + +# 7. Install all other compilers, from apt-get RUN apt-get update && apt-get install -yqq --no-install-recommends\ default-jdk golang nodejs php-cli python3 ruby rustc \ # Cleanup what we don't need diff --git a/README.md b/README.md index 01aa99ec..a6285d20 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,7 @@ If you enjoy working on this project, consider sharing it with your friends. The | java | javac Main.java && java Main | 0 / 25 | | node | node --harmony-top-level-await main.mjs | 0 / 25 | | php | php main.php | 0 / 25 | +| deno | deno run -q main.ts | 0 / 25 | *List of programming languages supported by our [Dockerfile](./Dockerfile)* diff --git a/days/day-00-example/solutions/example.ts b/days/day-00-example/solutions/example.ts new file mode 100644 index 00000000..e76f765c --- /dev/null +++ b/days/day-00-example/solutions/example.ts @@ -0,0 +1,14 @@ +import { readLines } from "https://deno.land/std@0.79.0/io/bufio.ts"; + +let sum_all = 0; +let sum_odd = 0; + +for await (const line of readLines(Deno.stdin)) { + const num = parseInt(line); + if (isNaN(num)) continue; + sum_all += num; + sum_odd += num % 2 == 1 ? num : 0; +} + +console.log(`${sum_all}`); +console.log(`${sum_odd}`); diff --git a/days/day-00-example/test.sh b/days/day-00-example/test.sh index 55e88005..a549c93e 100755 --- a/days/day-00-example/test.sh +++ b/days/day-00-example/test.sh @@ -9,11 +9,12 @@ $D/../../languages/rust.sh $D/input.txt $D/output.txt $D/solutions/example.rs $D/../../languages/go.sh $D/input.txt $D/output.txt $D/solutions/example.go $D/../../languages/c.sh $D/input.txt $D/output.txt $D/solutions/example.c $D/../../languages/cpp.sh $D/input.txt $D/output.txt $D/solutions/example.cpp -$D/../../languages/sml.sh $D/input.txt $D/output.txt $D/solutions/example.sml $D/../../languages/bash.sh $D/input.txt $D/output.txt $D/solutions/example.bash $D/../../languages/php.sh $D/input.txt $D/output.txt $D/solutions/example.php $D/../../languages/python.sh $D/input.txt $D/output.txt $D/solutions/example.py -$D/../../languages/ruby.sh $D/input.txt $D/output.txt $D/solutions/example.rb +$D/../../languages/deno.sh $D/input.txt $D/output.txt $D/solutions/example.ts $D/../../languages/node.sh $D/input.txt $D/output.txt $D/solutions/example.mjs +$D/../../languages/ruby.sh $D/input.txt $D/output.txt $D/solutions/example.rb $D/../../languages/java.sh $D/input.txt $D/output.txt $D/solutions Example +$D/../../languages/sml.sh $D/input.txt $D/output.txt $D/solutions/example.sml echo "" diff --git a/languages/deno.sh b/languages/deno.sh new file mode 100755 index 00000000..fe3cc5ab --- /dev/null +++ b/languages/deno.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +set -euo pipefail + +# Usage: ../../languages/deno.sh INPUT OUTPUT SOLUTION +# Example: ../../languages/deno.sh input.txt output.txt solutions/day03.ts + +INPUT="$1" +OUTPUT="$2" +SOLUTION="$3" + +start=$(($(date +%s%N)/1000000)) +cat $INPUT | deno run -q $SOLUTION | diff - $OUTPUT +end=$(($(date +%s%N)/1000000)) + +TIME="$(expr $end - $start)" + +D=$(dirname $(realpath $0)) +$D/../scripts/print-test.sh "deno" "$TIME" "$SOLUTION" diff --git a/scripts/print-versions.sh b/scripts/print-versions.sh index 20347745..c30955cc 100755 --- a/scripts/print-versions.sh +++ b/scripts/print-versions.sh @@ -9,6 +9,10 @@ echo "$ bash --version" bash --version echo "" +echo "$ deno --version" +deno --version +echo "" + echo "$ gcc version" gcc --version From f6ba57de5d4ee285f319544e0973fd905f6674a3 Mon Sep 17 00:00:00 2001 From: Jonas Johan Solsvik Date: Mon, 7 Dec 2020 21:06:06 +0100 Subject: [PATCH 2/9] solve day01 in deno --- days/day-00-example/solutions/example.ts | 8 ++++---- days/day-00-example/test.sh | 2 +- days/day-01/solutions/day01.ts | 22 ++++++++++++++++++++++ days/day-01/test.sh | 1 + languages/deno.sh | 4 +++- 5 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 days/day-01/solutions/day01.ts diff --git a/days/day-00-example/solutions/example.ts b/days/day-00-example/solutions/example.ts index e76f765c..1398fbd1 100644 --- a/days/day-00-example/solutions/example.ts +++ b/days/day-00-example/solutions/example.ts @@ -1,14 +1,14 @@ -import { readLines } from "https://deno.land/std@0.79.0/io/bufio.ts"; +const file = (await Deno.readTextFile('/dev/stdin')) + .split("\n") + .slice(0, -1) let sum_all = 0; let sum_odd = 0; -for await (const line of readLines(Deno.stdin)) { +for (const line of file) { const num = parseInt(line); - if (isNaN(num)) continue; sum_all += num; sum_odd += num % 2 == 1 ? num : 0; } - console.log(`${sum_all}`); console.log(`${sum_odd}`); diff --git a/days/day-00-example/test.sh b/days/day-00-example/test.sh index a549c93e..a199fe2f 100755 --- a/days/day-00-example/test.sh +++ b/days/day-00-example/test.sh @@ -9,6 +9,7 @@ $D/../../languages/rust.sh $D/input.txt $D/output.txt $D/solutions/example.rs $D/../../languages/go.sh $D/input.txt $D/output.txt $D/solutions/example.go $D/../../languages/c.sh $D/input.txt $D/output.txt $D/solutions/example.c $D/../../languages/cpp.sh $D/input.txt $D/output.txt $D/solutions/example.cpp +$D/../../languages/sml.sh $D/input.txt $D/output.txt $D/solutions/example.sml $D/../../languages/bash.sh $D/input.txt $D/output.txt $D/solutions/example.bash $D/../../languages/php.sh $D/input.txt $D/output.txt $D/solutions/example.php $D/../../languages/python.sh $D/input.txt $D/output.txt $D/solutions/example.py @@ -16,5 +17,4 @@ $D/../../languages/deno.sh $D/input.txt $D/output.txt $D/solutions/example.ts $D/../../languages/node.sh $D/input.txt $D/output.txt $D/solutions/example.mjs $D/../../languages/ruby.sh $D/input.txt $D/output.txt $D/solutions/example.rb $D/../../languages/java.sh $D/input.txt $D/output.txt $D/solutions Example -$D/../../languages/sml.sh $D/input.txt $D/output.txt $D/solutions/example.sml echo "" diff --git a/days/day-01/solutions/day01.ts b/days/day-01/solutions/day01.ts new file mode 100644 index 00000000..20834cfc --- /dev/null +++ b/days/day-01/solutions/day01.ts @@ -0,0 +1,22 @@ +const numbers = (await Deno.readTextFile('/dev/stdin')) + .split("\n") + .slice(0, -1) + .map((line: string) => parseInt(line)) + +let part1 = 0, part2 = 0; + +for (const i of numbers) { + for (const j of numbers) { + if (!part1 && i + j === 2020) { + part1 = i * j; + } + for (const k of numbers) { + if (!part2 && i + j + k === 2020) { + part2 = i * j * k; + } + } + } +} + +console.log(`${part1}`) +console.log(`${part2}`) diff --git a/days/day-01/test.sh b/days/day-01/test.sh index 1f018130..27798b54 100755 --- a/days/day-01/test.sh +++ b/days/day-01/test.sh @@ -8,6 +8,7 @@ echo "--- Day 1: Report Repair ---" $D/../../languages/go.sh $D/input.txt $D/output.txt $D/solutions/day01.go $D/../../languages/c.sh $D/input.txt $D/output.txt $D/solutions/day01.c $D/../../languages/sml.sh $D/input.txt $D/output.txt $D/solutions/day01.sml +$D/../../languages/deno.sh $D/input.txt $D/output.txt $D/solutions/day01.ts $D/../../languages/python.sh $D/input.txt $D/output.txt $D/solutions/one-liner.py $D/../../languages/cpp.sh $D/input.txt $D/output.txt $D/solutions/day01.cpp $D/../../languages/python.sh $D/input.txt $D/output.txt $D/solutions/day01.py diff --git a/languages/deno.sh b/languages/deno.sh index fe3cc5ab..97a0a1cf 100755 --- a/languages/deno.sh +++ b/languages/deno.sh @@ -8,8 +8,10 @@ INPUT="$1" OUTPUT="$2" SOLUTION="$3" +deno install -f --quiet "$SOLUTION" >/dev/null + start=$(($(date +%s%N)/1000000)) -cat $INPUT | deno run -q $SOLUTION | diff - $OUTPUT +cat $INPUT | deno run --allow-read $SOLUTION | diff - $OUTPUT end=$(($(date +%s%N)/1000000)) TIME="$(expr $end - $start)" From 6e2c85c1c00a02eea9b173d631d0279ad892d37d Mon Sep 17 00:00:00 2001 From: Jonas Johan Solsvik Date: Mon, 7 Dec 2020 21:28:56 +0100 Subject: [PATCH 3/9] solve day02 in deno --- days/day-02/solutions/day02.c | 2 ++ days/day-02/solutions/day02.ts | 32 ++++++++++++++++++++++++++++++++ days/day-02/test.sh | 1 + 3 files changed, 35 insertions(+) create mode 100644 days/day-02/solutions/day02.ts diff --git a/days/day-02/solutions/day02.c b/days/day-02/solutions/day02.c index 7aab5e22..279c7d39 100644 --- a/days/day-02/solutions/day02.c +++ b/days/day-02/solutions/day02.c @@ -20,10 +20,12 @@ int main() { } } + // --- Part1 --- if (letter_occurances >= from && letter_occurances <= to) { valid_passwords += 1; } + // --- Part2 --- int letter_occurances_part2 = 0; if (password[from-1] == letter) { letter_occurances_part2 += 1; diff --git a/days/day-02/solutions/day02.ts b/days/day-02/solutions/day02.ts new file mode 100644 index 00000000..ea9a6718 --- /dev/null +++ b/days/day-02/solutions/day02.ts @@ -0,0 +1,32 @@ +const passwords = [...(await Deno.readTextFile('/dev/stdin')) + .matchAll(/(\d+)-(\d+) (\w): (\w+)/g)] + .map(([,from, to, letter, password]) => ({ + from: parseInt(from), + to: parseInt(to), + letter, + password + })) + + +// --- part1 --- +const part1_passwords = passwords + .reduce((count, {from, to, letter, password}) => { + + const letterCount = [...password] + .reduce((letterCount, passwordLetter) => letterCount + (letter === passwordLetter ? 1:0), 0) + + return count + (from <= letterCount && letterCount <= to ? 1:0); + }, 0) + +const part2_passwords = passwords + .reduce((count, {from, to, letter, password}) => { + + let letterCount = 0; + letterCount += (password.charAt(from-1) === letter ? 1:0); + letterCount += (password.charAt(to-1) === letter ? 1:0); + + return count + (letterCount === 1 ? 1:0); + }, 0); + +console.log(`${part1_passwords}`); +console.log(`${part2_passwords}`); diff --git a/days/day-02/test.sh b/days/day-02/test.sh index 405781b1..43aab6ba 100755 --- a/days/day-02/test.sh +++ b/days/day-02/test.sh @@ -12,4 +12,5 @@ $D/../../languages/cpp.sh $D/input.txt $D/output.txt $D/solutions/day02.cpp $D/../../languages/sml.sh $D/input.txt $D/output.txt $D/solutions/day02.sml $D/../../languages/go.sh $D/input.txt $D/output.txt $D/solutions/day02.tholok97.go $D/../../languages/python.sh $D/input.txt $D/output.txt $D/solutions/day02.py +$D/../../languages/deno.sh $D/input.txt $D/output.txt $D/solutions/day02.ts echo "" From 6f2c84af04a552399c7f90761f4c436158beb813 Mon Sep 17 00:00:00 2001 From: Jonas Johan Solsvik Date: Mon, 7 Dec 2020 21:43:59 +0100 Subject: [PATCH 4/9] solve day03 in deno --- days/day-03/solutions/day03.ts | 42 ++++++++++++++++++++++++++++++++++ days/day-03/test.sh | 1 + 2 files changed, 43 insertions(+) create mode 100644 days/day-03/solutions/day03.ts diff --git a/days/day-03/solutions/day03.ts b/days/day-03/solutions/day03.ts new file mode 100644 index 00000000..c55a9ec5 --- /dev/null +++ b/days/day-03/solutions/day03.ts @@ -0,0 +1,42 @@ +const lines = [...(await Deno.readTextFile('/dev/stdin')) + .matchAll(/[.#]+/g)] + .map(([line])=> line); + +/** @assumption: every line has the same width */ +const LINE_WIDTH = lines[0].length + +const toboggans = [ + { x: 0, y: 0, right: 3, down: 1, treesHit: 0}, + { x: 0, y: 0, right: 1, down: 1, treesHit: 0}, + { x: 0, y: 0, right: 5, down: 1, treesHit: 0}, + { x: 0, y: 0, right: 7, down: 1, treesHit: 0}, + { x: 0, y: 0, right: 1, down: 2, treesHit: 0}, +] + +const FREE = "."; +const TREE = "#"; + +for (const toboggan of toboggans) { + // Move toboggan + toboggan.x = (toboggan.x + toboggan.right) % LINE_WIDTH; + toboggan.y = toboggan.down; + + while (toboggan.y < lines.length) { + const line = lines[toboggan.y]; + + // Check hit? + toboggan.treesHit += (line.charAt(toboggan.x) === TREE ? 1:0); + + toboggan.x = (toboggan.x + toboggan.right) % LINE_WIDTH; + toboggan.y = toboggan.y + toboggan.down; + } +} + + +let treeProduct = 1; +for (const { treesHit } of toboggans) { + treeProduct *= treesHit; +} + +console.log(`${toboggans[0].treesHit}`) +console.log(`${treeProduct}`) diff --git a/days/day-03/test.sh b/days/day-03/test.sh index e22dff49..cd45467f 100755 --- a/days/day-03/test.sh +++ b/days/day-03/test.sh @@ -12,4 +12,5 @@ $D/../../languages/sml.sh $D/input.txt $D/output.txt $D/solutions/day03.sml $D/../../languages/python.sh $D/input.txt $D/output.txt $D/solutions/day03.preng.py $D/../../languages/python.sh $D/input.txt $D/output.txt $D/solutions/one-liner.py $D/../../languages/python.sh $D/input.txt $D/output.txt $D/solutions/day03.klyve.py +$D/../../languages/deno.sh $D/input.txt $D/output.txt $D/solutions/day03.ts echo "" From 529cf915d9441342ff2e718da408db767760b802 Mon Sep 17 00:00:00 2001 From: Jonas Johan Solsvik Date: Mon, 7 Dec 2020 22:01:08 +0100 Subject: [PATCH 5/9] solve day04 in deno --- days/day-04/solutions/day04.ts | 81 ++++++++++++++++++++++++++++++++++ days/day-04/test.sh | 1 + 2 files changed, 82 insertions(+) create mode 100644 days/day-04/solutions/day04.ts diff --git a/days/day-04/solutions/day04.ts b/days/day-04/solutions/day04.ts new file mode 100644 index 00000000..0c458cc8 --- /dev/null +++ b/days/day-04/solutions/day04.ts @@ -0,0 +1,81 @@ +const lines = await Deno.readTextFile('/dev/stdin') + +const passportStrings = [...lines.matchAll(/\S+([ \n][\S]+)*/g)] + .map(([match]) => match); + +const passportTokens = passportStrings + .map(pass => pass.replaceAll(/\n/g, " ").split(" ")) + +const passportKeyValues = passportTokens + .map(tokens => tokens.map((token: string) => token.split(":"))) + +const REQUIRED_VALIDATORS = { + // byr (Birth Year) - four digits; at least 1920 and at most 2002. + byr: (value: string) => !!value.match(/[\d]{4}/) + && 1920 <= parseInt(value) && parseInt(value) <= 2002 + , + + // iyr (Issue Year) - four digits; at least 2010 and at most 2020. + iyr: (value: string) => !!value.match(/[\d]{4}/) + && 2010 <= parseInt(value) && parseInt(value) <= 2020 + , + + // eyr (Expiration Year) - four digits; at least 2020 and at most 2030. + eyr: (value: string) => !!value.match(/[\d]{4}/) + && 2020 <= parseInt(value) && parseInt(value) <= 2030 + , + + // hgt (Height) - a number followed by either cm or in: + // If cm, the number must be at least 150 and at most 193. + // If in, the number must be at least 59 and at most 76. + hgt: (value: string) => { + const [,num, unit] = value.match(/(\d\d\d?)(in|cm)/) ?? []; + if (!num) return false; + + const number = parseInt(num); + + return (unit === "cm" && 150 <= number && number <= 193) + || (unit === "in" && 59 <= number && number <= 76); + }, + + // hcl (Hair Color) - a # followed by exactly six characters 0-9 or a-f. + hcl: (value: string) => !!value.match(/#[a-z0-9]{6}/), + + // ecl (Eye Color) - exactly one of: amb blu brn gry grn hzl oth. + ecl: (value: string) => !!value.match(/amb|blu|brn|gry|grn|hzl|oth/), + + // pid (Passport ID) - a nine-digit number, including leading zeroes. + pid: (value: string) => value.length === 9 && !!value.match(/[0]*[1-9][0-9]*/), +} as { + [key: string]: (value: string) => boolean +} + +const REQUIRED_KEYS = Object.keys(REQUIRED_VALIDATORS); + +const passportRequiredKeyValues = passportKeyValues + .map(keyValues => keyValues + .filter(([key]) => REQUIRED_KEYS.includes(key))) + +const [part1, part2] = passportRequiredKeyValues + .reduce(([part1, part2], requiredKeyValues) => { + + const keys = requiredKeyValues + .map(([key]) => key) + + const keySet = [...new Set(keys)]; + + const passportKeysAreValid = REQUIRED_KEYS + .every(key => keySet.includes(key)); + + const passportValuesAreValid = passportKeysAreValid && + requiredKeyValues + .every(([key, value]) => REQUIRED_VALIDATORS[key](value)); + + return [ + part1 + (passportKeysAreValid ? 1:0), + part2 + (passportValuesAreValid ? 1:0) + ]; + }, [0,0]) + +console.log(`${part1}`); +console.log(`${part2}`); diff --git a/days/day-04/test.sh b/days/day-04/test.sh index 8825cae3..5a975fee 100755 --- a/days/day-04/test.sh +++ b/days/day-04/test.sh @@ -12,6 +12,7 @@ $D/../../languages/python.sh $D/input.txt $D/output.txt $D/solutions/day04.klyve $D/../../languages/python.sh $D/input.txt $D/output.txt $D/solutions/day04.preng.py $D/../../languages/python.sh $D/input.txt $D/output.txt $D/solutions/day04.jorgen.py $D/../../languages/python.sh $D/input.txt $D/output.txt $D/solutions/day04.stektpotet.py +$D/../../languages/deno.sh $D/input.txt $D/output.txt $D/solutions/day04.ts $D/../../languages/node.sh $D/input.txt $D/output.txt $D/solutions/day04.mjs $D/../../languages/ruby.sh $D/input.txt $D/output.txt $D/solutions/day04.klyve.rb echo From 09f687fa125de45b6648fe9e5d89127fe7270a11 Mon Sep 17 00:00:00 2001 From: Jonas Johan Solsvik Date: Mon, 7 Dec 2020 22:26:46 +0100 Subject: [PATCH 6/9] solve day05 in deno --- days/day-05/solutions/day05.ts | 22 ++++++++++++++++++++++ days/day-05/test.sh | 1 + 2 files changed, 23 insertions(+) create mode 100644 days/day-05/solutions/day05.ts diff --git a/days/day-05/solutions/day05.ts b/days/day-05/solutions/day05.ts new file mode 100644 index 00000000..764a96f7 --- /dev/null +++ b/days/day-05/solutions/day05.ts @@ -0,0 +1,22 @@ +const lines = [...(await Deno.readTextFile('/dev/stdin')) + .matchAll(/[BFLR]+/g)] + .map(([line]) => line) + +let ids = lines + .map(line => [...line] + .reverse() + .reduce((acc, char, i) => acc | (char === "B" || char === "R" ? (1 << i) : 0) + , 0) +) + +// --- part1 --- +const maxSeat = ids.reduce((acc, id) => Math.max(acc, id), -Infinity) + +// --- part2 --- +const emptySeat = ids + .sort((a, b) => a - b) + .find((id, i) => i > 0 && id !== (ids[i-1] + 1)) + ?? 0 + +console.log(`${maxSeat}`) +console.log(`${emptySeat - 1}`) // Why -1? diff --git a/days/day-05/test.sh b/days/day-05/test.sh index 5a87b5c3..c6bc802c 100755 --- a/days/day-05/test.sh +++ b/days/day-05/test.sh @@ -13,4 +13,5 @@ $D/../../languages/python.sh $D/input.txt $D/output.txt $D/solutions/day05.stekt $D/../../languages/python.sh $D/input.txt $D/output.txt $D/solutions/day05.preng.py $D/../../languages/python.sh $D/input.txt $D/output.txt $D/solutions/one-liner.stektpotet.py $D/../../languages/python.sh $D/input.txt $D/output.txt $D/solutions/day05.klyve.py +$D/../../languages/deno.sh $D/input.txt $D/output.txt $D/solutions/day05.ts echo "" From 12d5ecf91fce34a4db20e13ec30dad737aed756b Mon Sep 17 00:00:00 2001 From: Jonas Johan Solsvik Date: Mon, 7 Dec 2020 22:31:40 +0100 Subject: [PATCH 7/9] solve day06 in deno --- days/day-06/solutions/day06.mjs | 2 +- days/day-06/solutions/day06.ts | 28 ++++++++++++++++++++++++++++ days/day-06/test.sh | 1 + 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 days/day-06/solutions/day06.ts diff --git a/days/day-06/solutions/day06.mjs b/days/day-06/solutions/day06.mjs index f1dde29a..24b58ef0 100644 --- a/days/day-06/solutions/day06.mjs +++ b/days/day-06/solutions/day06.mjs @@ -13,7 +13,7 @@ const groupsNoWhitespace = groups const anyoneAnsweredYes = groupsNoWhitespace .reduce((count, group) => count - + (new Set( group.split("") )).size, 0) + + (new Set( [...group] )).size, 0) // -- part 2 -- const groupsSplitByWhitespace = groups diff --git a/days/day-06/solutions/day06.ts b/days/day-06/solutions/day06.ts new file mode 100644 index 00000000..4b29723f --- /dev/null +++ b/days/day-06/solutions/day06.ts @@ -0,0 +1,28 @@ +const lines = (await Deno.readTextFile('/dev/stdin')) + +const groups = [...lines.matchAll(/[a-z]+([\s][a-z]+)*/g)] + .map(([match]) => match); + +// -- Part 1 --- +const groupsNoWhitespace = groups + .map(group => group.replaceAll(/[\s]/g, "")); + +const anyoneAnsweredYes = groupsNoWhitespace + .reduce((count, group) => count + + (new Set( [...group] )).size, 0) + +// -- part 2 -- +const groupsSplitByWhitespace = groups + .map(group => group.split(/[\s]/)) + +const alphabet = [..."abcdefghijklmnopqrstuvwxyz"]; + +const everyoneAnsweredYes = groupsSplitByWhitespace + .reduce((count, group) => count + alphabet + .reduce((count, char) => count + + (group.every(answer => answer.includes(char)) ? 1:0), + 0), + 0) + +console.log(`${anyoneAnsweredYes}`) +console.log(`${everyoneAnsweredYes}`) diff --git a/days/day-06/test.sh b/days/day-06/test.sh index b9ec50a0..31674aaf 100755 --- a/days/day-06/test.sh +++ b/days/day-06/test.sh @@ -12,5 +12,6 @@ $D/../../languages/python.sh $D/input.txt $D/output.txt $D/solutions/one-liner.s $D/../../languages/sml.sh $D/input.txt $D/output.txt $D/solutions/day06.sml $D/../../languages/python.sh $D/input.txt $D/output.txt $D/solutions/day06.stektpotet.py $D/../../languages/python.sh $D/input.txt $D/output.txt $D/solutions/day06.preng.py +$D/../../languages/deno.sh $D/input.txt $D/output.txt $D/solutions/day06.ts $D/../../languages/node.sh $D/input.txt $D/output.txt $D/solutions/day06.mjs echo "" From 854185a0bb3a430608b25695c60cdfec6e997854 Mon Sep 17 00:00:00 2001 From: Jonas Johan Solsvik Date: Mon, 7 Dec 2020 22:53:31 +0100 Subject: [PATCH 8/9] Makefile / update dockertag --- .github/workflows/day-01.yaml | 2 +- .github/workflows/day-02.yaml | 2 +- .github/workflows/day-03.yaml | 2 +- .github/workflows/day-04.yaml | 2 +- .github/workflows/day-05.yaml | 2 +- .github/workflows/day-06.yaml | 2 +- .github/workflows/day-07.yaml | 2 +- .github/workflows/day-08.yaml | 2 +- .github/workflows/day-09.yaml | 2 +- .github/workflows/day-10.yaml | 2 +- .github/workflows/day-11.yaml | 2 +- .github/workflows/day-12.yaml | 2 +- .github/workflows/day-13.yaml | 2 +- .github/workflows/day-14.yaml | 2 +- .github/workflows/day-15.yaml | 2 +- .github/workflows/day-16.yaml | 2 +- .github/workflows/day-17.yaml | 2 +- .github/workflows/day-18.yaml | 2 +- .github/workflows/day-19.yaml | 2 +- .github/workflows/day-20.yaml | 2 +- .github/workflows/day-21.yaml | 2 +- .github/workflows/day-22.yaml | 2 +- .github/workflows/day-23.yaml | 2 +- .github/workflows/day-24.yaml | 2 +- .github/workflows/day-25.yaml | 2 +- Makefile | 2 +- 26 files changed, 26 insertions(+), 26 deletions(-) diff --git a/.github/workflows/day-01.yaml b/.github/workflows/day-01.yaml index b730a67d..6dbd06e1 100644 --- a/.github/workflows/day-01.yaml +++ b/.github/workflows/day-01.yaml @@ -16,7 +16,7 @@ jobs: test: runs-on: ubuntu-latest container: - image: "jonasjso/adventofcode2020:2020-12-05-with-node15" + image: "jonasjso/adventofcode2020:2020-12-07-with-deno" steps: - uses: actions/checkout@v2 - name: make versions diff --git a/.github/workflows/day-02.yaml b/.github/workflows/day-02.yaml index 281993c1..67b8fe74 100644 --- a/.github/workflows/day-02.yaml +++ b/.github/workflows/day-02.yaml @@ -16,7 +16,7 @@ jobs: test: runs-on: ubuntu-latest container: - image: "jonasjso/adventofcode2020:2020-12-05-with-node15" + image: "jonasjso/adventofcode2020:2020-12-07-with-deno" steps: - uses: actions/checkout@v2 - name: make versions diff --git a/.github/workflows/day-03.yaml b/.github/workflows/day-03.yaml index 3ba37f0f..8338c6d9 100644 --- a/.github/workflows/day-03.yaml +++ b/.github/workflows/day-03.yaml @@ -16,7 +16,7 @@ jobs: test: runs-on: ubuntu-latest container: - image: "jonasjso/adventofcode2020:2020-12-05-with-node15" + image: "jonasjso/adventofcode2020:2020-12-07-with-deno" steps: - uses: actions/checkout@v2 - name: make versions diff --git a/.github/workflows/day-04.yaml b/.github/workflows/day-04.yaml index 83660ddf..46166ddd 100644 --- a/.github/workflows/day-04.yaml +++ b/.github/workflows/day-04.yaml @@ -16,7 +16,7 @@ jobs: test: runs-on: ubuntu-latest container: - image: "jonasjso/adventofcode2020:2020-12-05-with-node15" + image: "jonasjso/adventofcode2020:2020-12-07-with-deno" steps: - uses: actions/checkout@v2 - name: make versions diff --git a/.github/workflows/day-05.yaml b/.github/workflows/day-05.yaml index 9c2226f3..f6df224d 100644 --- a/.github/workflows/day-05.yaml +++ b/.github/workflows/day-05.yaml @@ -16,7 +16,7 @@ jobs: test: runs-on: ubuntu-latest container: - image: "jonasjso/adventofcode2020:2020-12-05-with-node15" + image: "jonasjso/adventofcode2020:2020-12-07-with-deno" steps: - uses: actions/checkout@v2 - name: make versions diff --git a/.github/workflows/day-06.yaml b/.github/workflows/day-06.yaml index 0d890419..4677e030 100644 --- a/.github/workflows/day-06.yaml +++ b/.github/workflows/day-06.yaml @@ -16,7 +16,7 @@ jobs: test: runs-on: ubuntu-latest container: - image: "jonasjso/adventofcode2020:2020-12-05-with-node15" + image: "jonasjso/adventofcode2020:2020-12-07-with-deno" steps: - uses: actions/checkout@v2 - name: make versions diff --git a/.github/workflows/day-07.yaml b/.github/workflows/day-07.yaml index 7df7ff5d..0a47afb0 100644 --- a/.github/workflows/day-07.yaml +++ b/.github/workflows/day-07.yaml @@ -16,7 +16,7 @@ jobs: test: runs-on: ubuntu-latest container: - image: "jonasjso/adventofcode2020:2020-12-05-with-node15" + image: "jonasjso/adventofcode2020:2020-12-07-with-deno" steps: - uses: actions/checkout@v2 - name: make versions diff --git a/.github/workflows/day-08.yaml b/.github/workflows/day-08.yaml index cfd6194f..63232bce 100644 --- a/.github/workflows/day-08.yaml +++ b/.github/workflows/day-08.yaml @@ -16,7 +16,7 @@ jobs: test: runs-on: ubuntu-latest container: - image: "jonasjso/adventofcode2020:2020-12-05-with-node15" + image: "jonasjso/adventofcode2020:2020-12-07-with-deno" steps: - uses: actions/checkout@v2 - name: make versions diff --git a/.github/workflows/day-09.yaml b/.github/workflows/day-09.yaml index 8466783c..4f138559 100644 --- a/.github/workflows/day-09.yaml +++ b/.github/workflows/day-09.yaml @@ -16,7 +16,7 @@ jobs: test: runs-on: ubuntu-latest container: - image: "jonasjso/adventofcode2020:2020-12-05-with-node15" + image: "jonasjso/adventofcode2020:2020-12-07-with-deno" steps: - uses: actions/checkout@v2 - name: make versions diff --git a/.github/workflows/day-10.yaml b/.github/workflows/day-10.yaml index 639616ac..756f5730 100644 --- a/.github/workflows/day-10.yaml +++ b/.github/workflows/day-10.yaml @@ -16,7 +16,7 @@ jobs: test: runs-on: ubuntu-latest container: - image: "jonasjso/adventofcode2020:2020-12-05-with-node15" + image: "jonasjso/adventofcode2020:2020-12-07-with-deno" steps: - uses: actions/checkout@v2 - name: make versions diff --git a/.github/workflows/day-11.yaml b/.github/workflows/day-11.yaml index 7ffa2cda..aa4be83c 100644 --- a/.github/workflows/day-11.yaml +++ b/.github/workflows/day-11.yaml @@ -16,7 +16,7 @@ jobs: test: runs-on: ubuntu-latest container: - image: "jonasjso/adventofcode2020:2020-12-05-with-node15" + image: "jonasjso/adventofcode2020:2020-12-07-with-deno" steps: - uses: actions/checkout@v2 - name: make versions diff --git a/.github/workflows/day-12.yaml b/.github/workflows/day-12.yaml index 551bcbd6..1ddb9d47 100644 --- a/.github/workflows/day-12.yaml +++ b/.github/workflows/day-12.yaml @@ -16,7 +16,7 @@ jobs: test: runs-on: ubuntu-latest container: - image: "jonasjso/adventofcode2020:2020-12-05-with-node15" + image: "jonasjso/adventofcode2020:2020-12-07-with-deno" steps: - uses: actions/checkout@v2 - name: make versions diff --git a/.github/workflows/day-13.yaml b/.github/workflows/day-13.yaml index 89b51dc7..e791eca9 100644 --- a/.github/workflows/day-13.yaml +++ b/.github/workflows/day-13.yaml @@ -16,7 +16,7 @@ jobs: test: runs-on: ubuntu-latest container: - image: "jonasjso/adventofcode2020:2020-12-05-with-node15" + image: "jonasjso/adventofcode2020:2020-12-07-with-deno" steps: - uses: actions/checkout@v2 - name: make versions diff --git a/.github/workflows/day-14.yaml b/.github/workflows/day-14.yaml index 1da237ef..31a2e8d2 100644 --- a/.github/workflows/day-14.yaml +++ b/.github/workflows/day-14.yaml @@ -16,7 +16,7 @@ jobs: test: runs-on: ubuntu-latest container: - image: "jonasjso/adventofcode2020:2020-12-05-with-node15" + image: "jonasjso/adventofcode2020:2020-12-07-with-deno" steps: - uses: actions/checkout@v2 - name: make versions diff --git a/.github/workflows/day-15.yaml b/.github/workflows/day-15.yaml index ba6eb922..cd4a3093 100644 --- a/.github/workflows/day-15.yaml +++ b/.github/workflows/day-15.yaml @@ -16,7 +16,7 @@ jobs: test: runs-on: ubuntu-latest container: - image: "jonasjso/adventofcode2020:2020-12-05-with-node15" + image: "jonasjso/adventofcode2020:2020-12-07-with-deno" steps: - uses: actions/checkout@v2 - name: make versions diff --git a/.github/workflows/day-16.yaml b/.github/workflows/day-16.yaml index 9e97f8cb..73683f54 100644 --- a/.github/workflows/day-16.yaml +++ b/.github/workflows/day-16.yaml @@ -16,7 +16,7 @@ jobs: test: runs-on: ubuntu-latest container: - image: "jonasjso/adventofcode2020:2020-12-05-with-node15" + image: "jonasjso/adventofcode2020:2020-12-07-with-deno" steps: - uses: actions/checkout@v2 - name: make versions diff --git a/.github/workflows/day-17.yaml b/.github/workflows/day-17.yaml index 6a6fef2f..145b4b96 100644 --- a/.github/workflows/day-17.yaml +++ b/.github/workflows/day-17.yaml @@ -16,7 +16,7 @@ jobs: test: runs-on: ubuntu-latest container: - image: "jonasjso/adventofcode2020:2020-12-05-with-node15" + image: "jonasjso/adventofcode2020:2020-12-07-with-deno" steps: - uses: actions/checkout@v2 - name: make versions diff --git a/.github/workflows/day-18.yaml b/.github/workflows/day-18.yaml index b0825efd..68fbcd1d 100644 --- a/.github/workflows/day-18.yaml +++ b/.github/workflows/day-18.yaml @@ -16,7 +16,7 @@ jobs: test: runs-on: ubuntu-latest container: - image: "jonasjso/adventofcode2020:2020-12-05-with-node15" + image: "jonasjso/adventofcode2020:2020-12-07-with-deno" steps: - uses: actions/checkout@v2 - name: make versions diff --git a/.github/workflows/day-19.yaml b/.github/workflows/day-19.yaml index 6ca68bb4..092b6377 100644 --- a/.github/workflows/day-19.yaml +++ b/.github/workflows/day-19.yaml @@ -16,7 +16,7 @@ jobs: test: runs-on: ubuntu-latest container: - image: "jonasjso/adventofcode2020:2020-12-05-with-node15" + image: "jonasjso/adventofcode2020:2020-12-07-with-deno" steps: - uses: actions/checkout@v2 - name: make versions diff --git a/.github/workflows/day-20.yaml b/.github/workflows/day-20.yaml index 10ba0444..ce7bb18a 100644 --- a/.github/workflows/day-20.yaml +++ b/.github/workflows/day-20.yaml @@ -16,7 +16,7 @@ jobs: test: runs-on: ubuntu-latest container: - image: "jonasjso/adventofcode2020:2020-12-05-with-node15" + image: "jonasjso/adventofcode2020:2020-12-07-with-deno" steps: - uses: actions/checkout@v2 - name: make versions diff --git a/.github/workflows/day-21.yaml b/.github/workflows/day-21.yaml index 0239fd02..f28ebda5 100644 --- a/.github/workflows/day-21.yaml +++ b/.github/workflows/day-21.yaml @@ -16,7 +16,7 @@ jobs: test: runs-on: ubuntu-latest container: - image: "jonasjso/adventofcode2020:2020-12-05-with-node15" + image: "jonasjso/adventofcode2020:2020-12-07-with-deno" steps: - uses: actions/checkout@v2 - name: make versions diff --git a/.github/workflows/day-22.yaml b/.github/workflows/day-22.yaml index 6ac7daae..b52158c1 100644 --- a/.github/workflows/day-22.yaml +++ b/.github/workflows/day-22.yaml @@ -16,7 +16,7 @@ jobs: test: runs-on: ubuntu-latest container: - image: "jonasjso/adventofcode2020:2020-12-05-with-node15" + image: "jonasjso/adventofcode2020:2020-12-07-with-deno" steps: - uses: actions/checkout@v2 - name: make versions diff --git a/.github/workflows/day-23.yaml b/.github/workflows/day-23.yaml index b5b2cd67..dabe8883 100644 --- a/.github/workflows/day-23.yaml +++ b/.github/workflows/day-23.yaml @@ -16,7 +16,7 @@ jobs: test: runs-on: ubuntu-latest container: - image: "jonasjso/adventofcode2020:2020-12-05-with-node15" + image: "jonasjso/adventofcode2020:2020-12-07-with-deno" steps: - uses: actions/checkout@v2 - name: make versions diff --git a/.github/workflows/day-24.yaml b/.github/workflows/day-24.yaml index 476eb83b..047be5c6 100644 --- a/.github/workflows/day-24.yaml +++ b/.github/workflows/day-24.yaml @@ -16,7 +16,7 @@ jobs: test: runs-on: ubuntu-latest container: - image: "jonasjso/adventofcode2020:2020-12-05-with-node15" + image: "jonasjso/adventofcode2020:2020-12-07-with-deno" steps: - uses: actions/checkout@v2 - name: make versions diff --git a/.github/workflows/day-25.yaml b/.github/workflows/day-25.yaml index a516d238..7ea9a6a2 100644 --- a/.github/workflows/day-25.yaml +++ b/.github/workflows/day-25.yaml @@ -16,7 +16,7 @@ jobs: test: runs-on: ubuntu-latest container: - image: "jonasjso/adventofcode2020:2020-12-05-with-node15" + image: "jonasjso/adventofcode2020:2020-12-07-with-deno" steps: - uses: actions/checkout@v2 - name: make versions diff --git a/Makefile b/Makefile index a2749b5b..d6efbfef 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -export DOCKER_TAG="jonasjso/adventofcode2020:2020-12-05-with-node15" +export DOCKER_TAG="jonasjso/adventofcode2020:2020-12-07-with-deno" .PHONY: test\ From 20d03c175a9cc918a9a795a6a96849d086a1635c Mon Sep 17 00:00:00 2001 From: Jonas Johan Solsvik Date: Mon, 7 Dec 2020 23:05:17 +0100 Subject: [PATCH 9/9] solve day 07 in deno --- .github/workflows/day-00-example.yaml | 2 +- days/day-07/solutions/day07.mjs | 1 - days/day-07/solutions/day07.ts | 83 +++++++++++++++++++++++++++ days/day-07/test.sh | 1 + 4 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 days/day-07/solutions/day07.ts diff --git a/.github/workflows/day-00-example.yaml b/.github/workflows/day-00-example.yaml index f43c89a8..ef85175c 100644 --- a/.github/workflows/day-00-example.yaml +++ b/.github/workflows/day-00-example.yaml @@ -20,7 +20,7 @@ jobs: test: runs-on: ubuntu-latest container: - image: "jonasjso/adventofcode2020:2020-12-05-with-node15" + image: "jonasjso/adventofcode2020:2020-12-07-with-deno" steps: - uses: actions/checkout@v2 - name: make versions diff --git a/days/day-07/solutions/day07.mjs b/days/day-07/solutions/day07.mjs index 6e6e9cc7..4e4d9254 100644 --- a/days/day-07/solutions/day07.mjs +++ b/days/day-07/solutions/day07.mjs @@ -1,5 +1,4 @@ import { readFileSync } from "fs"; -import { resolve } from "path"; const STANDARD_IN = 0 const input = readFileSync(STANDARD_IN) diff --git a/days/day-07/solutions/day07.ts b/days/day-07/solutions/day07.ts new file mode 100644 index 00000000..35a332c8 --- /dev/null +++ b/days/day-07/solutions/day07.ts @@ -0,0 +1,83 @@ +const input = (await Deno.readTextFile("/dev/stdin")); + +type Bag = { + color: string; + bags_inside: BagRef[]; +}; +type BagRef = { + count: number; + color: string; +}; + +const bags = [ + ...input.matchAll(/([\w]+ [\w]+) bags contain (\d (.*)[.]|no other bags)/g), +].map(([, color, rest]) => { + const bags_inside = ([ + ...rest.matchAll(/(\d) ([\w]+ [\w]+)/g), + ]) + .map(([, count, color]) => ({ count: parseInt(count), color })) as BagRef[]; + + return ({ color, bags_inside }); +}) as Bag[]; + +const COLOR_SHINY_GOLD = "shiny gold"; + +// --- Part 1 ---- +// Recursively find shiny golden bags +function findShinyGoldenBag(bag_refs: BagRef[]) { + if (bag_refs.length === 0) { + return 0; + } + + const resolved_bags = bag_refs + .map((bag) => bags.find(({ color }) => color === bag.color)) + .filter((bag) => bag !== undefined) as Bag[]; + + for (const { color, bags_inside } of resolved_bags) { + if (color === COLOR_SHINY_GOLD) { + return 1; + } else { + const found = findShinyGoldenBag(bags_inside); + if (found) { + return 1; + } else { + continue; + } + } + } + return 0; +} + +const bagContainingShinyGoldeBagCount = bags + .reduce((count, bag) => count + findShinyGoldenBag(bag.bags_inside), 0); + +// --- Part 2 --- +// Recursively count bags inside +function countBagsInside(bag_refs: BagRef[]): number { + if (bag_refs.length === 0) { + return 1; + } + + const resolved_bags = bag_refs + .map( + (bag) => [bag.count, bags.find(({ color }) => color === bag.color)] + ) as [number, Bag][]; + + const count = resolved_bags + .reduce( + (acc, [bag_count, bag]) => + acc + bag_count * countBagsInside(bag.bags_inside), + 1, + ); + + return count; +} + +const shinyGoldenBag = bags.find(({ color }) => + color === COLOR_SHINY_GOLD +) as Bag; + +const shinyGoldBagInsideCount = countBagsInside(shinyGoldenBag.bags_inside) - 1; + +console.log(`${bagContainingShinyGoldeBagCount}`); +console.log(`${shinyGoldBagInsideCount}`); diff --git a/days/day-07/test.sh b/days/day-07/test.sh index 7d315d37..d3fb35b3 100755 --- a/days/day-07/test.sh +++ b/days/day-07/test.sh @@ -10,4 +10,5 @@ $D/../../languages/sml.sh $D/input.txt $D/output.txt $D/solutions/day07.sml $D/../../languages/go.sh $D/input.txt $D/output.txt $D/solutions/tholok97.go $D/../../languages/python.sh $D/input.txt $D/output.txt $D/solutions/day07.stektpotet.py $D/../../languages/node.sh $D/input.txt $D/output.txt $D/solutions/day07.mjs +$D/../../languages/deno.sh $D/input.txt $D/output.txt $D/solutions/day07.ts echo ""