[Rust/2015/04] Add solution
This commit is contained in:
parent
b997aaa04a
commit
24eea1ba64
9 changed files with 126 additions and 3 deletions
|
@ -83,10 +83,10 @@
|
|||
|[**19**](https://adventofcode.com/2016/day/19) [<img height=12 src=".assets/py.svg">](Python/2016/19.py "Python solution for 2016/19")|[**20**](https://adventofcode.com/2016/day/20) [<img height=12 src=".assets/py.svg">](Python/2016/20.py "Python solution for 2016/20")|[**21**](https://adventofcode.com/2016/day/21) [<img height=12 src=".assets/py.svg">](Python/2016/21.py "Python solution for 2016/21")|[**22**](https://adventofcode.com/2016/day/22) [<img height=12 src=".assets/py.svg">](Python/2016/22.py "Python solution for 2016/22")|[**23**](https://adventofcode.com/2016/day/23) [<img height=12 src=".assets/py.svg">](Python/2016/23.py "Python solution for 2016/23")|[**24**](https://adventofcode.com/2016/day/24) [<img height=12 src=".assets/py.svg">](Python/2016/24.py "Python solution for 2016/24")|[**25**](https://adventofcode.com/2016/day/25) [<img height=12 src=".assets/py.svg">](Python/2016/25.py "Python solution for 2016/25")|
|
||||
|26|27|28|29|30|31||
|
||||
|
||||
## [2015](https://adventofcode.com/2015) ([<img height=18 src=".assets/py.svg"> Python](Python/2015): 25/25 | [<img height=18 src=".assets/rs.svg"> Rust](Rust/2015): 3/25 | [<img height=18 src=".assets/ua.png"> Uiua](Uiua/2015): 3/25 | [<img height=18 src=".assets/nix.svg"> Nix](Nix/2015): 1/25)
|
||||
## [2015](https://adventofcode.com/2015) ([<img height=18 src=".assets/py.svg"> Python](Python/2015): 25/25 | [<img height=18 src=".assets/rs.svg"> Rust](Rust/2015): 4/25 | [<img height=18 src=".assets/ua.png"> Uiua](Uiua/2015): 3/25 | [<img height=18 src=".assets/nix.svg"> Nix](Nix/2015): 1/25)
|
||||
|Mo|Tu|We|Th|Fr|Sa|Su|
|
||||
|-|-|-|-|-|-|-|
|
||||
||[**1**](https://adventofcode.com/2015/day/1) [<img height=12 src=".assets/rs.svg">](Rust/2015/01.rs "Rust solution for 2015/01") [<img height=12 src=".assets/py.svg">](Python/2015/01.py "Python solution for 2015/01") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgotwqwuPUAoCuKKgygrMeKKl8KvMVwrKS8rCg== "Uiua solution for 2015/01") [<img height=12 src=".assets/nix.svg">](Nix/2015/01.nix "Nix solution for 2015/01")|[**2**](https://adventofcode.com/2015/day/2) [<img height=12 src=".assets/rs.svg">](Rust/2015/02.rs "Rust solution for 2015/02") [<img height=12 src=".assets/py.svg">](Python/2015/02.py "Python solution for 2015/02") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgriipwo4oqc4ouV4omgQHguKeKJoEBcbi4KCuKIqSgvKyspIOKJoeKKgygvw5d8w5cyLeKKgy_ihqUvK3zDt-KKgy_ihqUvw5d8w5cyLyviiaEvw5fil6sy4oavWzRdKQo= "Uiua solution for 2015/02")|[**3**](https://adventofcode.com/2015/day/3) [<img height=12 src=".assets/rs.svg">](Rust/2015/03.rs "Rust solution for 2015/03") [<img height=12 src=".assets/py.svg">](Python/2015/03.py "Python solution for 2015/03") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgrilr3iiIosInZePD4iCuKItSjiioLiioMoLeKIqT1AXixAdikoLeKIqT1APCxAPikpCgriiKko4qe74oqdKSDiioMo4oqC4oipKFwr4oqCMF8wKSDiiKko4o2J4oqiKeKHjC7ijYnihq_iioI6Ml8yw7cy4qe7LikgKFwr4oqCMF8wKQo= "Uiua solution for 2015/03")|[**4**](https://adventofcode.com/2015/day/4) [<img height=12 src=".assets/py.svg">](Python/2015/04.py "Python solution for 2015/04")|[**5**](https://adventofcode.com/2015/day/5) [<img height=12 src=".assets/py.svg">](Python/2015/05.py "Python solution for 2015/05")|[**6**](https://adventofcode.com/2015/day/6) [<img height=12 src=".assets/py.svg">](Python/2015/06.py "Python solution for 2015/06")|
|
||||
||[**1**](https://adventofcode.com/2015/day/1) [<img height=12 src=".assets/rs.svg">](Rust/2015/01.rs "Rust solution for 2015/01") [<img height=12 src=".assets/py.svg">](Python/2015/01.py "Python solution for 2015/01") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgotwqwuPUAoCuKKgygrMeKKl8KvMVwrKS8rCg== "Uiua solution for 2015/01") [<img height=12 src=".assets/nix.svg">](Nix/2015/01.nix "Nix solution for 2015/01")|[**2**](https://adventofcode.com/2015/day/2) [<img height=12 src=".assets/rs.svg">](Rust/2015/02.rs "Rust solution for 2015/02") [<img height=12 src=".assets/py.svg">](Python/2015/02.py "Python solution for 2015/02") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgriipwo4oqc4ouV4omgQHguKeKJoEBcbi4KCuKIqSgvKyspIOKJoeKKgygvw5d8w5cyLeKKgy_ihqUvK3zDt-KKgy_ihqUvw5d8w5cyLyviiaEvw5fil6sy4oavWzRdKQo= "Uiua solution for 2015/02")|[**3**](https://adventofcode.com/2015/day/3) [<img height=12 src=".assets/rs.svg">](Rust/2015/03.rs "Rust solution for 2015/03") [<img height=12 src=".assets/py.svg">](Python/2015/03.py "Python solution for 2015/03") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgrilr3iiIosInZePD4iCuKItSjiioLiioMoLeKIqT1AXixAdikoLeKIqT1APCxAPikpCgriiKko4qe74oqdKSDiioMo4oqC4oipKFwr4oqCMF8wKSDiiKko4o2J4oqiKeKHjC7ijYnihq_iioI6Ml8yw7cy4qe7LikgKFwr4oqCMF8wKQo= "Uiua solution for 2015/03")|[**4**](https://adventofcode.com/2015/day/4) [<img height=12 src=".assets/rs.svg">](Rust/2015/04.rs "Rust solution for 2015/04") [<img height=12 src=".assets/py.svg">](Python/2015/04.py "Python solution for 2015/04")|[**5**](https://adventofcode.com/2015/day/5) [<img height=12 src=".assets/py.svg">](Python/2015/05.py "Python solution for 2015/05")|[**6**](https://adventofcode.com/2015/day/6) [<img height=12 src=".assets/py.svg">](Python/2015/06.py "Python solution for 2015/06")|
|
||||
|[**7**](https://adventofcode.com/2015/day/7) [<img height=12 src=".assets/py.svg">](Python/2015/07.py "Python solution for 2015/07")|[**8**](https://adventofcode.com/2015/day/8) [<img height=12 src=".assets/py.svg">](Python/2015/08.py "Python solution for 2015/08")|[**9**](https://adventofcode.com/2015/day/9) [<img height=12 src=".assets/py.svg">](Python/2015/09.py "Python solution for 2015/09")|[**10**](https://adventofcode.com/2015/day/10) [<img height=12 src=".assets/py.svg">](Python/2015/10.py "Python solution for 2015/10")|[**11**](https://adventofcode.com/2015/day/11) [<img height=12 src=".assets/py.svg">](Python/2015/11.py "Python solution for 2015/11")|[**12**](https://adventofcode.com/2015/day/12) [<img height=12 src=".assets/py.svg">](Python/2015/12.py "Python solution for 2015/12")|[**13**](https://adventofcode.com/2015/day/13) [<img height=12 src=".assets/py.svg">](Python/2015/13.py "Python solution for 2015/13")|
|
||||
|[**14**](https://adventofcode.com/2015/day/14) [<img height=12 src=".assets/py.svg">](Python/2015/14.py "Python solution for 2015/14")|[**15**](https://adventofcode.com/2015/day/15) [<img height=12 src=".assets/py.svg">](Python/2015/15.py "Python solution for 2015/15")|[**16**](https://adventofcode.com/2015/day/16) [<img height=12 src=".assets/py.svg">](Python/2015/16.py "Python solution for 2015/16")|[**17**](https://adventofcode.com/2015/day/17) [<img height=12 src=".assets/py.svg">](Python/2015/17.py "Python solution for 2015/17")|[**18**](https://adventofcode.com/2015/day/18) [<img height=12 src=".assets/py.svg">](Python/2015/18.py "Python solution for 2015/18")|[**19**](https://adventofcode.com/2015/day/19) [<img height=12 src=".assets/py.svg">](Python/2015/19.py "Python solution for 2015/19")|[**20**](https://adventofcode.com/2015/day/20) [<img height=12 src=".assets/py.svg">](Python/2015/20.py "Python solution for 2015/20")|
|
||||
|[**21**](https://adventofcode.com/2015/day/21) [<img height=12 src=".assets/py.svg">](Python/2015/21.py "Python solution for 2015/21")|[**22**](https://adventofcode.com/2015/day/22) [<img height=12 src=".assets/py.svg">](Python/2015/22.py "Python solution for 2015/22")|[**23**](https://adventofcode.com/2015/day/23) [<img height=12 src=".assets/py.svg">](Python/2015/23.py "Python solution for 2015/23")|[**24**](https://adventofcode.com/2015/day/24) [<img height=12 src=".assets/py.svg">](Python/2015/24.py "Python solution for 2015/24")|[**25**](https://adventofcode.com/2015/day/25) [<img height=12 src=".assets/py.svg">](Python/2015/25.py "Python solution for 2015/25")|26|27|
|
||||
|
|
59
Rust/2015/04.rs
Normal file
59
Rust/2015/04.rs
Normal file
|
@ -0,0 +1,59 @@
|
|||
#![feature(test)]
|
||||
|
||||
use std::{
|
||||
sync::{
|
||||
atomic::{self, AtomicUsize},
|
||||
Arc,
|
||||
},
|
||||
thread::{self, available_parallelism},
|
||||
};
|
||||
|
||||
use md5::{Digest, Md5};
|
||||
|
||||
type Input = String;
|
||||
|
||||
fn setup(input: &str) -> Input {
|
||||
input.trim().into()
|
||||
}
|
||||
|
||||
fn solve(input: &Input, n: usize) -> usize {
|
||||
let parallelism = usize::from(available_parallelism().unwrap());
|
||||
|
||||
thread::scope(|s| {
|
||||
let result = Arc::new(AtomicUsize::new(usize::MAX));
|
||||
(0..parallelism)
|
||||
.map(|i| {
|
||||
let result = Arc::clone(&result);
|
||||
s.spawn(move || {
|
||||
(1..)
|
||||
.skip(i)
|
||||
.step_by(parallelism)
|
||||
.take_while(|&j| j <= result.load(atomic::Ordering::SeqCst))
|
||||
.find(|&j| test(input, j, n))
|
||||
.inspect(|&j| {
|
||||
result.fetch_min(j, atomic::Ordering::SeqCst);
|
||||
});
|
||||
})
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.into_iter()
|
||||
.for_each(|t| t.join().unwrap());
|
||||
|
||||
Arc::into_inner(result).unwrap().into_inner()
|
||||
})
|
||||
}
|
||||
|
||||
fn test(input: &Input, i: usize, n: usize) -> bool {
|
||||
let hash = Md5::new().chain_update(format!("{input}{i}")).finalize();
|
||||
hash.iter().take(n >> 1).all(|&x| x == 0) && (n & 1 == 0 || hash[n >> 1] >> 4 == 0)
|
||||
}
|
||||
|
||||
fn part1(input: &Input) -> usize {
|
||||
solve(input, 5)
|
||||
}
|
||||
|
||||
fn part2(input: &Input) -> usize {
|
||||
solve(input, 6)
|
||||
}
|
||||
|
||||
aoc::main!(2015, 4, ex: 1[a], 2[a]);
|
|
@ -2,7 +2,7 @@ aoc::year! {
|
|||
"01.rs",
|
||||
"02.rs",
|
||||
"03.rs",
|
||||
// "04.rs",
|
||||
"04.rs",
|
||||
// "05.rs",
|
||||
// "06.rs",
|
||||
// "07.rs",
|
||||
|
|
56
Rust/Cargo.lock
generated
56
Rust/Cargo.lock
generated
|
@ -37,6 +37,7 @@ dependencies = [
|
|||
"counter",
|
||||
"indexmap",
|
||||
"itertools 0.13.0",
|
||||
"md-5",
|
||||
"ndarray 0.16.1",
|
||||
"num",
|
||||
"paste",
|
||||
|
@ -96,6 +97,15 @@ version = "2.6.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
|
||||
|
||||
[[package]]
|
||||
name = "block-buffer"
|
||||
version = "0.10.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71"
|
||||
dependencies = [
|
||||
"generic-array",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
version = "1.5.0"
|
||||
|
@ -162,6 +172,26 @@ version = "0.8.20"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80"
|
||||
|
||||
[[package]]
|
||||
name = "crypto-common"
|
||||
version = "0.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
|
||||
dependencies = [
|
||||
"generic-array",
|
||||
"typenum",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "digest"
|
||||
version = "0.10.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
|
||||
dependencies = [
|
||||
"block-buffer",
|
||||
"crypto-common",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.13.0"
|
||||
|
@ -202,6 +232,16 @@ version = "1.0.7"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
|
||||
|
||||
[[package]]
|
||||
name = "generic-array"
|
||||
version = "0.14.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
|
||||
dependencies = [
|
||||
"typenum",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.2.15"
|
||||
|
@ -315,6 +355,16 @@ dependencies = [
|
|||
"rawpointer",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "md-5"
|
||||
version = "0.10.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"digest",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.7.4"
|
||||
|
@ -754,6 +804,12 @@ dependencies = [
|
|||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "typenum"
|
||||
version = "1.17.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"
|
||||
|
||||
[[package]]
|
||||
name = "unarray"
|
||||
version = "0.1.4"
|
||||
|
|
|
@ -22,6 +22,7 @@ rpath = false
|
|||
counter = "0.6.0"
|
||||
indexmap = "2.5.0"
|
||||
itertools = "0.13.0"
|
||||
md-5 = "0.10.6"
|
||||
ndarray = "0.16.1"
|
||||
num = "0.4.3"
|
||||
paste = "1.0.15"
|
||||
|
@ -52,6 +53,9 @@ path = "2015/02.rs"
|
|||
[[bin]]
|
||||
name = "2015_03"
|
||||
path = "2015/03.rs"
|
||||
[[bin]]
|
||||
name = "2015_04"
|
||||
path = "2015/04.rs"
|
||||
|
||||
# 2020
|
||||
[[bin]]
|
||||
|
|
1
examples/2015/4/1
Normal file
1
examples/2015/4/1
Normal file
|
@ -0,0 +1 @@
|
|||
abcdef
|
1
examples/2015/4/1.1
Normal file
1
examples/2015/4/1.1
Normal file
|
@ -0,0 +1 @@
|
|||
609043
|
1
examples/2015/4/2
Normal file
1
examples/2015/4/2
Normal file
|
@ -0,0 +1 @@
|
|||
pqrstuv
|
1
examples/2015/4/2.1
Normal file
1
examples/2015/4/2.1
Normal file
|
@ -0,0 +1 @@
|
|||
1048970
|
Loading…
Add table
Add a link
Reference in a new issue