1
0
mirror of https://gitlab.com/MisterBiggs/aoc_2015-rust.git synced 2025-06-15 22:46:43 +00:00

Finished day 1

This commit is contained in:
Anson 2022-11-02 22:49:18 -06:00
commit 84e90489b2
6 changed files with 100 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

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 = "AoC2015"
version = "0.1.0"

8
Cargo.toml Normal file
View File

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

1
inputs/day1.txt Normal file

File diff suppressed because one or more lines are too long

77
src/day1.rs Normal file
View File

@ -0,0 +1,77 @@
use std::fs;
pub fn run() {
println!("Day 1:");
let input = fs::read_to_string("./inputs/day1.txt")
.expect("Could not read file")
.to_string();
// part1(&lines);
// part2(&lines);
println!("\tPart 1: {}", part1(&input));
println!("\tPart 2: {}", part2(&input));
}
fn part1(directions: &String) -> isize {
directions
.chars()
.map(|c| match c {
'(' => 1,
')' => -1,
_ => 0,
})
.sum::<isize>()
}
fn part2(directions: &String) -> usize {
let mut floor: isize = 0;
for (num, dir) in directions.chars().enumerate() {
floor += match dir {
'(' => 1,
')' => -1,
_ => 0,
};
if floor == -1 {
return num + 1;
}
}
return directions.len() + 1;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_1() {
let mut input = "(())".to_string();
assert_eq!(part1(&input), 0);
input = "()()".to_string();
assert_eq!(part1(&input), 0);
input = "(((".to_string();
assert_eq!(part1(&input), 3);
input = "(()(()(".to_string();
assert_eq!(part1(&input), 3);
input = "())".to_string();
assert_eq!(part1(&input), -1);
input = ")())())".to_string();
assert_eq!(part1(&input), -3);
}
#[test]
fn test_2() {
let mut input = ")".to_string();
assert_eq!(part2(&input), 1);
input = "()())".to_string();
assert_eq!(part2(&input), 5);
}
}

6
src/main.rs Normal file
View File

@ -0,0 +1,6 @@
mod day1;
fn main() {
println!("Running Advent of Code 2021");
day1::run();
}