Day 2 working
This commit is contained in:
parent
80f32ef868
commit
3b7d3c5462
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,44 @@
|
|||
use std::error::Error;
|
||||
use std::fs::File;
|
||||
use std::io::{self, BufRead};
|
||||
use std::vec::Vec;
|
||||
|
||||
fn to_moves(line: String) -> (i32, i32) {
|
||||
let mut parts = line.split(" ");
|
||||
let dir = parts.nth(0).unwrap();
|
||||
let arg: i32 = parts.nth(0).unwrap().parse().unwrap();
|
||||
|
||||
match dir {
|
||||
"up" => (0, -arg),
|
||||
"down" => (0, arg),
|
||||
"forward" => (arg, 0),
|
||||
_ => panic!("Unsupported Direction: {}", dir),
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
let file = File::open("inputs/day2.txt")?;
|
||||
let lines = io::BufReader::new(file).lines().map(|l| l.unwrap());
|
||||
|
||||
let moves: Vec<(i32, i32)> = lines.map(|l| to_moves(l)).collect();
|
||||
let pos = moves
|
||||
.iter()
|
||||
.fold((0, 0), |(ax, ay), (bx, by)| (ax + bx, ay + by));
|
||||
|
||||
println!("Answer1: {}", pos.0 * pos.1);
|
||||
|
||||
let mut aim = 0;
|
||||
let mut pos = (0, 0);
|
||||
for m in moves.iter() {
|
||||
aim = aim + m.1;
|
||||
println!("{} {}", m.0, m.1);
|
||||
|
||||
pos = (pos.0 + m.0, pos.1 + m.0 * aim);
|
||||
println!("{} {}", pos.0, pos.1);
|
||||
|
||||
}
|
||||
|
||||
println!("Answer2: {}", pos.0 * pos.1);
|
||||
|
||||
Ok(())
|
||||
}
|
Loading…
Reference in New Issue