init
This commit is contained in:
72
d09/src/a1.rs
Normal file
72
d09/src/a1.rs
Normal file
@ -0,0 +1,72 @@
|
||||
|
||||
fn move_tail(head :&(i32, i32), tail :&mut (i32, i32)) {
|
||||
|
||||
let dx = head.0 - tail.0;
|
||||
let dy = head.1 - tail.1;
|
||||
|
||||
if dx.abs() > 1 || dy.abs() > 1{
|
||||
tail.0 += dx.signum();
|
||||
tail.1 += dy.signum();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn execute_move(head :&mut (i32, i32), tail :&mut (i32, i32), cmd :&char) {
|
||||
|
||||
match cmd {
|
||||
|
||||
'U' => {
|
||||
head.1 += 1;
|
||||
}
|
||||
|
||||
'D' => {
|
||||
head.1 -= 1;
|
||||
}
|
||||
|
||||
'L' => {
|
||||
head.0 -= 1;
|
||||
}
|
||||
|
||||
'R' => {
|
||||
head.0 += 1;
|
||||
}
|
||||
|
||||
_ => {}
|
||||
}
|
||||
|
||||
move_tail(head, tail);
|
||||
|
||||
}
|
||||
|
||||
fn parse_command(inp :&String) -> (char, i32) {
|
||||
|
||||
let split = inp.split(' ').collect::<Vec<&str>>();
|
||||
return (split[0].chars().nth(0).unwrap(), split[1].parse::<i32>().unwrap());
|
||||
}
|
||||
|
||||
pub fn run(inp :Vec<String>) {
|
||||
|
||||
let mut visited_coordinates :Vec<(i32, i32)> = vec![];
|
||||
let mut head_pos = (0, 0);
|
||||
let mut tail_pos = (0, 0);
|
||||
|
||||
visited_coordinates.push(tail_pos.clone());
|
||||
|
||||
for curr_move in inp {
|
||||
let commands = parse_command(&curr_move);
|
||||
|
||||
for _ in 0..commands.1 {
|
||||
|
||||
execute_move(&mut head_pos, &mut tail_pos, &commands.0);
|
||||
|
||||
if !visited_coordinates.contains(&tail_pos) {
|
||||
visited_coordinates.push(tail_pos.clone());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
println!("a1: {}", visited_coordinates.len());
|
||||
}
|
83
d09/src/a2.rs
Normal file
83
d09/src/a2.rs
Normal file
@ -0,0 +1,83 @@
|
||||
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
struct Pos {
|
||||
x :i32,
|
||||
y :i32
|
||||
}
|
||||
|
||||
fn move_tail(head :&Pos, tail :&mut Pos) {
|
||||
|
||||
let dx = head.x - tail.x;
|
||||
let dy = head.y - tail.y;
|
||||
|
||||
if dx.abs() > 1 || dy.abs() > 1{
|
||||
tail.x += dx.signum();
|
||||
tail.y += dy.signum();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn execute_move(head :&mut Pos, tails :&mut Vec<Pos>, cmd :&char) {
|
||||
|
||||
match cmd {
|
||||
|
||||
'U' => {
|
||||
head.y += 1;
|
||||
}
|
||||
|
||||
'D' => {
|
||||
head.y -= 1;
|
||||
}
|
||||
|
||||
'L' => {
|
||||
head.x -= 1;
|
||||
}
|
||||
|
||||
'R' => {
|
||||
head.x += 1;
|
||||
}
|
||||
|
||||
_ => {}
|
||||
}
|
||||
|
||||
move_tail(head, &mut tails.first_mut().unwrap());
|
||||
|
||||
for i in 1..tails.len() {
|
||||
|
||||
move_tail(&tails[i-1].clone(), &mut tails[i]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn parse_command(inp :&String) -> (char, i32) {
|
||||
|
||||
let split = inp.split(' ').collect::<Vec<&str>>();
|
||||
return (split[0].chars().nth(0).unwrap(), split[1].parse::<i32>().unwrap());
|
||||
}
|
||||
|
||||
pub fn run(inp :Vec<String>) {
|
||||
|
||||
let mut visited_coordinates :Vec<Pos> = vec![];
|
||||
let mut head_pos = Pos {x: 0, y: 0} ;
|
||||
let mut tails :Vec<Pos> = vec![Pos {x: 0, y: 0}; 9];
|
||||
|
||||
visited_coordinates.push(head_pos.clone());
|
||||
|
||||
for curr_move in inp {
|
||||
let commands = parse_command(&curr_move);
|
||||
|
||||
for _ in 0..commands.1 {
|
||||
|
||||
execute_move(&mut head_pos, &mut tails, &commands.0);
|
||||
|
||||
if !visited_coordinates.contains(&tails.last().unwrap()) {
|
||||
visited_coordinates.push(tails.last().unwrap().clone());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
println!("a2: {}", visited_coordinates.len());
|
||||
}
|
41
d09/src/main.rs
Normal file
41
d09/src/main.rs
Normal file
@ -0,0 +1,41 @@
|
||||
use std::io::BufRead;
|
||||
|
||||
mod a1;
|
||||
mod a2;
|
||||
|
||||
fn read_file(path :&str) -> Vec<String> {
|
||||
|
||||
let file = std::fs::File::open(path);
|
||||
|
||||
return match file {
|
||||
|
||||
Ok(handle) => {
|
||||
|
||||
let reader = std::io::BufReader::new(handle);
|
||||
|
||||
let mut vec : Vec<String> = vec![];
|
||||
|
||||
reader.lines().for_each(|elem| {
|
||||
vec.push(elem.unwrap());
|
||||
});
|
||||
|
||||
vec
|
||||
|
||||
}
|
||||
|
||||
Err(_) => vec![]
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
let inp :Vec<String> = read_file("input.txt");
|
||||
|
||||
a1::run(inp.clone());
|
||||
|
||||
a2::run(inp);
|
||||
|
||||
}
|
Reference in New Issue
Block a user