aoc 2022 d3 with deno + ts. Let's go
commit
46ba1d4cd0
|
@ -0,0 +1,62 @@
|
|||
"""
|
||||
https://adventofcode.com/2022/day/1
|
||||
"""
|
||||
|
||||
from typing import Iterable
|
||||
|
||||
def calories_sum(input_lines: Iterable[str])->Iterable[int]:
|
||||
# idk why this doesn't work with file inputs
|
||||
# string parsing is a bit tough here
|
||||
elves = [[]]
|
||||
for raw_line in input_lines:
|
||||
line = raw_line.strip()
|
||||
if len(line) == 0:
|
||||
elves.append([])
|
||||
continue
|
||||
elves[-1].append(int(line))
|
||||
return (sum(carry) for carry in elves if len(carry) > 0)
|
||||
|
||||
def batch_and_exec(input_lines: Iterable[str])->int:
|
||||
return sorted(calories_sum(input_lines), reverse=True)[0]
|
||||
|
||||
def top3_sum(input_lines: Iterable[str])->int:
|
||||
return sum(sorted(calories_sum(input_lines), reverse=True)[:3])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
input_f = "2022/d1_input.txt"
|
||||
# First elf: [1000, 2000, 3000] -> 6000 calories
|
||||
# Second elf: [4000]
|
||||
# Third elf: [5000, 6000] -> 11000 calories
|
||||
# Fourth elf: [7000, 8000, 9000] -> 24000 calories
|
||||
# Fifth elf: [10000]
|
||||
# Want to know: which elf is carrying the most calories
|
||||
# in this case: 24000
|
||||
input = """
|
||||
1000
|
||||
2000
|
||||
3000
|
||||
|
||||
4000
|
||||
|
||||
5000
|
||||
6000
|
||||
|
||||
7000
|
||||
8000
|
||||
9000
|
||||
|
||||
10000
|
||||
"""
|
||||
output = batch_and_exec(input.splitlines())
|
||||
assert output == 24000, f"Got {output}, expected {24000}"
|
||||
ex_top3 = top3_sum(input.splitlines())
|
||||
assert ex_top3 == 45000, f"Got {ex_top3}, expected {45000}"
|
||||
|
||||
with open(input_f) as inp:
|
||||
print(batch_and_exec(inp))
|
||||
|
||||
with open(input_f) as inp:
|
||||
print(top3_sum(inp))
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,5 @@
|
|||
if command -v nix-shell &> /dev/null
|
||||
then
|
||||
use flake
|
||||
fi
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
# AoC Day 3
|
||||
|
||||
## Tasks
|
||||
|
||||
- [x] Sets up Deno (+ts) environment with Neovim
|
||||
- [x] Sets up a Deno (+TS) project
|
||||
- [ ] Solves [AoC 2022/d3](https://adventofcode.com/2022/day/3)
|
||||
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
vJrwpWtwJgWrhcsFMMfFFhFp
|
||||
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
|
||||
PmmdzqPrVvPwwTWBwg
|
||||
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
|
||||
ttgJtRGJQctTZtZT
|
||||
CrZsJsPPZsGzwwsLwLmpwMDw
|
|
@ -0,0 +1,58 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"allowJs": false,
|
||||
"lib": [
|
||||
"deno.window"
|
||||
],
|
||||
"strict": true
|
||||
},
|
||||
"importMap": "import_map.json",
|
||||
"lint": {
|
||||
"files": {
|
||||
"include": [
|
||||
"src/"
|
||||
],
|
||||
"exclude": [
|
||||
"src/testdata/"
|
||||
]
|
||||
},
|
||||
"rules": {
|
||||
"tags": [
|
||||
"recommended"
|
||||
],
|
||||
"include": [
|
||||
"ban-untagged-todo"
|
||||
],
|
||||
"exclude": [
|
||||
"no-unused-vars"
|
||||
]
|
||||
}
|
||||
},
|
||||
"fmt": {
|
||||
"files": {
|
||||
"include": [
|
||||
"src/"
|
||||
],
|
||||
"exclude": [
|
||||
"src/testdata/"
|
||||
]
|
||||
},
|
||||
"options": {
|
||||
"useTabs": true,
|
||||
"lineWidth": 80,
|
||||
"indentWidth": 4,
|
||||
"singleQuote": true,
|
||||
"proseWrap": "preserve"
|
||||
}
|
||||
},
|
||||
"test": {
|
||||
"files": {
|
||||
"include": [
|
||||
"src/"
|
||||
],
|
||||
"exclude": [
|
||||
"src/testdata/"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
{
|
||||
"nodes": {
|
||||
"flake-utils": {
|
||||
"locked": {
|
||||
"lastModified": 1667395993,
|
||||
"narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=",
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1669791787,
|
||||
"narHash": "sha256-KBfoA2fOI5+wCrm7PR+j7jHqXeTkVRPQ0m5fcKchyuU=",
|
||||
"owner": "nixos",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "e76c78d20685a043d23f5f9e0ccd2203997f1fb1",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "nixos",
|
||||
"ref": "nixos-unstable",
|
||||
"repo": "nixpkgs",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"flake-utils": "flake-utils",
|
||||
"nixpkgs": "nixpkgs"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
{
|
||||
description = "Typescript and Deno";
|
||||
inputs = {
|
||||
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
|
||||
flake-utils.url = "github:numtide/flake-utils";
|
||||
};
|
||||
outputs = { self, nixpkgs, flake-utils, ... }:
|
||||
flake-utils.lib.eachSystem flake-utils.lib.defaultSystems (sys:
|
||||
let pkgs = import nixpkgs { system = sys; }; in
|
||||
{
|
||||
devShell = pkgs.mkShell {
|
||||
nativeBuildInputs = [ pkgs.bashInteractive pkgs.deno ];
|
||||
shellHook = ''
|
||||
echo "Testing deno's installed version:"
|
||||
deno --version
|
||||
'';
|
||||
};
|
||||
}
|
||||
);
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
#!/usr/bin/env sh
|
||||
|
||||
# Runs deno at src/d3_deno.ts with arguments ["example.txt"]
|
||||
echo "example.txt"
|
||||
deno run --allow-read src/d3_deno.ts "example.txt"
|
||||
|
||||
echo "submission.txt"
|
||||
deno run --allow-read src/d3_deno.ts "submission.txt"
|
||||
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
if(Deno.args.length < 1) {
|
||||
throw Error("d3_deno.ts expects a single argument that points to AOC's day 3 input file");
|
||||
}
|
||||
const fileLoc = Deno.args[0];
|
||||
const text = await Deno.readTextFile(fileLoc);
|
||||
|
Loading…
Reference in New Issue