2016: 01
This commit is contained in:
parent
b72d705cf4
commit
11c4b68ebc
14
2016/.gitignore
vendored
Normal file
14
2016/.gitignore
vendored
Normal file
@ -0,0 +1,14 @@
|
||||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
debug/
|
||||
target/
|
||||
|
||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||
Cargo.lock
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
||||
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||
*.pdb
|
8
2016/aoc01/Cargo.toml
Normal file
8
2016/aoc01/Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "aoc01"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
1
2016/aoc01/input
Normal file
1
2016/aoc01/input
Normal file
@ -0,0 +1 @@
|
||||
R4, R4, L1, R3, L5, R2, R5, R1, L4, R3, L5, R2, L3, L4, L3, R1, R5, R1, L3, L1, R3, L1, R2, R2, L2, R5, L3, L4, R4, R4, R2, L4, L1, R5, L1, L4, R4, L1, R1, L2, R5, L2, L3, R2, R1, L194, R2, L4, R49, R1, R3, L5, L4, L1, R4, R2, R1, L5, R3, L5, L4, R4, R4, L2, L3, R78, L5, R4, R191, R4, R3, R1, L2, R1, R3, L1, R3, R4, R2, L2, R1, R4, L5, R2, L2, L4, L2, R1, R2, L3, R5, R2, L3, L3, R3, L1, L1, R5, L4, L4, L2, R5, R1, R4, L3, L5, L4, R5, L4, R5, R4, L3, L2, L5, R4, R3, L3, R1, L5, R5, R1, L3, R2, L5, R5, L3, R1, R4, L5, R4, R2, R3, L4, L5, R3, R4, L5, L5, R4, L4, L4, R1, R5, R3, L1, L4, L3, L4, R1, L5, L1, R2, R2, R4, R4, L5, R4, R1, L1, L1, L3, L5, L2, R4, L3, L5, L4, L1, R3
|
78
2016/aoc01/src/main.rs
Normal file
78
2016/aoc01/src/main.rs
Normal file
@ -0,0 +1,78 @@
|
||||
use std::collections::HashMap;
|
||||
use std::{fs, io, process};
|
||||
use std::io::BufRead;
|
||||
|
||||
enum Direction {
|
||||
Right,
|
||||
Left
|
||||
}
|
||||
|
||||
struct Instruction {
|
||||
direction: Direction,
|
||||
steps: i32,
|
||||
}
|
||||
|
||||
fn read_instructions(filename: &str) -> Vec<Instruction> {
|
||||
let mut vec: Vec<Instruction> = Vec::new();
|
||||
|
||||
let file = fs::File::open(filename).expect("File not found");
|
||||
|
||||
let lines = io::BufReader::new(file).lines();
|
||||
for line_res in lines {
|
||||
if let Ok(line) = line_res {
|
||||
let instructions = line.split(", ");
|
||||
for instruction in instructions {
|
||||
let direction = match instruction.chars().nth(0) {
|
||||
Some('L') => Direction::Left,
|
||||
Some('R') => Direction::Right,
|
||||
_ => process::exit(1), // not elegant
|
||||
};
|
||||
vec.push(Instruction{direction: direction, steps: instruction.to_string()[1..].parse::<i32>().unwrap()});
|
||||
}
|
||||
}
|
||||
}
|
||||
return vec;
|
||||
}
|
||||
|
||||
fn part1(instructions: &Vec<Instruction>) -> i32 {
|
||||
let mut position = (0,0);
|
||||
let mut direction = (1,0);
|
||||
for instruction in instructions {
|
||||
direction = match instruction.direction {
|
||||
Direction::Right => (-direction.1, direction.0),
|
||||
Direction::Left => (direction.1, -direction.0),
|
||||
};
|
||||
position = (position.0 + instruction.steps * direction.0, position.1 + instruction.steps * direction.1);
|
||||
}
|
||||
|
||||
return position.0.abs() + position.1.abs();
|
||||
}
|
||||
|
||||
fn part2(instructions: &Vec<Instruction>) -> i32 {
|
||||
let mut position = (0,0);
|
||||
let mut direction = (1,0);
|
||||
let mut map: HashMap<(i32, i32), i32> = HashMap::new();
|
||||
map.insert((0,0), 1);
|
||||
for instruction in instructions {
|
||||
direction = match instruction.direction {
|
||||
Direction::Right => (-direction.1, direction.0),
|
||||
Direction::Left => (direction.1, -direction.0),
|
||||
};
|
||||
for _ in 0..instruction.steps {
|
||||
position = (position.0 + direction.0, position.1 + direction.1);
|
||||
if map.contains_key(&position) {
|
||||
return position.0.abs() + position.1.abs();
|
||||
} else {
|
||||
map.insert(position, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let instructions = read_instructions("input");
|
||||
println!("The Easter Bunny HQ is {} blocks away", part1(&instructions));
|
||||
println!("The Easter Bunny HQ is actually {} blocks away", part2(&instructions));
|
||||
}
|
Loading…
Reference in New Issue
Block a user