diff --git a/2022/.gitignore b/2022/.gitignore new file mode 100644 index 0000000..4bc139e --- /dev/null +++ b/2022/.gitignore @@ -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 + diff --git a/2022/Cargo.toml b/2022/Cargo.toml new file mode 100644 index 0000000..706d2e1 --- /dev/null +++ b/2022/Cargo.toml @@ -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] diff --git a/2022/get.sh b/2022/get.sh new file mode 100755 index 0000000..a1a1f37 --- /dev/null +++ b/2022/get.sh @@ -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 diff --git a/2022/src/bin/template.rs b/2022/src/bin/template.rs new file mode 100644 index 0000000..2f5e42e --- /dev/null +++ b/2022/src/bin/template.rs @@ -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 + } +} diff --git a/2022/src/lib.rs b/2022/src/lib.rs new file mode 100644 index 0000000..52e3177 --- /dev/null +++ b/2022/src/lib.rs @@ -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::>() + .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)); + } +}