Setup for 2022
This commit is contained in:
parent
f9463ba6c1
commit
c889b75029
21
2022/.gitignore
vendored
Normal file
21
2022/.gitignore
vendored
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
# Created by https://www.toptal.com/developers/gitignore/api/rust
|
||||||
|
# Edit at https://www.toptal.com/developers/gitignore?templates=rust
|
||||||
|
|
||||||
|
### Rust ###
|
||||||
|
# Generated by Cargo
|
||||||
|
# will have compiled files and executables
|
||||||
|
debug/
|
||||||
|
target/
|
||||||
|
|
||||||
|
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||||
|
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||||
|
Cargo.lock
|
||||||
|
|
||||||
|
# These are backup files generated by rustfmt
|
||||||
|
**/*.rs.bk
|
||||||
|
|
||||||
|
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||||
|
*.pdb
|
||||||
|
|
||||||
|
# End of https://www.toptal.com/developers/gitignore/api/rust
|
||||||
|
|
8
2022/Cargo.toml
Normal file
8
2022/Cargo.toml
Normal 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]
|
4
2022/get.sh
Executable file
4
2022/get.sh
Executable file
|
@ -0,0 +1,4 @@
|
||||||
|
#!/bin/bash
|
||||||
|
source ../.env
|
||||||
|
mkdir -p input/$1
|
||||||
|
curl "https://adventofcode.com/2022/day/$1/input" -H "Cookie: session=${SESSION}" > input/$1/input
|
28
2022/src/bin/template.rs
Normal file
28
2022/src/bin/template.rs
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
use aoc::Solver;
|
||||||
|
|
||||||
|
pub struct Day {}
|
||||||
|
fn main() {
|
||||||
|
Day::solve();
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn part1() {
|
||||||
|
Day::test(aoc::Part::ONE);
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn part2() {
|
||||||
|
Day::test(aoc::Part::TWO);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl aoc::Solver for Day {
|
||||||
|
fn day() -> u8 {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
fn part1(input: &str) -> u32 {
|
||||||
|
input.len() as u32
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part2(input: &str) -> u32 {
|
||||||
|
input.len() as u32
|
||||||
|
}
|
||||||
|
}
|
49
2022/src/lib.rs
Normal file
49
2022/src/lib.rs
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
|
pub enum Part {
|
||||||
|
ONE,
|
||||||
|
TWO
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait Solver {
|
||||||
|
fn day() -> u8;
|
||||||
|
fn part1(input: &str) -> u32;
|
||||||
|
fn part2(input: &str) -> u32;
|
||||||
|
|
||||||
|
fn test(part: Part) {
|
||||||
|
// Select the right function
|
||||||
|
let fun = match part {
|
||||||
|
Part::ONE => Self::part1,
|
||||||
|
Part::TWO => Self::part2,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Read the test input
|
||||||
|
let test = fs::read_to_string(format!("input/{}/test", Self::day())).expect("Test file does not exist!");
|
||||||
|
|
||||||
|
// Get the correct test result
|
||||||
|
let result: u32 = match test.split("\n")
|
||||||
|
.skip(part as usize)
|
||||||
|
.next()
|
||||||
|
.expect("Expected second line to contain result for part1")
|
||||||
|
.parse() {
|
||||||
|
Ok(result) => result,
|
||||||
|
_ => 0, // Use zero if no value is specified yet
|
||||||
|
};
|
||||||
|
|
||||||
|
// Get the input for the test
|
||||||
|
// @TODO This creates a new string, would be nice if we could actually get a slice here
|
||||||
|
let input = test.split("\n")
|
||||||
|
.skip(2)
|
||||||
|
.collect::<Vec<&str>>()
|
||||||
|
.join("\n");
|
||||||
|
|
||||||
|
// Assert that the result matches the expected value
|
||||||
|
assert_eq!(fun(&input), result);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn solve() {
|
||||||
|
let input = fs::read_to_string(format!("input/{}/test", Self::day())).expect("Input file does not exist!");
|
||||||
|
println!("Part 1: {}", Self::part1(&input));
|
||||||
|
println!("Part 2: {}", Self::part2(&input));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user