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,14 +1,9 @@
|
|||
use std::fs;
|
||||
|
||||
fn read_file(path: &str) -> String {
|
||||
fs::read_to_string(path)
|
||||
.expect("File not Found")
|
||||
}
|
||||
use std::num::ParseIntError;
|
||||
|
||||
fn get_coordinates(encrypted: &[isize], key: isize, rounds: u8) -> (isize, isize, isize) {
|
||||
let decrypted = shuffle_with_key(encrypted, key, rounds);
|
||||
|
||||
let offset = decrypted.iter().position(|&x| x == 0).unwrap() as usize;
|
||||
let offset = decrypted.iter().position(|&x| x == 0).unwrap();
|
||||
let c1 = decrypted[(1000 + offset) % decrypted.len()];
|
||||
let c2 = decrypted[(2000 + offset) % decrypted.len()];
|
||||
let c3 = decrypted[(3000 + offset) % decrypted.len()];
|
||||
|
@ -43,38 +38,33 @@ fn shuffle_with_key(old: &[isize], key: isize, rounds: u8) -> Vec<isize> {
|
|||
.collect()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let contents = read_file("input");
|
||||
|
||||
let encrypted: Vec<isize> = contents.lines().map(|i| i.parse().unwrap()).collect();
|
||||
|
||||
let (c1, c2, c3) = get_coordinates(&encrypted, 1, 1);
|
||||
println!("The relevant numbers are {}, {} and {}, totalling {}.", c1, c2, c3, c1+c2+c3);
|
||||
|
||||
let (d1, d2, d3) = get_coordinates(&encrypted, 811589153, 10);
|
||||
println!("With Key, the relevant numbers are {}, {} and {}, totalling {}.", d1, d2, d3, d1+d2+d3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sample_input() {
|
||||
let contents = read_file("tests/sample_input");
|
||||
let encrypted: Vec<isize> = contents.lines().map(|i| i.parse().unwrap()).collect();
|
||||
|
||||
pub fn run(input: &str) -> Result<(isize, isize), ParseIntError> {
|
||||
let encrypted: Vec<isize> = input.lines().map(|i| i.parse()).collect::<Result<Vec<_>, _>>()?;
|
||||
let (c1, c2, c3) = get_coordinates(&encrypted, 1, 1);
|
||||
let (d1, d2, d3) = get_coordinates(&encrypted, 811589153, 10);
|
||||
|
||||
assert_eq!((c1, c2, c3), (4, -3, 2));
|
||||
assert_eq!((d1, d2, d3), (811589153, 2434767459, -1623178306));
|
||||
let first = c1+c2+c3;
|
||||
let second = d1+d2+d3;
|
||||
Ok((first, second))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn challenge_input() {
|
||||
let contents = read_file("tests/input");
|
||||
let encrypted: Vec<isize> = contents.lines().map(|i| i.parse().unwrap()).collect();
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::fs::read_to_string;
|
||||
|
||||
let (c1, c2, c3) = get_coordinates(&encrypted, 1, 1);
|
||||
let (d1, d2, d3) = get_coordinates(&encrypted, 811589153, 10);
|
||||
fn read_file(name: &str) -> String {
|
||||
read_to_string(name).expect(&format!("Unable to read file: {name}")[..]).trim().to_string()
|
||||
}
|
||||
|
||||
assert_eq!((c1, c2, c3), (6790, 9749, -8511));
|
||||
assert_eq!((d1, d2, d3), (6447264231432, 3708150840057, -1356977063816));
|
||||
#[test]
|
||||
fn test_sample() {
|
||||
let sample_input = read_file("tests/sample_input");
|
||||
assert_eq!(run(&sample_input), Ok((3, 1623178306)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_challenge() {
|
||||
let challenge_input = read_file("tests/challenge_input");
|
||||
assert_eq!(run(&challenge_input), Ok((8028, 8798438007673)));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue