Skip to content

Instantly share code, notes, and snippets.

Created December 21, 2017 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/f2a458a45ea2c5d967944336fbdc4a47 to your computer and use it in GitHub Desktop.
Save anonymous/f2a458a45ea2c5d967944336fbdc4a47 to your computer and use it in GitHub Desktop.
Rust code shared from the playground
const INPUT: &str = "\
../.. => .##/##./.#.
#./.. => .#./#.#/##.
##/.. => #.#/#.#/###
.#/#. => #../.#./.#.
##/#. => ##./#.#/..#
##/## => #.#/#.#/...
.../.../... => ..##/##../##../#.#.
#../.../... => ##.#/..#./#.#./.#..
.#./.../... => ..#./##.#/#.##/###.
##./.../... => ###./##.#/.###/#.#.
#.#/.../... => ##../#..#/.###/#.#.
###/.../... => ...#/#..#/...#/...#
.#./#../... => ...#/.##./#.##/..#.
##./#../... => .##./.#../.##./.#..
..#/#../... => ####/.#../#.#./.###
#.#/#../... => ###./.#../##../....
.##/#../... => ##../#.#./#.#./##..
###/#../... => #.##/#..#/.#../##..
.../.#./... => .#.#/.###/.##./##..
#../.#./... => .###/.##./..##/..##
.#./.#./... => .##./.#.#/#.##/.###
##./.#./... => ..#./..../..#./###.
#.#/.#./... => ..../..#./..##/##..
###/.#./... => .#.#/#..#/.###/#..#
.#./##./... => ..../..#./.#../####
##./##./... => ..##/#.##/..#./#.##
..#/##./... => ..../#.##/.##./####
#.#/##./... => ..##/#.#./.#../.##.
.##/##./... => #.../...#/###./....
###/##./... => .#../#.#./#.##/....
.../#.#/... => #.#./####/#.../..#.
#../#.#/... => ...#/.#.#/###./.#.#
.#./#.#/... => #..#/#.../###./#.##
##./#.#/... => .##./#.../...#/#.##
#.#/#.#/... => #..#/##../##../.#..
###/#.#/... => #.#./...#/.#.#/.##.
.../###/... => .#.#/.##./..#./.#..
#../###/... => .###/..##/#.##/.#..
.#./###/... => #.../#.../.#../#...
##./###/... => .###/...#/.#.#/.#..
#.#/###/... => .#../..##/#..#/#...
###/###/... => .###/##../##.#/#.#.
..#/.../#.. => ##.#/..../...#/..##
#.#/.../#.. => .#.#/###./...#/.#.#
.##/.../#.. => ##.#/.#../####/#.##
###/.../#.. => #.../#..#/###./....
.##/#../#.. => #..#/..#./####/...#
###/#../#.. => ####/###./##.#/....
..#/.#./#.. => .##./.##./##../#..#
#.#/.#./#.. => #..#/#..#/#.../.#..
.##/.#./#.. => ##../##.#/#.##/..##
###/.#./#.. => #.##/..##/.##./#.#.
.##/##./#.. => #.##/..../##../....
###/##./#.. => ###./.#.#/.###/.#..
#../..#/#.. => .###/#.##/..#./.##.
.#./..#/#.. => #..#/..##/.#.#/##..
##./..#/#.. => ###./#.../..##/##..
#.#/..#/#.. => #.../.##./.###/###.
.##/..#/#.. => ...#/##.#/..#./...#
###/..#/#.. => ###./..#./.#../...#
#../#.#/#.. => #..#/...#/..#./.#.#
.#./#.#/#.. => #..#/##.#/####/.##.
##./#.#/#.. => .###/##../..../.#..
..#/#.#/#.. => ..#./##.#/####/###.
#.#/#.#/#.. => #.#./#.##/##.#/.###
.##/#.#/#.. => ..#./####/##../.###
###/#.#/#.. => .#.#/###./.#.#/#...
#../.##/#.. => .###/..##/.#.#/..#.
.#./.##/#.. => #.##/.#../.###/#.#.
##./.##/#.. => .###/#.../#.../..#.
#.#/.##/#.. => ##../...#/..#./...#
.##/.##/#.. => ..##/.#.#/...#/####
###/.##/#.. => ##../.###/##../###.
#../###/#.. => ###./#..#/#.#./....
.#./###/#.. => ..../#.#./.###/.###
##./###/#.. => .###/##../#..#/####
..#/###/#.. => ..../#.#./#..#/##..
#.#/###/#.. => .#.#/..##/##.#/#..#
.##/###/#.. => .#../...#/##../.#..
###/###/#.. => #.../.###/###./##.#
.#./#.#/.#. => .#.#/#.##/###./#...
##./#.#/.#. => .#../.#../.#../.#..
#.#/#.#/.#. => ##.#/..../###./.#..
###/#.#/.#. => #.#./##.#/.#.#/##..
.#./###/.#. => ##.#/..#./..#./#.#.
##./###/.#. => ####/.###/.#.#/.##.
#.#/###/.#. => .#../.###/##../#.#.
###/###/.#. => #.../.##./..##/####
#.#/..#/##. => ..../..#./##../...#
###/..#/##. => .###/..#./#.##/###.
.##/#.#/##. => .###/..../#.#./...#
###/#.#/##. => ###./...#/.###/####
#.#/.##/##. => #.##/#.../..../...#
###/.##/##. => #.../#.../#..#/...#
.##/###/##. => .#../###./.###/..#.
###/###/##. => ##.#/.#../###./.#..
#.#/.../#.# => #.#./#.#./..../...#
###/.../#.# => ####/###./..../##.#
###/#../#.# => .###/##.#/#.##/..#.
#.#/.#./#.# => ###./.###/#.##/....
###/.#./#.# => .##./###./#.#./##..
###/##./#.# => #.../.#.#/#.##/#..#
#.#/#.#/#.# => ..#./#.#./##../..##
###/#.#/#.# => ..#./.#../...#/.##.
#.#/###/#.# => ..#./###./##.#/####
###/###/#.# => #.../#.#./#..#/.#.#
###/#.#/### => ..##/.##./.#.#/#...
###/###/### => .##./..##/####/###.\
";
const _INPUT: &str = "\
../.# => ##./#../...
.#./..#/### => #..#/..../..../#..#\
";
const ITERATIONS: usize = 18;
fn parse_grid(src: &str) -> Vec<Vec<bool>> {
src
.split('/')
.map(|row| {
row
.chars()
.map(|c| c == '#')
.collect()
})
.collect()
}
fn rotate(v: &Vec<Vec<bool>>) -> Vec<Vec<bool>> {
let mut rotated = v.clone();
for i in 0..v.len() {
for j in 0..v.len() {
rotated[i][j] = v[v.len() - j - 1][i];
}
}
rotated
}
fn main() {
let mut grid = vec![
vec![false, true, false],
vec![false, false, true],
vec![true, true, true],
];
let mut inputs_2d = Vec::new();
let mut outputs_3d = Vec::new();
let mut inputs_3d = Vec::new();
let mut outputs_4d = Vec::new();
for line in INPUT.split('\n') {
let mut iter = line.split(" => ");
let input = parse_grid(iter.next().unwrap());
let output = parse_grid(iter.next().unwrap());
let rot0 = input;
let rot1 = rotate(&rot0);
let rot2 = rotate(&rot1);
let rot3 = rotate(&rot2);
let flip_vert = |v1: &Vec<Vec<bool>>| -> Vec<Vec<bool>> {
let mut v2 = v1.clone();
v2.reverse();
v2
};
let flip_horiz = |v1: &Vec<Vec<bool>>| -> Vec<Vec<bool>> {
let mut v2 = v1.clone();
for row in &mut v2 {
row.reverse();
}
v2
};
let rot0v = flip_vert(&rot0);
let rot1v = flip_vert(&rot1);
let rot2v = flip_vert(&rot2);
let rot3v = flip_vert(&rot3);
let rot0h = flip_horiz(&rot0);
let rot1h = flip_horiz(&rot1);
let rot2h = flip_horiz(&rot2);
let rot3h = flip_horiz(&rot3);
let (inputs, outputs) = if rot0.len() == 2 {
(&mut inputs_2d, &mut outputs_3d)
} else {
(&mut inputs_3d, &mut outputs_4d)
};
outputs.push(output);
let output_i = outputs.len() - 1;
inputs.push((rot0, output_i));
inputs.push((rot1, output_i));
inputs.push((rot2, output_i));
inputs.push((rot3, output_i));
inputs.push((rot0v, output_i));
inputs.push((rot1v, output_i));
inputs.push((rot2v, output_i));
inputs.push((rot3v, output_i));
inputs.push((rot0h, output_i));
inputs.push((rot1h, output_i));
inputs.push((rot2h, output_i));
inputs.push((rot3h, output_i));
}
for n in 0..ITERATIONS {
let (inputs, outputs, size) = if grid.len() % 2 == 0 {
(&inputs_2d, &outputs_3d, 2)
} else {
(&inputs_3d, &outputs_4d, 3)
};
let new_size = size + 1;
let new_len = (grid.len() / size) * new_size;
let mut new_grid = vec![vec![false; new_len]; new_len];
println!("{}: {:?}", n, grid.len());
for i_0 in 0..(grid.len() / size) {
for j_0 in 0..(grid.len() / size) {
let output_i = inputs.iter().find(|&&(ref pattern, _)| {
(0..size).all(|i| {
(0..size).all(|j| {
grid[i_0 * size + i][j_0 * size + j] == pattern[i][j]
})
})
}).unwrap().1;
for i in 0..new_size {
for j in 0..new_size {
new_grid[i_0 * new_size + i][j_0 * new_size + j] = outputs[output_i][i][j];
}
}
}
}
grid = new_grid;
}
let count = grid.into_iter().flat_map(|row| row).filter(|&cell| cell).count();
println!("{}", count);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment