Reorganize Rust

This commit is contained in:
Tobias Berger 2023-04-27 10:29:10 +02:00
parent f678f49ada
commit 4f8cda8c29
Signed by: toby
GPG key ID: 2D05EFAB764D6A88
16 changed files with 177 additions and 80 deletions

View file

@ -6,6 +6,9 @@
],
"settings": {
"editor.wordBasedSuggestions": false,
"javascript.suggest.names": false
"javascript.suggest.names": false,
"rust-analyzer.linkedProjects": [
"./rust/Cargo.toml"
]
}
}

View file

@ -1,13 +0,0 @@
[package]
name = "day1"
version = "0.1.0"
authors = ["GitHub <noreply@github.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
[[bin]]
name = "day1"
path = "main.rs"

View file

@ -1,64 +0,0 @@
use std::fs::File;
use std::io;
use std::io::prelude::*;
use std::path::Path;
fn parse_input<B: BufRead>(buf: B) -> Vec<u64> {
buf.lines()
.map(|line| {
line.expect("Failed to read line.")
.parse()
.expect("Failed to parse line input.")
})
.collect()
}
fn puzzle_a(nums: &[u64]) -> Option<u64> {
for a in nums {
for b in nums {
if a + b == 2020 {
return Some(a * b);
}
}
}
None
}
fn puzzle_b(nums: &[u64]) -> Option<u64> {
for a in nums {
for b in nums {
for c in nums {
if a + b + c == 2020 {
return Some(a * b * c);
}
}
}
}
None
}
fn open_file<P>(filename: P) -> io::Result<io::BufReader<File>>
where
P: AsRef<Path>,
{
let file = File::open(filename)?;
Ok(io::BufReader::new(file))
}
fn main() -> Result<(), &'static str> {
let buffer = open_file("input").unwrap();
let puzzle_input = parse_input(buffer);
println!(
"Part A:\n{}",
puzzle_a(&puzzle_input).expect("No solution for A found.")
);
println!("--------");
println!(
"Part B:\n{}",
puzzle_b(&puzzle_input).expect("No solution for B found.")
);
Ok(())
}

View file

View file

@ -1,5 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day1"
version = "0.1.0"
name = "advent-of-code"
version = "20.1.2"

28
rust/Cargo.toml Normal file
View file

@ -0,0 +1,28 @@
[package]
name = "advent-of-code"
version = "20.1.2"
edition = "2021"
resolver = "2"
[profile.release]
strip = "symbols"
lto = "fat"
opt-level = 3
codegen-units = 1
[profile.dev.package."*"]
opt-level = 3
codegen-units = 1
[profile.release.package."*"]
opt-level = 3
codegen-units = 1
[profile.dev.build-override]
opt-level = 3
codegen-units = 1
[profile.release.build-override]
opt-level = 3
codegen-units = 1
[[bin]]
name = "day01"
path = "src/day01/main.rs"

1
rust/src/day00/input.txt Normal file
View file

@ -0,0 +1 @@
Placeholder input

11
rust/src/day00/main.rs Normal file
View file

@ -0,0 +1,11 @@
const INPUT: &str = include_str!("input.txt");
mod part_1;
use part_1::part_1;
mod part_2;
use part_2::part_2;
pub fn main() {
part_1(INPUT);
part_2(INPUT);
}

24
rust/src/day00/part_1.rs Normal file
View file

@ -0,0 +1,24 @@
pub(crate) fn part_1(_input: &'static str) -> u64 {
todo!("Part 1")
}
#[cfg(test)]
mod tests {
const SAMPLE_INPUT: &str = include_str!("sample_input.txt");
#[test]
fn test_with_sample_solution() {
assert_eq!(
super::part_1(SAMPLE_INPUT),
todo!("Add result from example part 1")
);
}
#[test]
fn test_with_solution() {
assert_eq!(
super::part_1(crate::INPUT),
todo!("Add result for solved part 1")
);
}
}

24
rust/src/day00/part_2.rs Normal file
View file

@ -0,0 +1,24 @@
pub(crate) fn part_2(_input: &'static str) -> u64 {
todo!("Part 2")
}
#[cfg(test)]
mod tests {
const SAMPLE_INPUT: &str = include_str!("sample_input.txt");
#[test]
fn test_with_sample_solution() {
assert_eq!(
super::part_2(SAMPLE_INPUT),
todo!("Add result from example part 2")
);
}
#[test]
fn test_with_solution() {
assert_eq!(
super::part_2(crate::INPUT),
todo!("Add result for solved part 2")
);
}
}

View file

19
rust/src/day01/main.rs Normal file
View file

@ -0,0 +1,19 @@
const INPUT: &str = include_str!("input.txt");
mod part_1;
use part_1::part_1;
mod part_2;
use part_2::part_2;
fn parse_input(input: &'static str) -> Vec<u64> {
input
.lines()
.map(|line| line.parse().expect("Failed to parse line input."))
.collect()
}
pub fn main() {
let input = parse_input(INPUT);
part_1(&input);
part_2(&input);
}

27
rust/src/day01/part_1.rs Normal file
View file

@ -0,0 +1,27 @@
pub(crate) fn part_1(nums: &[u64]) -> u64 {
for a in nums {
for b in nums {
if a + b == 2020 {
let result = a * b;
println!("Part 1: {result}");
return result;
}
}
}
unreachable!("No solution found for part 1");
}
#[cfg(test)]
mod tests {
const SAMPLE_INPUT: &str = include_str!("sample_input.txt");
#[test]
fn test_with_sample_solution() {
assert_eq!(super::part_1(&crate::parse_input(SAMPLE_INPUT)), 514579);
}
#[test]
fn test_with_solution() {
assert_eq!(super::part_1(&crate::parse_input(crate::INPUT)), 964875);
}
}

29
rust/src/day01/part_2.rs Normal file
View file

@ -0,0 +1,29 @@
pub(crate) fn part_2(nums: &[u64]) -> u64 {
for a in nums {
for b in nums {
for c in nums {
if a + b + c == 2020 {
let result = a * b * c;
println!("Part 2: {result}");
return result;
}
}
}
}
unreachable!("No solution found for part 2")
}
#[cfg(test)]
mod tests {
const SAMPLE_INPUT: &str = include_str!("sample_input.txt");
#[test]
fn test_with_sample_solution() {
assert_eq!(super::part_2(&crate::parse_input(SAMPLE_INPUT)), 241861950);
}
#[test]
fn test_with_solution() {
assert_eq!(super::part_2(&crate::parse_input(crate::INPUT)), 158661360);
}
}

View file

@ -0,0 +1,6 @@
1721
979
366
299
675
1456