day3 made testable

This commit is contained in:
Dylan Thies
2023-08-31 19:29:31 -04:00
parent f8ce0e6253
commit a4408d7d16

View File

@@ -1,18 +1,11 @@
#![warn(clippy::all, clippy::pedantic)]
use std::fs::File;
use std::io::{prelude::*, BufReader};
use std::fs;
fn main() -> std::io::Result<()> {
//Read in file
let file = File::open("input")?;
let reader = BufReader::new(file);
/*
let value = reader
fn part1(input: &str) -> String {
input
.lines()
.map(|line| {
let line = line.unwrap();
let (comp1, comp2) = line.split_at(line.len() / 2);
let duplicate = comp2.chars().find(|c| comp1.contains(*c)).unwrap();
match duplicate {
@@ -21,18 +14,18 @@ fn main() -> std::io::Result<()> {
_ => 0,
}
})
.sum::<i32>();
println!("Part 1: {value}");
*/
//part 2
// fold the lines into groups of three
let value = reader
.sum::<i32>()
.to_string()
}
fn part2(input: &str) -> String {
input
.lines()
.fold(Vec::new(), |mut acc: Vec<Vec<String>>, line| {
if acc.is_empty() || acc.last().unwrap().len() == 3 {
acc.push(Vec::new());
}
acc.last_mut().unwrap().push(line.unwrap());
acc.last_mut().unwrap().push(line.to_owned());
acc
})
.iter()
@@ -57,10 +50,41 @@ fn main() -> std::io::Result<()> {
_ => 0,
}
})
.sum::<i32>();
println!("Part 2: {value}");
.sum::<i32>()
.to_string()
}
fn main() -> std::io::Result<()> {
//Read in file
let file = fs::read_to_string("input")?;
// fold the lines into groups of three
println!("Part 1: {}", part1(&file));
println!("Part 2: {}", part2(&file));
// find common letter in the groups
// find common letters in the first 2 then find the common in the third
// sum the common letters
Ok(())
}
#[cfg(test)]
mod test {
use super::*;
const INPUT: &str = "vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw";
#[test]
fn part1_works() {
assert_eq!(part1(&INPUT), "157");
}
#[test]
fn part2_works() {
assert_eq!(part2(&INPUT), "70");
}
}