1
0
Fork 0

cargo fmt

main
Andrew Coleman 2018-12-13 15:00:47 -05:00
parent 0d71008a98
commit 92cdd760e2
5 changed files with 76 additions and 24 deletions

View File

@ -9,8 +9,18 @@ fn get_coords_from_line(line: &String) -> (usize, usize, usize, usize) {
let top_x = top_left_pieces.get(0).unwrap().parse::<usize>().unwrap();
let top_y = top_left_pieces.get(1).unwrap().parse::<usize>().unwrap();
let bottom_right_pieces: Vec<&str> = piece_dims.get(1).unwrap().split("x").collect();
let bottom_x = top_x + bottom_right_pieces.get(0).unwrap().parse::<usize>().unwrap();
let bottom_y = top_y + bottom_right_pieces.get(1).unwrap().parse::<usize>().unwrap();
let bottom_x = top_x
+ bottom_right_pieces
.get(0)
.unwrap()
.parse::<usize>()
.unwrap();
let bottom_y = top_y
+ bottom_right_pieces
.get(1)
.unwrap()
.parse::<usize>()
.unwrap();
(top_x, top_y, bottom_x, bottom_y)
}

View File

@ -32,7 +32,10 @@ fn calc_most_significant_monomer(polymers: &str) {
min_length = new_len;
}
}
println!("minimum new length {} found by replacing {}", min_length, min_letter);
println!(
"minimum new length {} found by replacing {}",
min_length, min_letter
);
}
fn react_monomer(polymers: &str, r: Regex) -> usize {

View File

@ -1,7 +1,7 @@
use std::collections::HashMap;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::iter::Iterator;
use std::collections::HashMap;
struct Point {
x: usize,
@ -20,7 +20,13 @@ impl std::fmt::Display for Point {
}
}
fn calc_max_area(points: &Vec<Point>, min_x: usize, min_y: usize, max_x: usize, max_y: usize) -> (u8, usize) {
fn calc_max_area(
points: &Vec<Point>,
min_x: usize,
min_y: usize,
max_x: usize,
max_y: usize,
) -> (u8, usize) {
let distances = calc_distances(points, min_x, min_y, max_x, max_y);
let mut areas: HashMap<u8, usize> = HashMap::new();
for i in min_x..=max_x {
@ -28,7 +34,10 @@ fn calc_max_area(points: &Vec<Point>, min_x: usize, min_y: usize, max_x: usize,
if distances[i][j] == 0 {
continue;
}
areas.entry(distances[i][j]).and_modify(|e| *e += 1).or_insert(1);
areas
.entry(distances[i][j])
.and_modify(|e| *e += 1)
.or_insert(1);
}
}
let mut index = 0;
@ -43,19 +52,27 @@ fn calc_max_area(points: &Vec<Point>, min_x: usize, min_y: usize, max_x: usize,
}
}
for (key, value) in areas.iter() {
println!("{}: {} area {}", key, points.get((index - 1) as usize).unwrap(), value);
println!(
"{}: {} area {}",
key,
points.get((index - 1) as usize).unwrap(),
value
);
}
(max_area_index, max_area)
}
fn calc_distances(points: &Vec<Point>, min_x: usize, min_y: usize, max_x: usize, max_y: usize) -> [[u8; 500]; 500] {
fn calc_distances(
points: &Vec<Point>,
min_x: usize,
min_y: usize,
max_x: usize,
max_y: usize,
) -> [[u8; 500]; 500] {
let mut distances = [[0u8; 500]; 500];
for i in min_x..=max_x {
for j in min_y..=max_y {
let cur_point = Point {
x: i,
y: j
};
let cur_point = Point { x: i, y: j };
let mut min_dis = 500;
let mut found_dupe = false;
let mut min_index = 0;
@ -114,17 +131,30 @@ fn main() {
let mut max_x = 0;
let mut max_y = 0;
for point in points.iter() {
if point.x < min_x { min_x = point.x };
if point.x > max_x { max_x = point.x };
if point.y < min_y { min_y = point.y };
if point.y > max_y { max_y = point.y };
if point.x < min_x {
min_x = point.x
};
if point.x > max_x {
max_x = point.x
};
if point.y < min_y {
min_y = point.y
};
if point.y > max_y {
max_y = point.y
};
}
let width = max_x - min_x;
let height = max_y - min_y;
println!("map size {}x{}", width, height);
let (max_area_point, max_area) = calc_max_area(&points, min_x, min_y, max_x, max_y);
let max_area_point_index = (max_area_point - 1) as usize;
println!("max area point {} {} is {}", max_area_point, points.get(max_area_point_index).unwrap(), max_area);
println!(
"max area point {} {} is {}",
max_area_point,
points.get(max_area_point_index).unwrap(),
max_area
);
let safe_zone = calc_safe_zone(&points, 10000);
println!("safe zone area {}", safe_zone);
}

View File

@ -1,7 +1,7 @@
use std::collections::{BTreeMap, HashMap};
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::iter::Iterator;
use std::collections::{BTreeMap, HashMap};
type Day7Map = BTreeMap<char, Vec<char>>;
@ -23,7 +23,7 @@ fn calc_order(steps: &mut Day7Map) -> String {
first_available = *key;
break;
}
};
}
let mut final_result = String::from("");
if first_available == '_' {
@ -93,9 +93,12 @@ fn main() {
let mut chars = line_str.chars();
let prereq = chars.nth(5).unwrap();
let target = chars.nth(30).unwrap();
steps.entry(target).and_modify(|e| e.push(prereq)).or_insert(vec![prereq]);
steps
.entry(target)
.and_modify(|e| e.push(prereq))
.or_insert(vec![prereq]);
steps.entry(prereq).or_insert(vec![]);
};
}
print_map(&steps);
let order = calc_order(&mut steps.clone());
println!("final order: {}", order);

View File

@ -45,10 +45,16 @@ fn calc_node_sum(ints: &mut Vec<i32>) -> i32 {
fn main() {
let mut line = String::new();
BufReader::new(File::open("input").unwrap()).read_line(&mut line).unwrap();
BufReader::new(File::open("input").unwrap())
.read_line(&mut line)
.unwrap();
let len_withoutcrlf = line.trim_right().len();
line.truncate(len_withoutcrlf);
let ints: Vec<i32> = line.as_str().split(" ").map(|i| i.parse::<i32>().unwrap()).collect();
let ints: Vec<i32> = line
.as_str()
.split(" ")
.map(|i| i.parse::<i32>().unwrap())
.collect();
let sum = calc_metadata_sum(&mut ints.clone());
println!("part 1 sum is {}", sum);
let sum2 = calc_node_sum(&mut ints.clone());