-
-
Save solson/7360d213a8a38aafb1c1cda9df42398a 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
// 25x25 | |
const INPUT: &str = "\ | |
#.....##.####.#.######### | |
.###..#..#..####.##....#. | |
..#########...###...####. | |
.##.#.##..#.#..#.#....### | |
...##....###..#.#..#.###. | |
###..#...######.####.#.#. | |
#..###..###..###.###.##.. | |
.#.#.###.#.#...####..#... | |
##........##.####..##...# | |
.#.##..#.#....##.##.##..# | |
###......#..##.####.###.# | |
....#..###..#######.#...# | |
#####.....#.##.#..#..#### | |
.#.###.#.###..##.#..####. | |
..#..##.###...#######.... | |
.#.##.#.#.#.#...###.#.#.. | |
##.###.#.#.###.#......#.. | |
###..##.#...#....#..####. | |
.#.#.....#..#....##..#..# | |
#####.#.##..#...##..#.... | |
##..#.#.#.####.#.##...##. | |
..#..#.#.####...#........ | |
###.###.##.#..#.##.....#. | |
.##..##.##...#..#..#.#..# | |
#...####.#.##...#..#.#.##\ | |
"; | |
// 3x3 | |
const _INPUT: &str = "\ | |
..# | |
#.. | |
...\ | |
"; | |
const INPUT_SIZE: usize = 25; | |
const SIZE: usize = 999; | |
const ITERATIONS: usize = 10_000; | |
fn main() { | |
let mut grid = Box::new([[false; SIZE]; SIZE]); | |
let center_offset = (SIZE - INPUT_SIZE) / 2; | |
for (i, line) in INPUT.split('\n').enumerate() { | |
for (j, c) in line.chars().enumerate() { | |
grid[center_offset + i][center_offset + j] = c == '#'; | |
} | |
} | |
let mut pos = (0, 0); | |
let mut dir = (-1, 0); | |
let mut infect_count = 0; | |
for _ in 0..ITERATIONS { | |
let center = ((SIZE - 1) / 2) as isize; | |
let i = (center + pos.0) as usize; | |
let j = (center + pos.1) as usize; | |
if grid[i][j] { | |
dir = match dir { | |
(1, 0) => (0, -1), | |
(-1, 0) => (0, 1), | |
(0, 1) => (1, 0), | |
(0, -1) => (-1, 0), | |
_ => unreachable!(), | |
}; | |
grid[i][j] = false; | |
} else { | |
dir = match dir { | |
(0, -1) => (1, 0), | |
(0, 1) => (-1, 0), | |
(1, 0) => (0, 1), | |
(-1, 0) => (0, -1), | |
_ => unreachable!(), | |
}; | |
grid[i][j] = true; | |
infect_count += 1; | |
} | |
pos.0 += dir.0; | |
pos.1 += dir.1; | |
} | |
println!("{}", infect_count); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment