more cleanup
- fix all clippy warnings - add times(n: usize) iterator Signed-off-by: Stephen Waits <swaits@amazon.com>
This commit is contained in:
6 files changed
+46
-56
No files matched your search
+2
-8
@@ -132,13 +132,7 @@ impl Console {
|
|||||||
.filter(|i| match self.program[*i as usize] {
|
.filter(|i| match self.program[*i as usize] {
|
||||||
// filter instructions we can modify
|
// filter instructions we can modify
|
||||||
Instruction::Jmp(_) => true,
|
Instruction::Jmp(_) => true,
|
||||||
Instruction::Nop(n) => {
|
Instruction::Nop(n) => n != 0,
|
||||||
if n != 0 {
|
|
||||||
true
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => false,
|
_ => false,
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
@@ -146,7 +140,7 @@ impl Console {
|
|||||||
// now test each of these candidates by exchanging jmp <=> nop
|
// now test each of these candidates by exchanging jmp <=> nop
|
||||||
for i in candidate_indices {
|
for i in candidate_indices {
|
||||||
// save original instruction
|
// save original instruction
|
||||||
let original_instruction = self.program[i].clone();
|
let original_instruction = self.program[i];
|
||||||
|
|
||||||
// exchange jmp<=>nop
|
// exchange jmp<=>nop
|
||||||
self.program[i] = match &self.program[i] {
|
self.program[i] = match &self.program[i] {
|
||||||
|
|||||||
+3
-3
@@ -3,7 +3,7 @@
|
|||||||
use crate::utils;
|
use crate::utils;
|
||||||
use anyhow::{anyhow, Result};
|
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];
|
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]]) {
|
if !utils::combinations(2, prev_n.len()).any(|c| data[i] == prev_n[c[0]] + prev_n[c[1]]) {
|
||||||
Ok(data[i])
|
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() {
|
for i in start..data.len() {
|
||||||
let ret = is_bad(data, n, i);
|
let ret = is_bad(data, n, i);
|
||||||
if ret.is_ok() {
|
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"))
|
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 set_size in 2..data.len() - 1 {
|
||||||
for i in 0..data.len() - set_size {
|
for i in 0..data.len() - set_size {
|
||||||
if data[i..i + set_size].iter().sum::<i64>() == x {
|
if data[i..i + set_size].iter().sum::<i64>() == x {
|
||||||
|
|||||||
+3
-3
@@ -3,7 +3,7 @@
|
|||||||
use crate::utils;
|
use crate::utils;
|
||||||
use anyhow::Result;
|
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_1 = diffs.iter().filter(|n| **n == 1).count();
|
||||||
let diff_3 = diffs.iter().filter(|n| **n == 3).count();
|
let diff_3 = diffs.iter().filter(|n| **n == 3).count();
|
||||||
diff_1 * diff_3
|
diff_1 * diff_3
|
||||||
@@ -21,7 +21,7 @@ fn paths_by_runlength(runlength: usize) -> usize {
|
|||||||
p[runlength % L]
|
p[runlength % L]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn count_paths(diffs: &Vec<i64>) -> usize {
|
fn count_paths(diffs: &[i64]) -> usize {
|
||||||
// count the runs of 1s
|
// count the runs of 1s
|
||||||
let mut last_diff: i64 = 0;
|
let mut last_diff: i64 = 0;
|
||||||
let mut runlength: usize = 1;
|
let mut runlength: usize = 1;
|
||||||
@@ -59,7 +59,7 @@ pub fn run() -> Result<(usize, usize)> {
|
|||||||
.iter()
|
.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.take(data.len() - 1)
|
.take(data.len() - 1)
|
||||||
.map(|(i, _)| (&data[i] - &data[i + 1]).abs())
|
.map(|(i, _)| (data[i] - data[i + 1]).abs())
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let p1 = count_diff1_diff3(&diffs);
|
let p1 = count_diff1_diff3(&diffs);
|
||||||
|
|||||||
+12
-18
@@ -12,7 +12,7 @@ enum Location {
|
|||||||
|
|
||||||
// compute the 8 immediately adjacent coordinates which contian chairs
|
// compute the 8 immediately adjacent coordinates which contian chairs
|
||||||
fn adjacent_coords(
|
fn adjacent_coords(
|
||||||
map: &Vec<Vec<Location>>,
|
map: &[Vec<Location>],
|
||||||
pos: (usize, usize),
|
pos: (usize, usize),
|
||||||
max: (usize, usize),
|
max: (usize, usize),
|
||||||
) -> Vec<(usize, usize)> {
|
) -> Vec<(usize, usize)> {
|
||||||
@@ -34,7 +34,7 @@ fn adjacent_coords(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// precompute all the immediately adjacent chairs for all positions on a map
|
// 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())
|
(0..map.len())
|
||||||
.map(|row| {
|
.map(|row| {
|
||||||
(0..map[row].len())
|
(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
|
// search in one direction from a position until finding a chair (used to compute
|
||||||
// advanced adjacency
|
// advanced adjacency
|
||||||
fn search_direction_for_chair(
|
fn search_direction_for_chair(
|
||||||
map: &Vec<Vec<Location>>,
|
map: &[Vec<Location>],
|
||||||
start: (usize, usize),
|
start: (usize, usize),
|
||||||
step: &(i32, i32),
|
step: &(i32, i32),
|
||||||
) -> Option<(usize, usize)> {
|
) -> Option<(usize, usize)> {
|
||||||
@@ -65,7 +65,7 @@ fn search_direction_for_chair(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// precompute all the directionally adjacent chairs for all positions on a map
|
// 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
|
// figure out our eight direction step values
|
||||||
let dirs: Vec<(i32, i32)> = (-1..2)
|
let dirs: Vec<(i32, i32)> = (-1..2)
|
||||||
.flat_map(|i| (-1..2).map(move |j| (i, j)))
|
.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| {
|
.map(|j| {
|
||||||
dirs.iter()
|
dirs.iter()
|
||||||
.filter_map(move |d| {
|
.filter_map(move |d| {
|
||||||
let dir = d.clone();
|
let dir = *d;
|
||||||
search_direction_for_chair(map, (i, j), &dir)
|
search_direction_for_chair(map, (i, j), &dir)
|
||||||
})
|
})
|
||||||
.collect()
|
.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
|
// run the seating algorithm, return the total number of changes
|
||||||
fn seating_round(
|
fn seating_round(
|
||||||
map: &mut Vec<Vec<Location>>,
|
map: &mut Vec<Vec<Location>>,
|
||||||
adj: &Vec<Vec<Vec<(usize, usize)>>>,
|
adj: &[Vec<Vec<(usize, usize)>>],
|
||||||
occupied_threshold: usize,
|
occupied_threshold: usize,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
let mut changes: Vec<(usize, usize, Location)> = Vec::new();
|
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()
|
changes.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
// coutnt occupised seats on a given map
|
// coutnt occupised seats on a given map
|
||||||
fn count_occupied(map: &Vec<Vec<Location>>) -> usize {
|
fn count_occupied(map: &[Vec<Location>]) -> usize {
|
||||||
let mut count = 0;
|
map.iter()
|
||||||
for i in 0..map.len() {
|
.flat_map(|row| row.iter().filter(|cell| **cell == Location::Occupied))
|
||||||
for j in 0..map[i].len() {
|
.count()
|
||||||
if map[i][j] == Location::Occupied {
|
|
||||||
count += 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
count
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// main
|
// main
|
||||||
@@ -167,7 +161,7 @@ pub fn run() -> Result<(usize, usize)> {
|
|||||||
drop(map1);
|
drop(map1);
|
||||||
|
|
||||||
// run seating alogirthm using advanced adjacency and occupied threshold of 5
|
// 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);
|
let advanced_adjacency = compute_advanced_adjacency(&map2);
|
||||||
while seating_round(&mut map2, &advanced_adjacency, 5) > 0 {}
|
while seating_round(&mut map2, &advanced_adjacency, 5) > 0 {}
|
||||||
let p2 = count_occupied(&map2);
|
let p2 = count_occupied(&map2);
|
||||||
|
|||||||
+20
-23
@@ -16,8 +16,6 @@ fn navigate(data: &str) -> usize {
|
|||||||
"S" => pos_n -= dist,
|
"S" => pos_n -= dist,
|
||||||
"E" => pos_e += dist,
|
"E" => pos_e += dist,
|
||||||
"W" => pos_e -= dist,
|
"W" => pos_e -= dist,
|
||||||
"L" => heading = (heading + 360 - dist) % 360,
|
|
||||||
"R" => heading = (heading + dist) % 360,
|
|
||||||
"F" => match heading {
|
"F" => match heading {
|
||||||
0 => pos_n += dist,
|
0 => pos_n += dist,
|
||||||
90 => pos_e += dist,
|
90 => pos_e += dist,
|
||||||
@@ -26,6 +24,8 @@ fn navigate(data: &str) -> usize {
|
|||||||
360 => pos_n += dist,
|
360 => pos_n += dist,
|
||||||
_ => panic!("non cardinal heading!"),
|
_ => panic!("non cardinal heading!"),
|
||||||
},
|
},
|
||||||
|
"L" => heading = (heading + 360 - dist) % 360,
|
||||||
|
"R" => heading = (heading + dist) % 360,
|
||||||
_ => panic!("bad direction!"),
|
_ => panic!("bad direction!"),
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -40,30 +40,27 @@ fn navigate2(data: &str) -> usize {
|
|||||||
let dir = &l[0..1];
|
let dir = &l[0..1];
|
||||||
let dist = &l[1..].parse::<i64>().expect("int parse error");
|
let dist = &l[1..].parse::<i64>().expect("int parse error");
|
||||||
|
|
||||||
match dir {
|
match (dir, dist) {
|
||||||
"N" => wpt_n += dist,
|
("N", _) => wpt_n += dist,
|
||||||
"S" => wpt_n -= dist,
|
("S", _) => wpt_n -= dist,
|
||||||
"E" => wpt_e += dist,
|
("E", _) => wpt_e += dist,
|
||||||
"W" => wpt_e -= dist,
|
("W", _) => wpt_e -= dist,
|
||||||
"L" | "R" => match (dir, dist) {
|
("F", _) => {
|
||||||
("L", 90) | ("R", 270) => {
|
|
||||||
wpt_n = -wpt_n;
|
|
||||||
mem::swap(&mut wpt_n, &mut wpt_e);
|
|
||||||
}
|
|
||||||
("L", 270) | ("R", 90) => {
|
|
||||||
wpt_e = -wpt_e;
|
|
||||||
mem::swap(&mut wpt_n, &mut wpt_e);
|
|
||||||
}
|
|
||||||
(_, 180) => {
|
|
||||||
wpt_n = -wpt_n;
|
|
||||||
wpt_e = -wpt_e;
|
|
||||||
}
|
|
||||||
_ => panic!("bad direction!"),
|
|
||||||
},
|
|
||||||
"F" => {
|
|
||||||
ship_n += dist * wpt_n;
|
ship_n += dist * wpt_n;
|
||||||
ship_e += dist * wpt_e;
|
ship_e += dist * wpt_e;
|
||||||
}
|
}
|
||||||
|
("L", 90) | ("R", 270) => {
|
||||||
|
wpt_n = -wpt_n;
|
||||||
|
mem::swap(&mut wpt_n, &mut wpt_e);
|
||||||
|
}
|
||||||
|
("L", 270) | ("R", 90) => {
|
||||||
|
wpt_e = -wpt_e;
|
||||||
|
mem::swap(&mut wpt_n, &mut wpt_e);
|
||||||
|
}
|
||||||
|
("L", 180) | ("R", 180) => {
|
||||||
|
wpt_n = -wpt_n;
|
||||||
|
wpt_e = -wpt_e;
|
||||||
|
}
|
||||||
_ => panic!("bad direction!"),
|
_ => panic!("bad direction!"),
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
+6
-1
@@ -1,7 +1,7 @@
|
|||||||
// ==== utilities
|
// ==== utilities
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use std::fs;
|
use std::{fs, iter};
|
||||||
|
|
||||||
pub fn read_i64s(filename: &str) -> Result<Vec<i64>> {
|
pub fn read_i64s(filename: &str) -> Result<Vec<i64>> {
|
||||||
Ok(read_file(filename)?
|
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.
|
// Lexicographic combinations.
|
||||||
//
|
//
|
||||||
// This algorithm generates all t-combinations of n numbers {0, 1, ..., n-1},
|
// This algorithm generates all t-combinations of n numbers {0, 1, ..., n-1},
|
||||||
|
|||||||
Reference in new issue
Block a user