day 1: problem 1 solution
This commit is contained in:
parent
76cacf4704
commit
892d59f21b
7
1/Cargo.lock
generated
Normal file
7
1/Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "day1"
|
||||
version = "0.1.0"
|
6
1/Cargo.toml
Normal file
6
1/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "day1"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
85
1/src/main.rs
Normal file
85
1/src/main.rs
Normal file
@ -0,0 +1,85 @@
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader, Lines};
|
||||
use std::iter::zip;
|
||||
use std::time::Instant;
|
||||
|
||||
// BOILERPLATE
|
||||
type InputIter = Lines<BufReader<File>>;
|
||||
|
||||
fn get_input() -> InputIter {
|
||||
let f = File::open("input").unwrap();
|
||||
let br = BufReader::new(f);
|
||||
br.lines()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let start = Instant::now();
|
||||
let ans1 = problem1(get_input());
|
||||
let duration = start.elapsed();
|
||||
println!("Problem 1 solution: {} [{}s]", ans1, duration.as_secs_f64());
|
||||
|
||||
let start = Instant::now();
|
||||
let ans2 = problem2(get_input());
|
||||
let duration = start.elapsed();
|
||||
println!("Problem 2 solution: {} [{}s]", ans2, duration.as_secs_f64());
|
||||
}
|
||||
|
||||
// PROBLEM 1 solution
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Entry {
|
||||
pos: usize,
|
||||
val: u32,
|
||||
}
|
||||
|
||||
fn problem1<T: BufRead>(input: Lines<T>) -> u64 {
|
||||
let mut left = Vec::new();
|
||||
let mut right = Vec::new();
|
||||
for (pos, line) in input.enumerate() {
|
||||
if line.is_err() {
|
||||
panic!("can't read line");
|
||||
}
|
||||
let line = line.unwrap();
|
||||
let parts: Vec<&str> = line.split_ascii_whitespace().collect();
|
||||
left.push(Entry{pos, val: parts[0].parse::<u32>().unwrap()});
|
||||
right.push(Entry{pos, val: parts[1].parse::<u32>().unwrap()});
|
||||
}
|
||||
left.sort_by_key(|entry| entry.val);
|
||||
right.sort_by_key(|entry| entry.val);
|
||||
|
||||
println!("{:?}", right);
|
||||
|
||||
zip(left, right).map(|(left, right)| {
|
||||
println!("{:?} {:?}", left, right);
|
||||
u64::abs_diff(left.val as u64, right.val as u64)}).sum()
|
||||
}
|
||||
|
||||
// PROBLEM 2 solution
|
||||
fn problem2<T: BufRead>(input: Lines<T>) -> u64 {
|
||||
0
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::*;
|
||||
use std::io::Cursor;
|
||||
|
||||
const EXAMPLE: &str = &"3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3";
|
||||
|
||||
#[test]
|
||||
fn problem1_example() {
|
||||
let c = Cursor::new(EXAMPLE);
|
||||
assert_eq!(problem1(c.lines()), 11);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn problem2_example() {
|
||||
let c = Cursor::new(EXAMPLE);
|
||||
assert_eq!(problem2(c.lines()), 0);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user