adventofcode

git clone https://git.ce9e.org/adventofcode.git

commit
6913c30f67ec131180e60bf8ab3b27ea70007972
parent
1e28e9c240de162240811350f7f86602b440b888
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2022-12-02 07:48
create reusable rust library

Diffstat

M 2022/01/part1.rs 12 +++---------
M 2022/02/part1.rs 15 ++++-----------
M 2022/02/part2.rs 15 ++++-----------
A 2022/lib.rs 10 ++++++++++

4 files changed, 21 insertions, 31 deletions


diff --git a/2022/01/part1.rs b/2022/01/part1.rs

@@ -1,17 +1,11 @@
    1    -1 use std::env::args;
    2    -1 use std::fs::File;
    3    -1 use std::io::BufRead;
    4    -1 use std::io::BufReader;
   -1     1 #[path = "../lib.rs"] mod lib;
    5     2 
    6     3 fn main() {
    7    -1     let path = args().nth(1).unwrap();
    8    -1     let file = File::open(path).unwrap();
    9    -1 
   10     4     let mut cur = 0;
   11     5     let mut v = vec![];
   12     6 
   13    -1     for line in BufReader::new(file).lines() {
   14    -1         match line.unwrap().parse::<i32>() {
   -1     7     for line in lib::iter_input() {
   -1     8         match line.parse::<i32>() {
   15     9             Ok(i) => {
   16    10                 cur += i;
   17    11             },

diff --git a/2022/02/part1.rs b/2022/02/part1.rs

@@ -1,23 +1,16 @@
    1    -1 use std::env::args;
    2    -1 use std::fs::File;
    3    -1 use std::io::BufRead;
    4    -1 use std::io::BufReader;
   -1     1 #[path = "../lib.rs"] mod lib;
    5     2 
    6     3 fn main() {
    7    -1     let path = args().nth(1).unwrap();
    8    -1     let file = File::open(path).unwrap();
    9    -1 
   10     4     let mut score = 0;
   11     5 
   12    -1     for line in BufReader::new(file).lines() {
   13    -1         let l = line.unwrap();
   14    -1         let opponent = match l.chars().nth(0) {
   -1     6     for line in lib::iter_input() {
   -1     7         let opponent = match line.chars().nth(0) {
   15     8             Some('A') => 0,
   16     9             Some('B') => 1,
   17    10             Some('C') => 2,
   18    11             _ => unreachable!(),
   19    12         };
   20    -1         let own = match l.chars().nth(2) {
   -1    13         let own = match line.chars().nth(2) {
   21    14             Some('X') => 0,
   22    15             Some('Y') => 1,
   23    16             Some('Z') => 2,

diff --git a/2022/02/part2.rs b/2022/02/part2.rs

@@ -1,23 +1,16 @@
    1    -1 use std::env::args;
    2    -1 use std::fs::File;
    3    -1 use std::io::BufRead;
    4    -1 use std::io::BufReader;
   -1     1 #[path = "../lib.rs"] mod lib;
    5     2 
    6     3 fn main() {
    7    -1     let path = args().nth(1).unwrap();
    8    -1     let file = File::open(path).unwrap();
    9    -1 
   10     4     let mut score = 0;
   11     5 
   12    -1     for line in BufReader::new(file).lines() {
   13    -1         let l = line.unwrap();
   14    -1         let opponent = match l.chars().nth(0) {
   -1     6     for line in lib::iter_input() {
   -1     7         let opponent = match line.chars().nth(0) {
   15     8             Some('A') => 0,
   16     9             Some('B') => 1,
   17    10             Some('C') => 2,
   18    11             _ => unreachable!(),
   19    12         };
   20    -1         let match_score = match l.chars().nth(2) {
   -1    13         let match_score = match line.chars().nth(2) {
   21    14             Some('X') => 0,
   22    15             Some('Y') => 1,
   23    16             Some('Z') => 2,

diff --git a/2022/lib.rs b/2022/lib.rs

@@ -0,0 +1,10 @@
   -1     1 use std::env::args;
   -1     2 use std::fs::File;
   -1     3 use std::io::BufRead;
   -1     4 use std::io::BufReader;
   -1     5 
   -1     6 pub fn iter_input() -> impl Iterator<Item = String> {
   -1     7     let path = args().nth(1).unwrap();
   -1     8     let file = File::open(path).unwrap();
   -1     9     return BufReader::new(file).lines().map(|l| l.unwrap());
   -1    10 }