Cleanup for 2022 days 11 through 21: Turned into a lib and introduced parse errors
This commit is contained in:
parent
c7852c9791
commit
fcb2fed515
26 changed files with 1095 additions and 879 deletions
|
@ -1,4 +1,18 @@
|
|||
use std::{fs, cmp::Ordering};
|
||||
use core::fmt::Display;
|
||||
use std::cmp::Ordering;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum ParseError<'a> {
|
||||
LineMalformed(&'a str),
|
||||
}
|
||||
|
||||
impl Display for ParseError<'_> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::LineMalformed(v) => write!(f, "Line is malformed: {v}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
enum PacketItem<T> {
|
||||
|
@ -13,19 +27,23 @@ struct Pair {
|
|||
right: PacketItem<usize>,
|
||||
}
|
||||
|
||||
impl Pair {
|
||||
fn from(read_string: &str) -> Self {
|
||||
let (first, second) = read_string.split_once('\n').expect("Failed to split input pair.");
|
||||
impl <'a> TryFrom<&'a str> for Pair {
|
||||
type Error = ParseError<'a>;
|
||||
|
||||
let left = parse_packet_item(first);
|
||||
let right = parse_packet_item(second);
|
||||
fn try_from(value: &'a str) -> Result<Self, Self::Error> {
|
||||
if let Some((first, second)) = value.split_once('\n') {
|
||||
|
||||
Self {
|
||||
left,
|
||||
right,
|
||||
let left = parse_packet_item(first);
|
||||
let right = parse_packet_item(second);
|
||||
|
||||
Ok(Self {
|
||||
left,
|
||||
right,
|
||||
})
|
||||
} else {
|
||||
Err(Self::Error::LineMalformed(value))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn are_correctly_ordered(left: &PacketItem<usize>, right: &PacketItem<usize>) -> Option<bool> {
|
||||
|
@ -118,17 +136,6 @@ fn parse_packet_item(string_representation: &str) -> PacketItem<usize> {
|
|||
PacketItem::List(sub_items)
|
||||
}
|
||||
|
||||
fn read_file(path: &str) -> String {
|
||||
fs::read_to_string(path)
|
||||
.expect("File not Found")
|
||||
}
|
||||
|
||||
fn get_pairs(received: &str) -> Vec<Pair> {
|
||||
received.split("\n\n")
|
||||
.map(Pair::from)
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn get_pair_sum(pairs: &[Pair]) -> usize {
|
||||
pairs.iter()
|
||||
.enumerate()
|
||||
|
@ -157,33 +164,60 @@ fn decode(pairs: &[Pair]) -> usize {
|
|||
.enumerate()
|
||||
.filter(|(_, packet)| *packet == divider1 || *packet == divider2)
|
||||
.map(|(index, _)| index + 1)
|
||||
.reduce(|a, b| a*b)
|
||||
.unwrap()
|
||||
.product::<usize>()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let received = read_file("input");
|
||||
|
||||
let pairs = get_pairs(&received);
|
||||
|
||||
println!("The sum of the indexes of correctly ordered pairs is {}", get_pair_sum(&pairs));
|
||||
println!("The decoder key is {}", decode(&pairs));
|
||||
pub fn run(input: &str) -> Result<(usize, usize), ParseError> {
|
||||
let pairs = input.split("\n\n").map(Pair::try_from).collect::<Result<Vec<_>, _>>()?;
|
||||
let first = get_pair_sum(&pairs);
|
||||
let second = decode(&pairs);
|
||||
Ok((first, second))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sample_input() {
|
||||
let received = read_file("tests/sample_input");
|
||||
let pairs = get_pairs(&received);
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::fs::read_to_string;
|
||||
|
||||
assert_eq!(get_pair_sum(&pairs), 13);
|
||||
assert_eq!(decode(&pairs), 140);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn challenge_input() {
|
||||
let received = read_file("tests/input");
|
||||
let pairs = get_pairs(&received);
|
||||
|
||||
assert_eq!(get_pair_sum(&pairs), 5659);
|
||||
assert_eq!(decode(&pairs), 22110);
|
||||
fn read_file(name: &str) -> String {
|
||||
read_to_string(name).expect(&format!("Unable to read file: {name}")[..]).trim().to_string()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sample() {
|
||||
let sample_input = read_file("tests/sample_input");
|
||||
assert_eq!(run(&sample_input), Ok((13, 140)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_challenge() {
|
||||
let challenge_input = read_file("tests/challenge_input");
|
||||
assert_eq!(run(&challenge_input), Ok((5659, 22110)));
|
||||
}
|
||||
}
|
||||
// fn main() {
|
||||
// let received = read_file("input");
|
||||
//
|
||||
// let pairs = get_pairs(&received);
|
||||
//
|
||||
// println!("The sum of the indexes of correctly ordered pairs is {}", get_pair_sum(&pairs));
|
||||
// println!("The decoder key is {}", decode(&pairs));
|
||||
// }
|
||||
//
|
||||
// #[test]
|
||||
// fn sample_input() {
|
||||
// let received = read_file("tests/sample_input");
|
||||
// let pairs = get_pairs(&received);
|
||||
//
|
||||
// assert_eq!(get_pair_sum(&pairs), 13);
|
||||
// assert_eq!(decode(&pairs), 140);
|
||||
// }
|
||||
//
|
||||
// #[test]
|
||||
// fn challenge_input() {
|
||||
// let received = read_file("tests/input");
|
||||
// let pairs = get_pairs(&received);
|
||||
//
|
||||
// assert_eq!(get_pair_sum(&pairs), 5659);
|
||||
// assert_eq!(decode(&pairs), 22110);
|
||||
// }
|
Loading…
Add table
Add a link
Reference in a new issue