optimized day 4

This commit is contained in:
Ishan Jain 2023-01-02 15:58:24 +05:30
parent 67421f94f0
commit fa5b9c2275
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27
2 changed files with 80 additions and 90 deletions

View File

@ -10,9 +10,9 @@ fn solution(ip: &'static str) -> usize {
let mut input: Vec<u8> = ip.bytes().collect();
let l = input.len();
loop {
let mut t = vec![];
let mut t = Vec::with_capacity(32);
loop {
let mut n = num;
while n != 0 {
let d = n % 10;
@ -21,21 +21,21 @@ fn solution(ip: &'static str) -> usize {
n /= 10;
}
input.extend(t.into_iter().rev());
input.extend(t.iter().rev());
let c = md5_compute(input.clone());
let ans = md5_compute(&mut input);
if c[0..2] == [0, 0] && c[2] & 0b11110000 == 0 {
if ans.to_be() < 4096 {
return num;
}
input.resize(l, 0);
t.clear();
num += 1;
}
}
fn md5_compute(mut ip: Vec<u8>) -> [u8; 16] {
// s specifies the per-round shift amounts
const S: [u32; 64] = [
7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, // 0..15
@ -62,6 +62,7 @@ fn md5_compute(mut ip: Vec<u8>) -> [u8; 16] {
0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391, // 60..63
];
fn md5_compute(ip: &mut Vec<u8>) -> u32 {
let l = ip.len();
// Padding
@ -76,14 +77,14 @@ fn md5_compute(mut ip: Vec<u8>) -> [u8; 16] {
ip.extend((l * 8).to_le_bytes());
let input: Vec<u32> = ip
.chunks(4)
.map(|chunk| u32::from_le_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]))
.collect();
let mut state: [u32; 4] = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476];
for chunk in input.chunks(16) {
for chunk in ip.chunks(64).map(|chunk| {
chunk
.chunks(4)
.map(|c| u32::from_le_bytes([c[0], c[1], c[2], c[3]]))
.collect::<Vec<u32>>()
}) {
let mut lstate = state;
for i in 0..64 {
@ -122,13 +123,7 @@ fn md5_compute(mut ip: Vec<u8>) -> [u8; 16] {
state[3] += lstate[3];
}
let mut hash = [0; 16];
for (i, c) in state.map(|c| c.to_le_bytes()).flatten().iter().enumerate() {
hash[i] = *c;
}
hash
state[0]
}
fn main() {

View File

@ -10,9 +10,9 @@ fn solution(ip: &'static str) -> usize {
let mut input: Vec<u8> = ip.bytes().collect();
let l = input.len();
loop {
let mut t = vec![];
let mut t = Vec::with_capacity(32);
loop {
let mut n = num;
while n != 0 {
let d = n % 10;
@ -21,21 +21,21 @@ fn solution(ip: &'static str) -> usize {
n /= 10;
}
input.extend(t.into_iter().rev());
input.extend(t.iter().rev());
let c = md5_compute(input.clone());
let ans = md5_compute(&mut input);
if c[0..3] == [0, 0, 0] {
if ans.to_be() < 256 {
return num;
}
input.resize(l, 0);
t.clear();
num += 1;
}
}
fn md5_compute(mut ip: Vec<u8>) -> [u8; 16] {
// s specifies the per-round shift amounts
const S: [u32; 64] = [
7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, // 0..15
@ -62,6 +62,7 @@ fn md5_compute(mut ip: Vec<u8>) -> [u8; 16] {
0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391, // 60..63
];
fn md5_compute(ip: &mut Vec<u8>) -> u32 {
let l = ip.len();
// Padding
@ -76,14 +77,14 @@ fn md5_compute(mut ip: Vec<u8>) -> [u8; 16] {
ip.extend((l * 8).to_le_bytes());
let input: Vec<u32> = ip
.chunks(4)
.map(|chunk| u32::from_le_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]))
.collect();
let mut state: [u32; 4] = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476];
for chunk in input.chunks(16) {
for chunk in ip.chunks(64).map(|chunk| {
chunk
.chunks(4)
.map(|c| u32::from_le_bytes([c[0], c[1], c[2], c[3]]))
.collect::<Vec<u32>>()
}) {
let mut lstate = state;
for i in 0..64 {
@ -122,13 +123,7 @@ fn md5_compute(mut ip: Vec<u8>) -> [u8; 16] {
state[3] += lstate[3];
}
let mut hash = [0; 16];
for (i, c) in state.map(|c| c.to_le_bytes()).flatten().iter().enumerate() {
hash[i] = *c;
}
hash
state[0]
}
fn main() {