-
-
Save solson/2fadd2e13b44b90736651a0b95562f2e to your computer and use it in GitHub Desktop.
Rust code shared from the playground
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
#![feature(i128_type)] | |
use std::collections::VecDeque; | |
const _INPUT: &str = "flqrgnkx"; | |
const INPUT: &str = "wenycdww"; | |
const SIZE: usize = 256; | |
const ROUNDS: usize = 64; | |
fn hash(input: &str) -> Vec<bool> { | |
let mut circle: Vec<usize> = (0..SIZE).collect(); | |
let mut pos = 0; | |
let mut skip_size = 0; | |
for _ in 0..ROUNDS { | |
for length in input.bytes().chain(vec![17, 31, 73, 47, 23]) { | |
let length = length as usize; | |
for i in 0..length / 2 { | |
circle.swap((pos + i) % SIZE, (pos + length - 1 - i) % SIZE); | |
} | |
pos += length + skip_size; | |
skip_size += 1; | |
} | |
} | |
let hash = circle | |
.chunks(16) | |
.map(|block| block.into_iter().fold(0, |acc, x| acc ^ x)) | |
.fold(0u128, |acc, x| (acc << 8) | x as u128); | |
(0..128).map(|i| (hash >> i) & 1 == 1).collect() | |
} | |
fn main() { | |
let mut grid: Vec<Vec<bool>> = (0..128) | |
.map(|row| hash(&format!("{}-{}", INPUT, row))) | |
.collect(); | |
let mut regions = 0; | |
for starting_row in 0..128 { | |
for starting_col in 0..128 { | |
if grid[starting_row][starting_col] { | |
regions += 1; | |
let mut queue = VecDeque::new(); | |
queue.push_back((starting_row, starting_col)); | |
grid[starting_row][starting_col] = false; | |
while let Some((row, col)) = queue.pop_front() { | |
let neighbors = [(-1, 0), (1, 0), (0, -1), (0, 1)] | |
.iter() | |
.filter_map(|&(row_diff, col_diff)| { | |
let r = row as isize + row_diff; | |
let c = col as isize + col_diff; | |
if r >= 0 && r < 128 && c >= 0 && c < 128 { | |
Some((r as usize, c as usize)) | |
} else { | |
None | |
} | |
}); | |
for (r, c) in neighbors { | |
if grid[r][c] { | |
queue.push_back((r, c)); | |
grid[r][c] = false; | |
} | |
} | |
} | |
} | |
} | |
} | |
println!("{}", regions); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment