Day 1
This commit is contained in:
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
*/input
|
||||||
|
*/part1
|
||||||
|
*/part2
|
30
01/part1.rs
Normal file
30
01/part1.rs
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
use std::error::Error;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::BufReader;
|
||||||
|
use std::io::prelude::*;
|
||||||
|
|
||||||
|
fn main() -> std::io::Result<()> {
|
||||||
|
let file = match File::open("input") {
|
||||||
|
// The `description` method of `io::Error` returns a string that describes the error
|
||||||
|
Err(why) => panic!("couldn't open input: {}", Error::description(&why)),
|
||||||
|
Ok(file) => file,
|
||||||
|
};
|
||||||
|
|
||||||
|
let reader = BufReader::new(file);
|
||||||
|
let lines = reader.lines();
|
||||||
|
|
||||||
|
let mut total: i128 = 0;
|
||||||
|
|
||||||
|
for result in lines {
|
||||||
|
let l = result.unwrap();
|
||||||
|
let offset:i128 = i128::from_str_radix(&l, 10).unwrap();
|
||||||
|
|
||||||
|
println!("{} => {}", l, offset);
|
||||||
|
|
||||||
|
total = total + offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{}", total);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
45
01/part2.rs
Normal file
45
01/part2.rs
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
use std::collections::HashSet;
|
||||||
|
use std::error::Error;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::BufReader;
|
||||||
|
use std::io::SeekFrom;
|
||||||
|
use std::io::prelude::*;
|
||||||
|
|
||||||
|
fn main() -> std::io::Result<()> {
|
||||||
|
let mut file = match File::open("input") {
|
||||||
|
// The `description` method of `io::Error` returns a string that describes the error
|
||||||
|
Err(why) => panic!("couldn't open input: {}", Error::description(&why)),
|
||||||
|
Ok(file) => file,
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut repeat = true;
|
||||||
|
let mut total: i128 = 0;
|
||||||
|
let mut seen = HashSet::<i128>::new();
|
||||||
|
seen.insert(0);
|
||||||
|
|
||||||
|
while repeat {
|
||||||
|
file.seek(SeekFrom::Start(0));
|
||||||
|
let reader = BufReader::new(&file);
|
||||||
|
let lines = reader.lines();
|
||||||
|
|
||||||
|
for result in lines {
|
||||||
|
let l = result.unwrap();
|
||||||
|
let offset:i128 = i128::from_str_radix(&l, 10).unwrap();
|
||||||
|
|
||||||
|
total = total + offset;
|
||||||
|
println!("{} => {}", offset, total);
|
||||||
|
|
||||||
|
if seen.contains(&total) {
|
||||||
|
println!("seen {} before", total);
|
||||||
|
repeat = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
seen.insert(total);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{}", total);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
Reference in New Issue
Block a user