Compare commits

...

4 Commits

Author SHA1 Message Date
Niccolo Borgioli 796033f6b3
day one improvements 2023-11-18 18:00:01 +01:00
Niccolo Borgioli ad6ddf4f75
benchmarks 2023-11-18 17:52:07 +01:00
Niccolo Borgioli eb723f5112
hygen rust 2023-11-18 17:06:39 +01:00
Niccolo Borgioli 3af5af2461
add rust 2023-11-18 16:45:50 +01:00
14 changed files with 169 additions and 8 deletions

4
.gitignore vendored
View File

@ -2,3 +2,7 @@
node_modules
*.txt
.env*
# Added by cargo
/target

45
2022/01/rust/main.rs Normal file
View File

@ -0,0 +1,45 @@
#![feature(test)]
extern crate test;
#[cfg(test)]
mod tests {
use super::*;
use test::Bencher;
#[bench]
fn bench_a(b: &mut Bencher) {
b.iter(|| part_a());
}
#[bench]
fn bench_b(b: &mut Bencher) {
b.iter(|| part_b());
}
}
fn part_a() {
let result: u32 = include_str!("../input.txt")
.trim()
.split("\n\n")
.map(|x| x.lines().map(|x| x.parse::<u32>().unwrap()).sum())
.max()
.unwrap();
println!("A: {}", result);
}
fn part_b() {
let mut result = include_str!("../input.txt")
.trim()
.split("\n\n")
.map(|x| x.lines().map(|x| x.parse::<u32>().unwrap()).sum::<u32>())
.collect::<Vec<u32>>();
result.sort_unstable();
let total = result.into_iter().rev().take(3).sum::<u32>();
println!("B: {}", total);
}
fn main() {
part_a();
part_b();
}

7
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 = "advent-of-code"
version = "0.1.0"

13
Cargo.toml Normal file
View File

@ -0,0 +1,13 @@
[package]
name = "advent-of-code"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[[bin]]
name = "2022-01"
path = "./2022/01/rust/main.rs"
# INJECT HERE

View File

@ -1,5 +1,37 @@
# Advent Of Code
> New days are generated with [`hygen`](https://github.com/jondot/hygen).
>
> `hygen riddle new --year 2021 --day 01`
## Getting started
New "days" are generated with [`hygen`](https://github.com/jondot/hygen). This will bootstrap code for python and rust. See details about the specifica languages below
```bash
# First time setup
bun i
# Generate
bun run gen --year 2022 --day 1
```
## Python
## Rust
Run a single day:
```bash
cargo run --bin 2022-01
```
Bench a day:
```bash
cargo bench --bin 2022-01
```
Bench all days:
```bash
cargo bench
```
> The benchmark is the built in Rust one. This is the [reason](https://doc.rust-lang.org/cargo/commands/cargo-bench.html) for the _nightly_ version.

View File

@ -0,0 +1,10 @@
---
inject: true
to: cargo.toml
before: "# INJECT HERE"
skip_if: <%= id %>
---
[[bin]]
name = "<%= id %>"
path = "<%= dir %>/rust/main.rs"

View File

@ -12,6 +12,13 @@ async function getInput(year, day) {
module.exports = {
params: async ({ args }) => {
return { ...args, input: await getInput(args.year, args.day) }
const day = args.day.toString().padStart(2, '0') // Padded
return {
...args,
id: `${args.year}-${day}`,
dir: `./${args.year}/${day}`,
input: await getInput(args.year, parseInt(day)),
day,
}
},
}

View File

@ -1,5 +1,5 @@
---
to: <%= year %>/<%= day %>/input.txt
to: <%= dir %>/input.txt
unless_exists: true
---
<%= input %>

View File

@ -1,5 +1,5 @@
---
to: <%= year %>/<%= day %>/python/main.py
to: <%= dir %>/python/main.py
unless_exists: true
---
#!/usr/bin/env python

View File

@ -1,5 +1,5 @@
---
to: <%= year %>/<%= day %>/README.md
to: <%= dir %>/README.md
unless_exists: true
---

View File

@ -0,0 +1,38 @@
---
to: <%= dir %>/rust/main.rs
unless_exists: true
---
#![feature(test)]
extern crate test;
#[cfg(test)]
mod tests {
use super::*;
use test::Bencher;
#[bench]
fn bench_a(b: &mut Bencher) {
b.iter(|| part_a());
}
#[bench]
fn bench_b(b: &mut Bencher) {
b.iter(|| part_b());
}
}
fn part_a() {
let result = include_str!("../input.txt");
println!("A: {}", result);
}
fn part_b() {
let result = include_str!("../input.txt");
println!("A: {}", result);
}
fn main() {
part_a();
part_b();
}

View File

@ -1,4 +1,4 @@
---
to: <%= year %>/<%= day %>/test.txt
to: <%= dir %>/test.txt
unless_exists: true
---

View File

@ -1,4 +1,7 @@
{
"scripts": {
"gen": "bun hygen riddle new"
},
"dependencies": {
"axios": "^1.6.0",
"hygen": "^6.2.11"

2
rust-toolchain.toml Normal file
View File

@ -0,0 +1,2 @@
[toolchain]
channel = "nightly"