Created
December 2, 2024 09:44
-
-
Save Deleplace/aff758d37a4212fbcc983eb01c01354e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
Author
Deleplace
commented
Dec 2, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment