Skip to content

Instantly share code, notes, and snippets.

@Deleplace
Created December 2, 2024 09:44
Show Gist options
  • Save Deleplace/aff758d37a4212fbcc983eb01c01354e to your computer and use it in GitHub Desktop.
Save Deleplace/aff758d37a4212fbcc983eb01c01354e to your computer and use it in GitHub Desktop.
use std::io::prelude::*;
use std::str::FromStr;
fn main() {
// solve_a();
solve_b();
}
fn solve_a() {
let lines = std::io::stdin().lock().lines().map(|x| x.unwrap()).collect::<Vec<String>>();
let mut safe = 0;
'linesloop: for report in lines {
let levels = report.split_whitespace()
.map(i32::from_str)
.collect::<Result<Vec<i32>, _>>()
.unwrap();
if levels.len() == 0 {
continue;
}
if levels.len() == 1 {
safe += 1;
continue;
}
let ASC = levels[1] > levels[0];
for i in 1..levels.len() {
let diff = (levels[i] - levels[i-1]).abs();
if diff<1 || diff>3 {
continue 'linesloop;
}
if (levels[i] > levels[i-1]) != ASC {
continue 'linesloop;
}
}
safe += 1;
}
println!("{}", safe);
}
fn is_safe(levels: Vec<i32>) -> bool {
if levels.len() == 0 {
return false;
}
if levels.len() == 1 {
return true;
}
let ASC = levels[1] > levels[0];
for i in 1..levels.len() {
let diff = (levels[i] - levels[i-1]).abs();
if diff<1 || diff>3 {
return false;
}
if (levels[i] > levels[i-1]) != ASC {
return false;
}
}
return true;
}
fn solve_b() {
let lines = std::io::stdin().lock().lines().map(|x| x.unwrap()).collect::<Vec<String>>();
let mut safe = 0;
for report in lines {
let levels = report.split_whitespace()
.map(i32::from_str)
.collect::<Result<Vec<i32>, _>>()
.unwrap();
if levels.len() == 0 {
continue;
}
if levels.len() == 1 {
safe += 1;
continue;
}
if is_safe(levels) {
safe += 1;
continue;
}
let n = levels.len();
// Pretend remove i-th
for i in 0..n {
let mut clo = levels.clone();
clo.remove(i);
if is_safe(clo) {
safe += 1;
break
}
}
}
println!("{}", safe);
}
@Deleplace
Copy link
Author

error[E0382]: borrow of moved value: `levels`
  --> src/main.rs:88:17
   |
70 |         let levels = report.split_whitespace()
   |             ------ move occurs because `levels` has type `Vec<i32>`, which does not implement the `Copy` trait
...
83 |         if is_safe(levels) {
   |                    ------ value moved here
...
88 |         let n = levels.len();
   |                 ^^^^^^^^^^^^ value borrowed here after move

For more information about this error, try `rustc --explain E0382`.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment