diff --git a/src/day1/mod.rs b/src/day1/mod.rs new file mode 100644 index 0000000..eb6a397 --- /dev/null +++ b/src/day1/mod.rs @@ -0,0 +1,58 @@ +use std::{fs::File, io::Read}; + +fn read_file(file: &str) -> String { + let mut directory = "/home/jason/Programming/Rust/aoc2025/src/day1/".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(); + file +} + +pub fn part_1(file: &str) -> u64 { + + let input = read_file(file); + + let mut pos = 50; + let mut count = 0; + + for i in input.lines() { + let mut char_iter = i.chars(); + let dir = char_iter.next().unwrap(); + let dist = char_iter.collect::().parse::().unwrap(); + + match dir { + 'L' => { + pos -= dist; + }, + 'R' => { + pos += dist; + }, + _ => panic!(), + } + + while pos < 0 { + pos += 100; + } + + while pos > 99 { + pos -= 100; + } + + if pos == 0 { + count += 1; + } + } + + count +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn day_one_part_one() { + assert_eq!(part_1("test.txt"), 3); + } +} \ No newline at end of file diff --git a/src/day3/mod.rs b/src/day3/mod.rs index 55b1ba4..4c3f1a5 100644 --- a/src/day3/mod.rs +++ b/src/day3/mod.rs @@ -1,4 +1,4 @@ -use std::{fs::File, io::Read}; +use std::{fs::{File, read}, io::Read}; fn read_file(file: &str) -> String { let mut directory = "/home/jason/Programming/Rust/aoc2025/src/day3/".to_owned(); @@ -22,6 +22,8 @@ pub fn part_1(file: &str) -> u64{ batteries.push(rowvec); } + let mut joltages: Vec = Vec::new(); + for bank in batteries { let mut first_digit = 0; let mut second_digit = 0; @@ -31,12 +33,49 @@ pub fn part_1(file: &str) -> u64{ let current = bank_iter.next().unwrap(); let next = bank_iter.peek(); match next { - Some(_) => {}, - None => {}, + Some(_) => { + if *current > first_digit { + first_digit = *current; + second_digit = 0; + } else if *current > second_digit { + second_digit = *current; + } + }, + None => { + if *current > second_digit { + second_digit = *current; + } + break; + }, } } + let output = first_digit*10 + second_digit; + joltages.push(output); } - 0 + + joltages.iter().fold(0, |acc, x| *x as u64 + acc) +} + +fn part_2(file: &str) -> u64 { + let data = read_file(file); + + let mut batteries: Vec> = Vec::new(); + + for row in data.lines() { + let mut rowvec = Vec::new(); + for c in row.chars() { + rowvec.push(c.to_digit(10).unwrap() as u8); + } + batteries.push(rowvec); + } + + let mut joltages: Vec = Vec::new(); + + for i in batteries { + + } + + joltages.iter().fold(0, |acc, x| *x as u64 + acc) } #[cfg(test)] @@ -44,7 +83,7 @@ mod tests { use super::*; #[test] - fn day_three_part_1() { + fn day_three_part_one() { assert_eq!(part_1("test.txt"), 357); } } \ No newline at end of file diff --git a/src/day8/mod.rs b/src/day8/mod.rs new file mode 100644 index 0000000..152a000 --- /dev/null +++ b/src/day8/mod.rs @@ -0,0 +1,62 @@ +use std::{fs::File, io::Read}; +//test 0-1_000 +//input 0-100_000 + +fn read_file(file: &str) -> String { + let mut directory = "/home/jason/Programming/Rust/aoc2025/src/day8/".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(); + file +} + +fn parse_file(data: String) -> Vec { + let mut points = Vec::new(); + for i in data.lines() { + let mut nums = i.split(','); + let x = nums.next().unwrap().parse().unwrap(); + let y = nums.next().unwrap().parse().unwrap(); + let z = nums.next().unwrap().parse().unwrap(); + points.push(Point{x,y,z}); + } + points +} + +#[derive(Debug)] +struct Point { + x: u32, + y: u32, + z: u32, +} + +fn part_1(file: &str) -> u64 { + + let grid_factor = 100; + let num_connections; + let grid_max; + + if file == "test.txt" { + num_connections = 10; + grid_max = 1000; + } else { + num_connections = 1000; + grid_max = 100000; + } + + let data = parse_file(read_file(file)); + + + + 0 +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn day_eight_p_1() { + assert_eq!(part_1("input.txt"), 40); + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 0fb36ef..5769202 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,38 @@ +use std::env; + +mod day1; mod day3; mod day6; mod day7; +mod day8; fn main() { - /*let d6p1 = day6::part_1("input.txt"); - println!("Day 6 Part 1 Solution: {}", d6p1); - let d6p2 = day6::part_2("input.txt"); - println!("Day 6 Part 2 Solution: {}", d6p2);*/ - let d7p1 = day7::part_1("input.txt"); - println!("Day 7 Part 1 Solution: {}", d7p1); - let d7p2 = day7::part_2("input.txt"); - println!("Day 7 Part 2 Solution: {}", d7p2); + let envargs = env::args().collect::>(); + + if envargs.contains(&"--day1".to_owned()) | envargs.contains(&"--all".to_owned()) { + let d3p1 = day1::part_1("input.txt"); + println!("Day 1 Part 1 Solution: {}", d3p1); + //let d3p2 = day3::part_2("input.txt"); + //println!("Day 3 Part 2 Solution: {}", d3p2); + } + if envargs.contains(&"--day3".to_owned()) | envargs.contains(&"--all".to_owned()) { + let d3p1 = day3::part_1("input.txt"); + println!("Day 3 Part 1 Solution: {}", d3p1); + //let d3p2 = day3::part_2("input.txt"); + //println!("Day 3 Part 2 Solution: {}", d3p2); + } + if envargs.contains(&"--day6".to_owned()) | envargs.contains(&"--all".to_owned()) { + let d6p1 = day6::part_1("input.txt"); + println!("Day 6 Part 1 Solution: {}", d6p1); + let d6p2 = day6::part_2("input.txt"); + println!("Day 6 Part 2 Solution: {}", d6p2); + } + if envargs.contains(&"--day7".to_owned()) | envargs.contains(&"--all".to_owned()) { + let d7p1 = day7::part_1("input.txt"); + println!("Day 7 Part 1 Solution: {}", d7p1); + let d7p2 = day7::part_2("input.txt"); + println!("Day 7 Part 2 Solution: {}", d7p2); + } + } \ No newline at end of file