more cleanup

- fix all clippy warnings
- add times(n: usize) iterator

Signed-off-by: Stephen Waits <swaits@amazon.com>
This commit is contained in:
Stephen Waits committed 2020-12-12 07:10:10 -08:00
1 parent 16670f26a5
commit 8fb67d653c
6 files changed
+38 -48

No files matched your search

+2 -8
View File
@@ -132,13 +132,7 @@ impl Console {
.filter(|i| match self.program[*i as usize] {
// filter instructions we can modify
Instruction::Jmp(_) => true,
Instruction::Nop(n) => {
if n != 0 {
true
} else {
false
}
}
Instruction::Nop(n) => n != 0,
_ => false,
})
.collect();
@@ -146,7 +140,7 @@ impl Console {
// now test each of these candidates by exchanging jmp <=> nop
for i in candidate_indices {
// save original instruction
let original_instruction = self.program[i].clone();
let original_instruction = self.program[i];
// exchange jmp<=>nop
self.program[i] = match &self.program[i] {
+3 -3
View File
@@ -3,7 +3,7 @@
use crate::utils;
use anyhow::{anyhow, Result};
fn is_bad(data: &Vec<i64>, n: usize, i: usize) -> Result<i64> {
fn is_bad(data: &[i64], n: usize, i: usize) -> Result<i64> {
let prev_n = &data[i - n..i];
if !utils::combinations(2, prev_n.len()).any(|c| data[i] == prev_n[c[0]] + prev_n[c[1]]) {
Ok(data[i])
@@ -12,7 +12,7 @@ fn is_bad(data: &Vec<i64>, n: usize, i: usize) -> Result<i64> {
}
}
fn find_first_bad(data: &Vec<i64>, n: usize, start: usize) -> Result<i64> {
fn find_first_bad(data: &[i64], n: usize, start: usize) -> Result<i64> {
for i in start..data.len() {
let ret = is_bad(data, n, i);
if ret.is_ok() {
@@ -22,7 +22,7 @@ fn find_first_bad(data: &Vec<i64>, n: usize, start: usize) -> Result<i64> {
Err(anyhow!("no result found"))
}
fn find_contiguous_set(data: &Vec<i64>, x: i64) -> Result<i64> {
fn find_contiguous_set(data: &[i64], x: i64) -> Result<i64> {
for set_size in 2..data.len() - 1 {
for i in 0..data.len() - set_size {
if data[i..i + set_size].iter().sum::<i64>() == x {
+3 -3
View File
@@ -3,7 +3,7 @@
use crate::utils;
use anyhow::Result;
pub fn count_diff1_diff3(diffs: &Vec<i64>) -> usize {
pub fn count_diff1_diff3(diffs: &[i64]) -> usize {
let diff_1 = diffs.iter().filter(|n| **n == 1).count();
let diff_3 = diffs.iter().filter(|n| **n == 3).count();
diff_1 * diff_3
@@ -21,7 +21,7 @@ fn paths_by_runlength(runlength: usize) -> usize {
p[runlength % L]
}
fn count_paths(diffs: &Vec<i64>) -> usize {
fn count_paths(diffs: &[i64]) -> usize {
// count the runs of 1s
let mut last_diff: i64 = 0;
let mut runlength: usize = 1;
@@ -59,7 +59,7 @@ pub fn run() -> Result<(usize, usize)> {
.iter()
.enumerate()
.take(data.len() - 1)
.map(|(i, _)| (&data[i] - &data[i + 1]).abs())
.map(|(i, _)| (data[i] - data[i + 1]).abs())
.collect();
let p1 = count_diff1_diff3(&diffs);
+12 -18
View File
@@ -12,7 +12,7 @@ enum Location {
// compute the 8 immediately adjacent coordinates which contian chairs
fn adjacent_coords(
map: &Vec<Vec<Location>>,
map: &[Vec<Location>],
pos: (usize, usize),
max: (usize, usize),
) -> Vec<(usize, usize)> {
@@ -34,7 +34,7 @@ fn adjacent_coords(
}
// precompute all the immediately adjacent chairs for all positions on a map
fn compute_basic_adjacency(map: &Vec<Vec<Location>>) -> Vec<Vec<Vec<(usize, usize)>>> {
fn compute_basic_adjacency(map: &[Vec<Location>]) -> Vec<Vec<Vec<(usize, usize)>>> {
(0..map.len())
.map(|row| {
(0..map[row].len())
@@ -47,7 +47,7 @@ fn compute_basic_adjacency(map: &Vec<Vec<Location>>) -> Vec<Vec<Vec<(usize, usiz
// search in one direction from a position until finding a chair (used to compute
// advanced adjacency
fn search_direction_for_chair(
map: &Vec<Vec<Location>>,
map: &[Vec<Location>],
start: (usize, usize),
step: &(i32, i32),
) -> Option<(usize, usize)> {
@@ -65,7 +65,7 @@ fn search_direction_for_chair(
}
// precompute all the directionally adjacent chairs for all positions on a map
fn compute_advanced_adjacency(map: &Vec<Vec<Location>>) -> Vec<Vec<Vec<(usize, usize)>>> {
fn compute_advanced_adjacency(map: &[Vec<Location>]) -> Vec<Vec<Vec<(usize, usize)>>> {
// figure out our eight direction step values
let dirs: Vec<(i32, i32)> = (-1..2)
.flat_map(|i| (-1..2).map(move |j| (i, j)))
@@ -79,7 +79,7 @@ fn compute_advanced_adjacency(map: &Vec<Vec<Location>>) -> Vec<Vec<Vec<(usize, u
.map(|j| {
dirs.iter()
.filter_map(move |d| {
let dir = d.clone();
let dir = *d;
search_direction_for_chair(map, (i, j), &dir)
})
.collect()
@@ -92,7 +92,7 @@ fn compute_advanced_adjacency(map: &Vec<Vec<Location>>) -> Vec<Vec<Vec<(usize, u
// run the seating algorithm, return the total number of changes
fn seating_round(
map: &mut Vec<Vec<Location>>,
adj: &Vec<Vec<Vec<(usize, usize)>>>,
adj: &[Vec<Vec<(usize, usize)>>],
occupied_threshold: usize,
) -> usize {
let mut changes: Vec<(usize, usize, Location)> = Vec::new();
@@ -123,21 +123,15 @@ fn seating_round(
}
}
changes.iter().for_each(|c| map[c.0][c.1] = c.2.clone());
changes.iter().for_each(|c| map[c.0][c.1] = c.2);
changes.len()
}
// coutnt occupised seats on a given map
fn count_occupied(map: &Vec<Vec<Location>>) -> usize {
let mut count = 0;
for i in 0..map.len() {
for j in 0..map[i].len() {
if map[i][j] == Location::Occupied {
count += 1
}
}
}
count
fn count_occupied(map: &[Vec<Location>]) -> usize {
map.iter()
.flat_map(|row| row.iter().filter(|cell| **cell == Location::Occupied))
.count()
}
// main
@@ -167,7 +161,7 @@ pub fn run() -> Result<(usize, usize)> {
drop(map1);
// run seating alogirthm using advanced adjacency and occupied threshold of 5
let mut map2 = starting_map.clone();
let mut map2 = starting_map;
let advanced_adjacency = compute_advanced_adjacency(&map2);
while seating_round(&mut map2, &advanced_adjacency, 5) > 0 {}
let p2 = count_occupied(&map2);
+12 -15
View File
@@ -16,8 +16,6 @@ fn navigate(data: &str) -> usize {
"S" => pos_n -= dist,
"E" => pos_e += dist,
"W" => pos_e -= dist,
"L" => heading = (heading + 360 - dist) % 360,
"R" => heading = (heading + dist) % 360,
"F" => match heading {
0 => pos_n += dist,
90 => pos_e += dist,
@@ -26,6 +24,8 @@ fn navigate(data: &str) -> usize {
360 => pos_n += dist,
_ => panic!("non cardinal heading!"),
},
"L" => heading = (heading + 360 - dist) % 360,
"R" => heading = (heading + dist) % 360,
_ => panic!("bad direction!"),
}
});
@@ -40,12 +40,15 @@ fn navigate2(data: &str) -> usize {
let dir = &l[0..1];
let dist = &l[1..].parse::<i64>().expect("int parse error");
match dir {
"N" => wpt_n += dist,
"S" => wpt_n -= dist,
"E" => wpt_e += dist,
"W" => wpt_e -= dist,
"L" | "R" => match (dir, dist) {
match (dir, dist) {
("N", _) => wpt_n += dist,
("S", _) => wpt_n -= dist,
("E", _) => wpt_e += dist,
("W", _) => wpt_e -= dist,
("F", _) => {
ship_n += dist * wpt_n;
ship_e += dist * wpt_e;
}
("L", 90) | ("R", 270) => {
wpt_n = -wpt_n;
mem::swap(&mut wpt_n, &mut wpt_e);
@@ -54,17 +57,11 @@ fn navigate2(data: &str) -> usize {
wpt_e = -wpt_e;
mem::swap(&mut wpt_n, &mut wpt_e);
}
(_, 180) => {
("L", 180) | ("R", 180) => {
wpt_n = -wpt_n;
wpt_e = -wpt_e;
}
_ => panic!("bad direction!"),
},
"F" => {
ship_n += dist * wpt_n;
ship_e += dist * wpt_e;
}
_ => panic!("bad direction!"),
}
});
+6 -1
View File
@@ -1,7 +1,7 @@
// ==== utilities
use anyhow::Result;
use std::fs;
use std::{fs, iter};
pub fn read_i64s(filename: &str) -> Result<Vec<i64>> {
Ok(read_file(filename)?
@@ -23,6 +23,11 @@ pub fn parse_usize_in_range(s: &str, min: usize, max: usize) -> Option<usize> {
}
}
#[allow(dead_code)]
pub fn times(n: usize) -> impl Iterator<Item = usize> {
iter::repeat(()).take(n).enumerate().map(|(i, _)| i)
}
// Lexicographic combinations.
//
// This algorithm generates all t-combinations of n numbers {0, 1, ..., n-1},