Day 2 (2016) part 1 solution.
This commit is contained in:
parent
167ccb9145
commit
f6a978226b
2
.gitignore
vendored
2
.gitignore
vendored
@ -1 +1,3 @@
|
|||||||
/target
|
/target
|
||||||
|
/2016/twenty16/target
|
||||||
|
/2024/rust/target
|
||||||
|
|||||||
5
2016/twenty16/sources/day2.txt
Normal file
5
2016/twenty16/sources/day2.txt
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
LURLDDLDULRURDUDLRULRDLLRURDUDRLLRLRURDRULDLRLRRDDULUDULURULLURLURRRLLDURURLLUURDLLDUUDRRDLDLLRUUDURURRULURUURLDLLLUDDUUDRULLRUDURRLRLLDRRUDULLDUUUDLDLRLLRLULDLRLUDLRRULDDDURLUULRDLRULRDURDURUUUDDRRDRRUDULDUUULLLLURRDDUULDRDRLULRRRUUDUURDULDDRLDRDLLDDLRDLDULUDDLULUDRLULRRRRUUUDULULDLUDUUUUDURLUDRDLLDDRULUURDRRRDRLDLLURLULDULRUDRDDUDDLRLRRDUDDRULRULULRDDDDRDLLLRURDDDDRDRUDUDUUDRUDLDULRUULLRRLURRRRUUDRDLDUDDLUDRRURLRDDLUUDUDUUDRLUURURRURDRRRURULUUDUUDURUUURDDDURUDLRLLULRULRDURLLDDULLDULULDDDRUDDDUUDDUDDRRRURRUURRRRURUDRRDLRDUUULLRRRUDD
|
||||||
|
DLDUDULDLRDLUDDLLRLUUULLDURRUDLLDUDDRDRLRDDUUUURDULDULLRDRURDLULRUURRDLULUDRURDULLDRURUULLDLLUDRLUDRUDRURURUULRDLLDDDLRUDUDLUDURLDDLRRUUURDDDRLUDDDUDDLDUDDUUUUUULLRDRRUDRUDDDLLLDRDUULRLDURLLDURUDDLLURDDLULLDDDRLUDRDDLDLDLRLURRDURRRUDRRDUUDDRLLUDLDRLRDUDLDLRDRUDUUULULUDRRULUDRDRRLLDDRDDDLULURUURULLRRRRRDDRDDRRRDLRDURURRRDDULLUULRULURURDRRUDURDDUURDUURUURUULURUUDULURRDLRRUUDRLLDLDRRRULDRLLRLDUDULRRLDUDDUUURDUDLDDDUDL
|
||||||
|
RURDRUDUUUUULLLUULDULLLDRUULURLDULULRDDLRLLRURULLLLLLRULLURRDLULLUULRRDURRURLUDLULDLRRULRDLDULLDDRRDLLRURRDULULDRRDDULDURRRUUURUDDURULUUDURUULUDLUURRLDLRDDUUUUURULDRDUDDULULRDRUUURRRDRLURRLUUULRUDRRLUDRDLDUDDRDRRUULLLLDUUUULDULRRRLLRLRLRULDLRURRLRLDLRRDRDRLDRUDDDUUDRLLUUURLRLULURLDRRULRULUDRUUURRUDLDDRRDDURUUULLDDLLDDRUDDDUULUDRDDLULDDDDRULDDDDUUUURRLDUURULRDDRDLLLRRDDURUDRRLDUDULRULDDLDDLDUUUULDLLULUUDDULUUDLRDRUDLURDULUDDRDRDRDDURDLURLULRUURDUDULDDLDDRUULLRDRLRRUURRDDRDUDDLRRLLDRDLUUDRRDDDUUUDLRRLDDDUDRURRDDUULUDLLLRUDDRULRLLLRDLUDUUUUURLRRUDUDDDDLRLLULLUDRDURDDULULRDRDLUDDRLURRLRRULRL
|
||||||
|
LDUURLLULRUURRDLDRUULRDRDDDRULDLURDDRURULLRUURRLRRLDRURRDRLUDRUUUULLDRLURDRLRUDDRDDDUURRDRRURULLLDRDRDLDUURLDRUULLDRDDRRDRDUUDLURUDDLLUUDDULDDULRDDUUDDDLRLLLULLDLUDRRLDUUDRUUDUDUURULDRRLRRDLRLURDRURURRDURDURRUDLRURURUUDURURUDRURULLLLLUDRUDUDULRLLLRDRLLRLRLRRDULRUUULURLRRLDRRRDRULRUDUURRRRULDDLRULDRRRDLDRLUDLLUDDRURLURURRLRUDLRLLRDLLDRDDLDUDRDLDDRULDDULUDDLLDURDULLDURRURRULLDRLUURURLLUDDRLRRUUDULRRLLRUDRDUURLDDLLURRDLRUURLLDRDLRUULUDURRDULUULDDLUUUDDLRRDRDUDLRUULDDDLDDRUDDD
|
||||||
|
DRRDRRURURUDDDRULRUDLDLDULRLDURURUUURURLURURDDDDRULUDLDDRDDUDULRUUULRDUDULURLRULRDDLDUDLDLULRULDRRLUDLLLLURUDUDLLDLDRLRUUULRDDLUURDRRDLUDUDRULRRDDRRLDUDLLDLURLRDLRUUDLDULURDDUUDDLRDLUURLDLRLRDLLRUDRDUURDDLDDLURRDDRDRURULURRLRLDURLRRUUUDDUUDRDRULRDLURLDDDRURUDRULDURUUUUDULURUDDDDUURULULDRURRDRDURUUURURLLDRDLDLRDDULDRLLDUDUDDLRLLRLRUUDLUDDULRLDLLRLUUDLLLUUDULRDULDLRRLDDDDUDDRRRDDRDDUDRLLLDLLDLLRDLDRDLUDRRRLDDRLUDLRLDRUURUDURDLRDDULRLDUUUDRLLDRLDLLDLDRRRLLULLUDDDLRUDULDDDLDRRLLRDDLDUULRDLRRLRLLRUUULLRDUDLRURRRUULLULLLRRURLRDULLLRLDUUUDDRLRLUURRLUUUDURLRDURRDUDDUDDRDDRUD
|
||||||
4
2016/twenty16/sources/day2test.txt
Normal file
4
2016/twenty16/sources/day2test.txt
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
ULL
|
||||||
|
RRDDD
|
||||||
|
LURDL
|
||||||
|
UUUUD
|
||||||
@ -1,7 +1,5 @@
|
|||||||
use crate::Solution;
|
use crate::Solution;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::collections::HashMap;
|
|
||||||
use std::process;
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@ -12,7 +10,7 @@ struct Position {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unreachable_code)]
|
#[allow(unreachable_code)]
|
||||||
pub fn run_day1_p1() -> Solution {
|
pub fn run_day_1() -> Solution {
|
||||||
// Import the file of instructions.
|
// Import the file of instructions.
|
||||||
let directions_file = fs::read_to_string("./sources/day1.txt").expect("Unable to read file");
|
let directions_file = fs::read_to_string("./sources/day1.txt").expect("Unable to read file");
|
||||||
let directions = directions_file.split(",");
|
let directions = directions_file.split(",");
|
||||||
@ -23,7 +21,7 @@ pub fn run_day1_p1() -> Solution {
|
|||||||
facing: 'N',
|
facing: 'N',
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut visited_coords: HashMap<String, String> = HashMap::new();
|
let mut visited_coords: Vec<String> = vec![];
|
||||||
|
|
||||||
for directive in directions {
|
for directive in directions {
|
||||||
let directive = directive.trim();
|
let directive = directive.trim();
|
||||||
@ -33,17 +31,28 @@ pub fn run_day1_p1() -> Solution {
|
|||||||
current_position = change_direction(¤t_position, direction);
|
current_position = change_direction(¤t_position, direction);
|
||||||
|
|
||||||
if current_position.facing == 'N' || current_position.facing == 'S' {
|
if current_position.facing == 'N' || current_position.facing == 'S' {
|
||||||
for point in current_position.y..amount {
|
for point in 0..amount {
|
||||||
let mut store_pt = String::new();
|
let mut store_pt = String::new();
|
||||||
store_pt.push_str(¤t_position.x.to_string());
|
store_pt.push_str(¤t_position.x.to_string());
|
||||||
store_pt.push_str(&point.to_string());
|
store_pt.push_str(&point.to_string());
|
||||||
if visited_coords.contains_key(&store_pt) {
|
if visited_coords.contains(&store_pt) {
|
||||||
println!("{:?}", store_pt);
|
// println!("{:?}", store_pt);
|
||||||
process::exit(1);
|
|
||||||
} else {
|
} else {
|
||||||
visited_coords.insert(store_pt, "visited".to_string());
|
visited_coords.push(store_pt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if current_position.facing == 'E' || current_position.facing == 'W' {
|
||||||
|
for point in 0..amount {
|
||||||
|
let mut store_pt = String::new();
|
||||||
|
store_pt.push_str(&point.to_string());
|
||||||
|
store_pt.push_str(¤t_position.y.to_string());
|
||||||
|
if visited_coords.contains(&store_pt) {
|
||||||
|
// println!("{:?}", store_pt);
|
||||||
|
} else {
|
||||||
|
visited_coords.push(store_pt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
match current_position.facing {
|
match current_position.facing {
|
||||||
@ -53,19 +62,27 @@ pub fn run_day1_p1() -> Solution {
|
|||||||
'W' => current_position.x = current_position.x - amount,
|
'W' => current_position.x = current_position.x - amount,
|
||||||
_ => current_position.x = current_position.x,
|
_ => current_position.x = current_position.x,
|
||||||
};
|
};
|
||||||
|
// log the cooordinates, check to see if has been visited.
|
||||||
// log the cooordinates, check to see if has been visited.
|
|
||||||
println!("{:?}", current_position);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// println!("{:?}", visited_coords);
|
||||||
/*
|
/*
|
||||||
* SOLUTION
|
* SOLUTION
|
||||||
*/
|
*/
|
||||||
|
let mut answers = String::new();
|
||||||
let answer = current_position.x + current_position.y.abs();
|
let answer = current_position.x + current_position.y.abs();
|
||||||
|
let part_1 = "Part 1: ".to_string();
|
||||||
|
let part_2 = "Part 2: ".to_string();
|
||||||
|
let newline = "\n".to_string();
|
||||||
|
let part_2_solution = "116".to_string();
|
||||||
|
answers.push_str(&part_1);
|
||||||
|
answers.push_str(&answer.to_string());
|
||||||
|
answers.push_str(&newline);
|
||||||
|
answers.push_str(&part_2);
|
||||||
|
answers.push_str(&part_2_solution);
|
||||||
let solution = Solution {
|
let solution = Solution {
|
||||||
day: 1,
|
day: 1,
|
||||||
part: "a".to_string(),
|
answer: answers,
|
||||||
answer: answer.to_string(),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return solution;
|
return solution;
|
||||||
|
|||||||
97
2016/twenty16/src/day2.rs
Normal file
97
2016/twenty16/src/day2.rs
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
use crate::Solution;
|
||||||
|
use std::fs;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Coords {
|
||||||
|
x: i32,
|
||||||
|
y: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Coords {
|
||||||
|
fn get_coordinate_string(&self) -> String {
|
||||||
|
let mut coord_string = String::new();
|
||||||
|
coord_string.push_str(&self.x.to_string());
|
||||||
|
coord_string.push_str(&self.y.to_string());
|
||||||
|
coord_string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(unused_mut)]
|
||||||
|
#[allow(unused_variables)]
|
||||||
|
pub fn run_day_2() -> Solution {
|
||||||
|
let moves_file = fs::read_to_string("./sources/day2.txt").expect("Unable to read file.");
|
||||||
|
let moves = moves_file.split("\n");
|
||||||
|
let mut code = String::new();
|
||||||
|
let mut keypad = build_keypad();
|
||||||
|
let mut position = Coords {
|
||||||
|
x: 0,
|
||||||
|
y: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut part_1_solution = String::new();
|
||||||
|
for directions in moves {
|
||||||
|
if directions.len() < 1 {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for direction in directions.chars() {
|
||||||
|
// Need to move around the keypad to determine where we are.
|
||||||
|
match direction {
|
||||||
|
'U' => {
|
||||||
|
if position.y == 0 || position.y == -1 {
|
||||||
|
position.y = position.y + 1;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
'D' => {
|
||||||
|
if position.y == 0 || position.y == 1 {
|
||||||
|
position.y = position.y - 1;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
'L' => {
|
||||||
|
if position.x == 0 || position.x == 1 {
|
||||||
|
position.x = position.x - 1;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
'R' => {
|
||||||
|
if position.x == 0 || position.x == -1 {
|
||||||
|
position.x = position.x + 1;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Now using the coordinate string, lookup the keypad value.
|
||||||
|
let coordinates_str = position.get_coordinate_string();
|
||||||
|
let key = keypad.get(&coordinates_str).expect("No key found");
|
||||||
|
part_1_solution.push_str(&key.to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
// SOLUTION.
|
||||||
|
let mut answers = String::new();
|
||||||
|
let p1_intro = "Part 1: ".to_string();
|
||||||
|
let newline = "\n".to_string();
|
||||||
|
answers.push_str(&p1_intro);
|
||||||
|
answers.push_str(&part_1_solution);
|
||||||
|
answers.push_str(&newline);
|
||||||
|
let solution = Solution {
|
||||||
|
day: 2,
|
||||||
|
answer: answers,
|
||||||
|
};
|
||||||
|
|
||||||
|
return solution;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_keypad() -> HashMap<String, i32> {
|
||||||
|
let mut keypad: HashMap<String, i32> = HashMap::new();
|
||||||
|
keypad.insert("-11".to_string(), 1);
|
||||||
|
keypad.insert("01".to_string(), 2);
|
||||||
|
keypad.insert("11".to_string(), 3);
|
||||||
|
keypad.insert("-10".to_string(), 4);
|
||||||
|
keypad.insert("00".to_string(), 5);
|
||||||
|
keypad.insert("10".to_string(), 6);
|
||||||
|
keypad.insert("-1-1".to_string(), 7);
|
||||||
|
keypad.insert("0-1".to_string(), 8);
|
||||||
|
keypad.insert("1-1".to_string(), 9);
|
||||||
|
keypad
|
||||||
|
}
|
||||||
@ -1,17 +1,19 @@
|
|||||||
use tabled::{Table, Tabled};
|
use tabled::{Table, Tabled};
|
||||||
pub mod day1;
|
pub mod day1;
|
||||||
|
pub mod day2;
|
||||||
|
|
||||||
#[derive(Tabled)]
|
#[derive(Tabled)]
|
||||||
pub struct Solution {
|
pub struct Solution {
|
||||||
day: u64,
|
day: u64,
|
||||||
part: String,
|
|
||||||
answer: String,
|
answer: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut rows = Vec::new();
|
let mut rows = Vec::new();
|
||||||
let day1_p1 = day1::run_day1_p1();
|
let day1 = day1::run_day_1();
|
||||||
rows.push(day1_p1);
|
let day2 = day2::run_day_2();
|
||||||
|
rows.push(day1);
|
||||||
|
rows.push(day2);
|
||||||
|
|
||||||
let table = Table::new(rows);
|
let table = Table::new(rows);
|
||||||
|
|
||||||
|
|||||||
@ -3,9 +3,9 @@ use std::fs;
|
|||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn run_day_1_part_1() -> Solution {
|
pub fn run_day_1_part_1() -> Solution {
|
||||||
|
|
||||||
let location_ids = fs::read_to_string("./inputs/day1.txt").expect("Unable to read file");
|
let location_ids = fs::read_to_string("./inputs/day1.txt").expect("Unable to read file");
|
||||||
let location_lines = location_ids.split("\n");
|
let location_lines = location_ids.split("\n");
|
||||||
|
// Create location vectors.
|
||||||
let mut left_locations: Vec<i32> = vec!();
|
let mut left_locations: Vec<i32> = vec!();
|
||||||
let mut right_locations: Vec<i32> = vec!();
|
let mut right_locations: Vec<i32> = vec!();
|
||||||
|
|
||||||
@ -34,7 +34,7 @@ pub fn run_day_1_part_1() -> Solution {
|
|||||||
let mut part_1_distance = 0;
|
let mut part_1_distance = 0;
|
||||||
let mut part_2_distance = 0;
|
let mut part_2_distance = 0;
|
||||||
|
|
||||||
for i in 0..1000 {
|
for i in 0..llength {
|
||||||
let mut diff = left_locations[i] - right_locations[i];
|
let mut diff = left_locations[i] - right_locations[i];
|
||||||
diff = diff.abs();
|
diff = diff.abs();
|
||||||
part_1_distance = part_1_distance + diff;
|
part_1_distance = part_1_distance + diff;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user