1
0
Fork 0

cargo fmt

main
Andrew Coleman 2018-12-20 13:09:06 -05:00
parent c164070d19
commit 28579c7487
5 changed files with 63 additions and 26 deletions

View File

@ -11,7 +11,7 @@ struct Point {
x: i32,
y: i32,
x_velocity: i32,
y_velocity: i32
y_velocity: i32,
}
fn bounding_box(points: &Vec<Point>) -> (i32, i32, i32, i32) {
@ -21,10 +21,18 @@ fn bounding_box(points: &Vec<Point>) -> (i32, i32, i32, i32) {
let mut max_y = std::i32::MIN;
for p in points.iter() {
if min_x > p.x { min_x = p.x };
if min_y > p.y { min_y = p.y };
if max_x < p.x { max_x = p.x };
if max_y < p.y { max_y = p.y };
if min_x > p.x {
min_x = p.x
};
if min_y > p.y {
min_y = p.y
};
if max_x < p.x {
max_x = p.x
};
if max_y < p.y {
max_y = p.y
};
}
(min_x, min_y, max_x, max_y)
@ -55,7 +63,10 @@ fn tick(points: &mut Vec<Point>) -> i32 {
}
let (min_x, min_y, max_x, max_y) = bounding_box(points);
println!("final bounding box: ({}, {}) ({}, {})", min_x, min_y, max_x, max_y);
println!(
"final bounding box: ({}, {}) ({}, {})",
min_x, min_y, max_x, max_y
);
let mut final_set = HashSet::with_capacity(points.len());
for p in points.iter() {
final_set.insert((p.x, p.y));
@ -81,7 +92,8 @@ fn main() {
.map(|line| line.unwrap())
.collect();
let mut points: Vec<Point> = Vec::with_capacity(lines.len());
let re = Regex::new(r"^position=<([-| ]\d+), ([-| ]\d+)> velocity=<([-| ]\d), ([-| ]\d)>$").unwrap();
let re =
Regex::new(r"^position=<([-| ]\d+), ([-| ]\d+)> velocity=<([-| ]\d), ([-| ]\d)>$").unwrap();
for line in lines.iter() {
if re.is_match(line) {
let cap = re.captures(line).unwrap();
@ -93,7 +105,7 @@ fn main() {
x,
y,
x_velocity,
y_velocity
y_velocity,
});
}
}

View File

@ -57,7 +57,9 @@ fn find_variable_max_sum(map: &[[i32; 300]; 300]) -> (usize, usize, i32, usize)
let mut sum = 0;
for i in 1..=300 {
let (new_x, new_y, new_sum) = find_max_sum(map, i);
if new_sum == 0 { break; }
if new_sum == 0 {
break;
}
if new_sum > sum {
size = i;
sum = new_sum;
@ -78,8 +80,14 @@ fn main() {
let map = calc_map_from_serial_number(serial_number);
let (x, y, sum) = find_max_sum(&map, 3);
println!("maximum sum coordinate of ({}, {}) with sum of {}", x, y, sum);
println!(
"maximum sum coordinate of ({}, {}) with sum of {}",
x, y, sum
);
let (x, y, sum, size) = find_variable_max_sum(&map);
println!("maximum variable sum found at ({}, {}) sum {} with size of {}", x, y, sum, size);
println!(
"maximum variable sum found at ({}, {}) sum {} with size of {}",
x, y, sum, size
);
}

View File

@ -16,7 +16,11 @@ fn get_score(state: &[char; MAP_SIZE]) -> i64 {
score
}
fn run_game(initial_state: String, states: HashMap<String, char>, generations: i64) -> (i64, [i64; MAP_SIZE]) {
fn run_game(
initial_state: String,
states: HashMap<String, char>,
generations: i64,
) -> (i64, [i64; MAP_SIZE]) {
let mut last_state = ['.'; MAP_SIZE];
for (i, c) in initial_state.char_indices() {
if c == '#' {
@ -30,7 +34,7 @@ fn run_game(initial_state: String, states: HashMap<String, char>, generations: i
let mut next_state = ['.'; MAP_SIZE];
for i in 2..(MAP_SIZE - 2) {
let mut key = String::new();
for j in i-2..=i+2 {
for j in i - 2..=i + 2 {
key.push(last_state[j]);
}
if let Some(dest_state) = states.get(&key) {
@ -53,7 +57,14 @@ fn find_big_score(initial_state: String, states: HashMap<String, char>, iteratio
if diff1 == diff2 {
if diff2 == diff3 {
let sum = ((iterations - i as i64 - 1) * diff1) + scores[i];
println!("({} - {}) * {} + {} = {}", iterations - 1, i as i64, diff1, scores[i], sum);
println!(
"({} - {}) * {} + {} = {}",
iterations - 1,
i as i64,
diff1,
scores[i],
sum
);
return sum;
}
}

View File

@ -10,7 +10,7 @@ struct Cart {
x_delta: i8,
y_delta: i8,
intersection: u8,
removed: bool
removed: bool,
}
impl Cart {
@ -60,7 +60,11 @@ impl Cart {
}
}
fn find_collision(map: &[[char; MAP_SIZE]; MAP_SIZE], carts: &mut Vec<Cart>, find_last: bool) -> (usize, usize, u32) {
fn find_collision(
map: &[[char; MAP_SIZE]; MAP_SIZE],
carts: &mut Vec<Cart>,
find_last: bool,
) -> (usize, usize, u32) {
let mut ticks = 0;
let cart_len = carts.len();
loop {
@ -97,20 +101,20 @@ fn find_collision(map: &[[char; MAP_SIZE]; MAP_SIZE], carts: &mut Vec<Cart>, fin
} else {
carts[i].turn_left();
}
},
}
'\\' => {
if carts[i].x_delta == 0 {
carts[i].turn_left();
} else {
carts[i].turn_right();
}
},
}
'+' => {
carts[i].change_direction_at_intersection();
},
}
' ' => {
panic!("off the rails!");
},
}
_ => {}
}
carts[i].x = next_x;
@ -151,7 +155,7 @@ fn main() {
x_delta: 0,
y_delta: 1,
intersection: 0,
removed: false
removed: false,
});
map[j][line_number] = '|';
} else if c == '^' {
@ -161,7 +165,7 @@ fn main() {
x_delta: 0,
y_delta: -1,
intersection: 0,
removed: false
removed: false,
});
map[j][line_number] = '|';
} else if c == '<' {
@ -171,7 +175,7 @@ fn main() {
x_delta: -1,
y_delta: 0,
intersection: 0,
removed: false
removed: false,
});
map[j][line_number] = '-';
} else if c == '>' {
@ -181,7 +185,7 @@ fn main() {
x_delta: 1,
y_delta: 0,
intersection: 0,
removed: false
removed: false,
});
map[j][line_number] = '-';
} else {
@ -193,5 +197,8 @@ fn main() {
let (x, y, ticks) = find_collision(&map, &mut carts.clone(), false);
println!("found collision at {},{} after {} ticks", x, y, ticks);
let (x, y, ticks) = find_collision(&map, &mut carts.clone(), true);
println!("found last remaining cart at {},{} after {} ticks", x, y, ticks);
println!(
"found last remaining cart at {},{} after {} ticks",
x, y, ticks
);
}

View File

@ -30,7 +30,6 @@ fn mutate_chocolate(scores: &mut Vec<u8>, recipes: i32) -> (String, usize) {
for s in scores.iter() {
score_str.push((s + 48) as char);
}
//println!("{}", score_str);
let recipe_str = format!("{}", recipes);
let recipe_index = score_str.find(recipe_str.as_str()).unwrap();