Last active
August 25, 2019 07:54
-
-
Save ksoda/2fd309bc0ec123d32aa22dcd1ad2ed69 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
(def alt-from-char {\U 1 \D -1}) | |
(defn ending-valley? [step alt] | |
(and (= alt -1) (= step \U))) | |
(defn countingValleys [n s] | |
(:cnt | |
(reduce | |
(fn [acc step] | |
(let [{:keys [cnt alt]} acc] | |
{:cnt (+ cnt (if (ending-valley? step alt) 1 0)) | |
:alt (+ alt (alt-from-char step))})) | |
{:cnt 0 :alt 0} (seq s)))) | |
(spit | |
(get (System/getenv) "OUTPUT_PATH") | |
(str (countingValleys | |
(Integer/parseInt (clojure.string/trim (read-line))) | |
(read-line)) | |
"\n") | |
:append true) |
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::{cmp, env, fs}; | |
#[allow(unused)] | |
macro_rules! get { | |
($t:ty) => { | |
{ | |
let mut line: String = String::new(); | |
std::io::stdin().read_line(&mut line).unwrap(); | |
line.trim().parse::<$t>().unwrap() | |
} | |
}; | |
($($t:ty),*) => { | |
{ | |
let mut line: String = String::new(); | |
std::io::stdin().read_line(&mut line).unwrap(); | |
let mut iter = line.split_whitespace(); | |
( | |
$(iter.next().unwrap().parse::<$t>().unwrap(),)* | |
) | |
} | |
}; | |
($t:ty; $n:expr) => { | |
(0..$n).map(|_| | |
get!($t) | |
).collect::<Vec<_>>() | |
}; | |
($($t:ty),*; $n:expr) => { | |
(0..$n).map(|_| | |
get!($($t),*) | |
).collect::<Vec<_>>() | |
}; | |
($t:ty ;;) => { | |
{ | |
let mut line: String = String::new(); | |
std::io::stdin().read_line(&mut line).unwrap(); | |
line.split_whitespace() | |
.map(|t| t.parse::<$t>().unwrap()) | |
.collect::<Vec<_>>() | |
} | |
}; | |
($t:ty ;; $n:expr) => { | |
(0..$n).map(|_| get!($t ;;)).collect::<Vec<_>>() | |
}; | |
} | |
fn main() -> std::io::Result<()> { | |
let _n = get!(u32; 1); | |
let c = get!(u32 ;;); | |
// println!("{}", step(c)); | |
let p = env::var("OUTPUT_PATH").unwrap(); | |
fs::write(p, step(c).to_string())?; | |
Ok(()) | |
} | |
#[allow(unused)] | |
fn step(x: Vec<u32>) -> u32 { | |
if (x.last() == Some(&1)) { | |
return std::u32::MAX; | |
} | |
let mut y = x.clone(); | |
match (y.len()) { | |
1 => 0, | |
2 => { | |
y.pop(); | |
1 + step(y) | |
} | |
_ => { | |
y.pop(); | |
let mut z = y.clone(); | |
z.pop(); | |
1 + cmp::min(step(y), step(z)) | |
} | |
} | |
} | |
#[cfg(test)] | |
mod tests { | |
use super::*; | |
#[test] | |
fn it_works() { | |
assert_eq!(step(vec![0]), 0); | |
assert_eq!(step(vec![0, 0]), 1); | |
assert_eq!(step(vec![0, 0, 0]), 1); | |
assert_eq!(step(vec![0, 0, 0, 0]), 2); | |
assert_eq!(step(vec![0, 1, 0]), 1); | |
assert_eq!(step(vec![0, 1, 0, 1, 0]), 2); | |
assert_eq!(step(vec![0, 0, 1, 0, 0]), 3); | |
} | |
} |
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::{env, fs}; | |
#[allow(unused)] | |
macro_rules! get { | |
($t:ty) => {{ | |
let mut line: String = String::new(); | |
std::io::stdin().read_line(&mut line).unwrap(); | |
line.trim().parse::<$t>().unwrap() | |
}}; | |
} | |
fn main() -> std::io::Result<()> { | |
let line = get!(String); | |
let n = get!(usize); | |
let p = env::var("OUTPUT_PATH").unwrap(); | |
fs::write(p, solve(&line, n).to_string())?; | |
Ok(()) | |
} | |
fn solve(s: &str, n: usize) -> usize { | |
let c = s.matches("a").count(); | |
let l = s.len(); | |
c * (n / l) + s[..(n % l)].matches("a").count() | |
} | |
#[cfg(test)] | |
mod tests { | |
use super::*; | |
#[test] | |
fn it_works() { | |
assert_eq!(solve("aba", 10), 7); | |
assert_eq!(solve("a", 1000000000000), 1000000000000); | |
} | |
} |
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
; Complete the sockMerchant function below. | |
(defn sockMerchant [n ar] | |
(reduce | |
+ (map #(quot (count %1) 2) | |
(partition-by identity (sort ar))))) | |
(def fptr (get (System/getenv) "OUTPUT_PATH")) | |
(def n (Integer/parseInt (clojure.string/trim (read-line)))) | |
(def ar (vec (map #(Integer/parseInt %) (clojure.string/split (read-line) #" ")))) | |
(def result (sockMerchant n ar)) | |
(spit fptr (str result "\n") :append true) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment