Start day 6
This commit is contained in:
7
day_06/Cargo.lock
generated
Normal file
7
day_06/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "day_06"
|
||||||
|
version = "0.1.0"
|
||||||
10
day_06/small_input.txt
Normal file
10
day_06/small_input.txt
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
....#.....
|
||||||
|
.........#
|
||||||
|
..........
|
||||||
|
..#.......
|
||||||
|
.......#..
|
||||||
|
..........
|
||||||
|
.#..^.....
|
||||||
|
........#.
|
||||||
|
#.........
|
||||||
|
......#...
|
||||||
@@ -1,3 +1,60 @@
|
|||||||
fn main() {
|
use std::{
|
||||||
println!("Hello, world!");
|
env,
|
||||||
|
error::Error,
|
||||||
|
fs::File,
|
||||||
|
io::{BufReader, prelude::*},
|
||||||
|
};
|
||||||
|
|
||||||
|
fn load_map(input_file: &str) -> Result<(Vec<Vec<bool>>, (usize, usize)), Box<dyn Error>> {
|
||||||
|
let buffer = BufReader::new(File::open(input_file)?);
|
||||||
|
let mut lines = buffer.lines().peekable();
|
||||||
|
|
||||||
|
let width = match lines.peek() {
|
||||||
|
Some(l) => match l {
|
||||||
|
Ok(li) => li.len(),
|
||||||
|
Err(_) => Err("Error reading first line of map file")?,
|
||||||
|
},
|
||||||
|
None => Err("Cannot load empty map file.")?,
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut data: Vec<Vec<bool>> = Vec::new();
|
||||||
|
let mut guard_position = (0, 0);
|
||||||
|
|
||||||
|
for (y, line) in lines.enumerate() {
|
||||||
|
let line = line?;
|
||||||
|
if line.len() != width {
|
||||||
|
println!("{}", line);
|
||||||
|
Err("Line length of input are not consistent")?;
|
||||||
|
}
|
||||||
|
let parsed_data: Vec<bool> = line
|
||||||
|
.chars()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(x, c)| {
|
||||||
|
if c == '^' {
|
||||||
|
guard_position = (x, y);
|
||||||
|
};
|
||||||
|
c == '#'
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
data.push(parsed_data);
|
||||||
|
}
|
||||||
|
let height = data.len();
|
||||||
|
|
||||||
|
Ok((data, guard_position))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
// Handle command input
|
||||||
|
let args: Vec<String> = env::args().collect();
|
||||||
|
if args.len() != 2 {
|
||||||
|
panic!(
|
||||||
|
"{} must be run with a single argument of files name!",
|
||||||
|
&args[0]
|
||||||
|
)
|
||||||
|
}
|
||||||
|
let input_file = &args[1];
|
||||||
|
let data = load_map(input_file)?;
|
||||||
|
println!("{:?}", data);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user