rust bootstrap

master
pegasust 2022-12-13 23:03:15 -07:00
parent 826cb55105
commit 17c3671974
10 changed files with 140 additions and 34 deletions

2
2022/d9/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.direnv
target

7
2022/d9/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "d9"
version = "0.1.0"

8
2022/d9/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "d9"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

7
2022/d9/aoc/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aoc"
version = "0.1.0"

8
2022/d9/aoc/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "aoc"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

3
2022/d9/aoc/src/main.rs Normal file
View File

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

View File

@ -15,6 +15,21 @@
"type": "github" "type": "github"
} }
}, },
"flake-utils_2": {
"locked": {
"lastModified": 1659877975,
"narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1670064435, "lastModified": 1670064435,
@ -31,10 +46,46 @@
"type": "github" "type": "github"
} }
}, },
"nixpkgs_2": {
"locked": {
"lastModified": 1665296151,
"narHash": "sha256-uOB0oxqxN9K7XGF1hcnY+PQnlQJ+3bP2vCn/+Ru/bbc=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "14ccaaedd95a488dd7ae142757884d8e125b3363",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": { "root": {
"inputs": { "inputs": {
"flake-utils": "flake-utils", "flake-utils": "flake-utils",
"nixpkgs": "nixpkgs" "nixpkgs": "nixpkgs",
"rust-overlay": "rust-overlay"
}
},
"rust-overlay": {
"inputs": {
"flake-utils": "flake-utils_2",
"nixpkgs": "nixpkgs_2"
},
"locked": {
"lastModified": 1670552927,
"narHash": "sha256-lCE51eAGrAFS4k9W5aDGFpVtOAwQQ/rFMN80PCDh0vo=",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "a0fdafd18c9cf599fde17fbaf07dbb20fa57eecb",
"type": "github"
},
"original": {
"owner": "oxalica",
"repo": "rust-overlay",
"type": "github"
} }
} }
}, },

View File

@ -3,26 +3,40 @@
inputs = { inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils"; flake-utils.url = "github:numtide/flake-utils";
rust-overlay.url = "github:oxalica/rust-overlay";
}; };
outputs = { nixpkgs, flake-utils, ... } @ inputs: outputs = { nixpkgs, flake-utils, rust-overlay, ... } @ inputs:
flake-utils.lib.eachSystem flake-utils.lib.defaultSystems (sys: flake-utils.lib.eachSystem flake-utils.lib.defaultSystems (sys:
let let
overlays = [ ]; overlays = [ rust-overlay.overlays.default ];
pkgs = import nixpkgs { system = sys; overlays = overlays; }; pkgs = import nixpkgs { system = sys; overlays = overlays; };
shellHookAfter = '' shellHookAfter = ''
echo "The input files should be placed under ./data/{submission,example}.txt" echo "The input files should be placed under ./data/{submission,example}.txt"
echo "This problem shares one input between two parts" echo "This problem shares one input between two parts"
''; '';
py_pkgs = [ pkgs.python310 ]; py_pkgs = [ pkgs.python310 ];
rs_pkgs = [
pkgs.openssl
pkgs.pkg-config
# Add rust-src, which rust-analyzer seems to rely upon
(pkgs.rust-bin.selectLatestNightlyWith
(
toolchain:
toolchain.default.override {
extensions = [ "rust-src" ];
}
))
];
in in
{ {
# Jack of all trades # Jack of all trades
devShell = pkgs.mkShell devShell = pkgs.mkShell
{ {
nativeBuildInputs = py_pkgs ++ ocaml_pkgs; buildInputs = py_pkgs ++ rs_pkgs;
shellHook = '' shellHook = ''
echo "> Default runtime. This contains both ocaml and python3 env" echo "> Default runtime. This contains both rust and python3 env"
echo "Run ./run-py.sh for Python's output and ./run-oml.sh for OCaml's output" echo "Run ./run-py.sh for Python's output and ./run-rs.sh for Rust's output"
'' + shellHookAfter; '' + shellHookAfter;
}; };
devShells = { devShells = {
@ -38,6 +52,13 @@
# nix develop ./#fennel # nix develop ./#fennel
# nix develop ./#python # nix develop ./#python
rust = pkgs.mkShell {
nativeBuildInputs = rs_pkgs;
shellHook = ''
echo "> Rust runtime"
echo "Run ./run-rs.sh to see output of the solution"
'' + shellHookAfter;
};
python = pkgs.mkShell { python = pkgs.mkShell {
nativeBuildInputs = py_pkgs; nativeBuildInputs = py_pkgs;
shellHook = '' shellHook = ''

View File

@ -1,18 +1,18 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import sys import sys
from typing import Iterable, Generator from typing import Iterable
from functools import reduce
from itertools import product
def add_nd(lhs: Iterable[int], rhs: Iterable[int]): def add_nd(lhs: Iterable[int], rhs: Iterable[int]):
# Add two generic n-d vector together
return tuple(l + r for l, r in zip(lhs, rhs)) return tuple(l + r for l, r in zip(lhs, rhs))
def negate_nd(vec: Iterable[int]): def negate_nd(vec: Iterable[int]):
# Negates a generic n-d vector
return tuple(-e for e in vec) return tuple(-e for e in vec)
def norm_1k_nd(vec: Iterable[int]):
return sum(abs(e) for e in vec)
def norm_infk_nd(vec: Iterable[int]): def norm_infk_nd(vec: Iterable[int]):
# Mathematical concept where I get the length of the longest vector component
return max(abs(e) for e in vec) return max(abs(e) for e in vec)
def sign(e: int):
return 0 if e == 0 else (1 if e > 0 else -1)
def dir_to_offset(dir: str): def dir_to_offset(dir: str):
match dir: match dir:
@ -26,8 +26,6 @@ def dir_to_offset(dir: str):
return (1, 0) return (1, 0)
case d: case d:
raise RuntimeError(f"Unknown direction {d}") raise RuntimeError(f"Unknown direction {d}")
def sign(e: int):
return 0 if e == 0 else (1 if e > 0 else -1)
def move_knot(knot: list[tuple[int, int]], offset: Iterable[int]): def move_knot(knot: list[tuple[int, int]], offset: Iterable[int]):
knot.append(add_nd(knot[-1], offset)) knot.append(add_nd(knot[-1], offset))
@ -35,50 +33,44 @@ def move_knot(knot: list[tuple[int, int]], offset: Iterable[int]):
def part1(commands: list[tuple[str, int]]): def part1(commands: list[tuple[str, int]]):
tail_locs: list[tuple[int, int]] = [(0,0)] # keeps track of where tail moved tail_locs: list[tuple[int, int]] = [(0,0)] # keeps track of where tail moved
# NOTE: I don't need to keep track of head's history here. This is only
# for ease of debugging
head_locs : list[tuple[int, int]] = [(0,0)] # keeps track of where head moved head_locs : list[tuple[int, int]] = [(0,0)] # keeps track of where head moved
for dir, rep in commands: for dir, rep in commands:
for _ in range(rep): for _ in range(rep):
move_knot(head_locs, dir_to_offset(dir)) move_knot(head_locs, dir_to_offset(dir))
offset = add_nd(head_locs[-1], negate_nd(tail_locs[-1])) offset = add_nd(head_locs[-1], negate_nd(tail_locs[-1]))
# print(f"{offset=}") # print(f"{offset=}")
match norm_1k_nd(offset), norm_infk_nd(offset): if norm_infk_nd(offset) <= 1:
case (0, _) | (1, _) | (2, 1): continue # no need to catch up
pass # don't need to catchup
case (_, _):
# negate & normalize in inf_k
move_knot(tail_locs, (sign(e) for e in offset)) move_knot(tail_locs, (sign(e) for e in offset))
case e:
raise RuntimeError(f"Unknown case {e}\n{tail_locs=}\n{head_locs=}")
# print(f"{tail_locs=}") # print(f"{tail_locs=}")
# print(f"{head_locs=}") # print(f"{head_locs=}")
return len(set(tail_locs)) return len(set(tail_locs))
def part2(commands: list[tuple[str, int]]): def part2(commands: list[tuple[str, int]]):
KNOTS = 10 KNOTS = 10
# NOTE: I don't need to keep track of any history but tail's. This is only
# for ease of debugging
locs = [[(0,0)] for _ in range(KNOTS)] # 10 knots :) locs = [[(0,0)] for _ in range(KNOTS)] # 10 knots :)
for dir, rep in commands: for dir, rep in commands:
for _ in range(rep): for _ in range(rep):
move_knot(locs[0], dir_to_offset(dir)) # move head move_knot(locs[0], dir_to_offset(dir)) # move head
for i in range(1, KNOTS): for i in range(1, KNOTS):
offset = add_nd(locs[i-1][-1], negate_nd(locs[i][-1])) offset = add_nd(locs[i-1][-1], negate_nd(locs[i][-1]))
match norm_1k_nd(offset), norm_infk_nd(offset): if norm_infk_nd(offset) <= 1:
case (0, _) | (1, _) | (2, 1): continue # no need to catch up
pass # don't need to catchup
case (_, _):
# negate & normalize in inf_k
move_knot(locs[i], (sign(e) for e in offset)) move_knot(locs[i], (sign(e) for e in offset))
case e:
raise RuntimeError(f"Unknown case {e}\n{locs[i]=}\n{locs[i-1]=}")
return len(set(locs[-1])) return len(set(locs[-1]))
def main(lines: Iterable[str]): def main(lines: Iterable[str]):
splited = (line.strip().split() for line in lines) splited = (line.strip().split() for line in lines)
striped_lines = [(comp[0], int(comp[1])) for comp in splited if len(comp) > 0] # -> [" D 4 ",...] -> [["D", "4"], ...]
# print(striped_lines) ## Filters out any line that has empty newline and turn repitions into int
commands = [(comp[0], int(comp[1])) for comp in striped_lines] commands = [(comp[0], int(comp[1])) for comp in splited if len(comp) > 0]
# -> [[], ["D", "4"], ...] -> [["D", 4]]
print("part1", part1(commands)) print("part1", part1(commands))
print("part2", part2(commands)) print("part2", part2(commands))
pass pass
@ -86,3 +78,4 @@ def main(lines: Iterable[str]):
if __name__=="__main__": if __name__=="__main__":
with open(sys.argv[1], "r") as f: with open(sys.argv[1], "r") as f:
main(f) main(f)

6
2022/d9/src/main.rs Normal file
View File

@ -0,0 +1,6 @@
fn main() {
// let test = include_str!("../data/example.txt");
let sub = inlcude_str!("../data/submission.txt");
let commands = sub.lines()
.map(|line| line.split(" "))
}