Initial commit
This commit is contained in:
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
/target
|
||||||
|
**/test.txt
|
||||||
|
**/input.txt
|
||||||
7
Cargo.lock
generated
Normal file
7
Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "aoc2025"
|
||||||
|
version = "0.1.0"
|
||||||
6
Cargo.toml
Normal file
6
Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "aoc2025"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
99
src/day6/mod.rs
Normal file
99
src/day6/mod.rs
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
use std::{fs::File, io::Read};
|
||||||
|
|
||||||
|
pub fn part_1(file:&str) -> u64 {
|
||||||
|
let mut directory = "/home/jason/Programming/Rust/aoc2025/src/day6/".to_owned();
|
||||||
|
directory.push_str(file);
|
||||||
|
let mut handle = File::open(directory).unwrap();
|
||||||
|
let mut file = String::new();
|
||||||
|
handle.read_to_string(&mut file).unwrap();
|
||||||
|
|
||||||
|
let mut file_iter = file.lines().rev();
|
||||||
|
let mut problems: Vec<Problem> = Vec::new();
|
||||||
|
|
||||||
|
for elem in file_iter.next().unwrap().split_whitespace() {
|
||||||
|
let op = match elem.chars().next().unwrap() {
|
||||||
|
'+' => {Operator::Add},
|
||||||
|
'*' => {Operator::Multiply},
|
||||||
|
_ => panic!("Encountered unhandled char!")
|
||||||
|
};
|
||||||
|
|
||||||
|
problems.push(Problem { numbers: Vec::new(), operator: op });
|
||||||
|
}
|
||||||
|
|
||||||
|
for lines in file_iter {
|
||||||
|
for (pos, num) in lines.split_whitespace().enumerate() {
|
||||||
|
let value = num.parse::<u64>().unwrap();
|
||||||
|
problems[pos].numbers.push(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let solution = problems.iter().fold(0, |acc, x| {
|
||||||
|
let n = match x.operator {
|
||||||
|
Operator::Add => x.numbers.iter().sum::<u64>(),
|
||||||
|
Operator::Multiply => x.numbers.iter().product::<u64>(),
|
||||||
|
};
|
||||||
|
n+acc
|
||||||
|
});
|
||||||
|
|
||||||
|
solution
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn part_2(file:&str) -> u64 {
|
||||||
|
let mut directory = "/home/jason/Programming/Rust/aoc2025/src/day6/".to_owned();
|
||||||
|
directory.push_str(file);
|
||||||
|
let mut handle = File::open(directory).unwrap();
|
||||||
|
let mut file = String::new();
|
||||||
|
handle.read_to_string(&mut file).unwrap();
|
||||||
|
|
||||||
|
let mut file_iter = file.lines().rev();
|
||||||
|
let mut problems: Vec<Problem> = Vec::new();
|
||||||
|
|
||||||
|
for elem in file_iter.next().unwrap().split_whitespace() {
|
||||||
|
let op = match elem.chars().next().unwrap() {
|
||||||
|
'+' => {Operator::Add},
|
||||||
|
'*' => {Operator::Multiply},
|
||||||
|
_ => panic!("Encountered unhandled char!")
|
||||||
|
};
|
||||||
|
|
||||||
|
problems.push(Problem { numbers: Vec::new(), operator: op });
|
||||||
|
}
|
||||||
|
|
||||||
|
for lines in file_iter {
|
||||||
|
for (pos, num) in lines.split_whitespace().enumerate() {
|
||||||
|
let value = num.parse::<u64>().unwrap();
|
||||||
|
problems[pos].numbers.push(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let solution = problems.iter().fold(0, |acc, x| {
|
||||||
|
let n = match x.operator {
|
||||||
|
Operator::Add => x.numbers.iter().sum::<u64>(),
|
||||||
|
Operator::Multiply => x.numbers.iter().product::<u64>(),
|
||||||
|
};
|
||||||
|
n+acc
|
||||||
|
});
|
||||||
|
|
||||||
|
solution
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Problem {
|
||||||
|
numbers: Vec<u64>,
|
||||||
|
operator: Operator,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum Operator {
|
||||||
|
Add,
|
||||||
|
Multiply,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test() {
|
||||||
|
assert_eq!(part_1("test.txt"), 4277556);
|
||||||
|
}
|
||||||
|
}
|
||||||
6
src/main.rs
Normal file
6
src/main.rs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
mod day6;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let d6p1 = day6::part_1("input.txt");
|
||||||
|
println!("Day 6 Part 1 Solution: {}", d6p1);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user