Guest User

Untitled

a guest
Dec 12th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. use std::collections::{HashMap, HashSet, VecDeque};
  2. use std::iter::once;
  3. use std::str;
  4.  
  5. fn main() {
  6. let mut state: Vec<_> = INITIAL_STATE.bytes().collect();
  7.  
  8. let mut idx_min = 0i64;
  9. let mut idx_max = state.len() as i64;
  10.  
  11. let rules: HashMap<&[u8], u8> = RULES
  12. .split('\n')
  13. .map(|r| (&r.as_bytes()[0..5], r.bytes().nth(9).unwrap()))
  14. .collect();
  15.  
  16. let mut visited = HashMap::new();
  17. visited.insert(state.clone(), (0, idx_min, idx_max));
  18.  
  19. //println!("{:3} | {} | {}", idx_min, str::from_utf8(&state[..]).unwrap(), idx_max);
  20.  
  21. let mut i = 0;
  22. loop {
  23. i += 1;
  24. let l = state.len();
  25.  
  26. idx_min -= 2;
  27. idx_max += 2;
  28.  
  29. state = once(&[b'.', b'.', b'.', b'.', state[0]][..])
  30. .chain(once(&[b'.', b'.', b'.', state[0], state[1]][..]))
  31. .chain(once(&[b'.', b'.', state[0], state[1], state[2]][..]))
  32. .chain(once(&[b'.', state[0], state[1], state[2], state[3]][..]))
  33. .chain(state.windows(5))
  34. .chain(once(
  35. &[state[l - 4], state[l - 3], state[l - 2], state[l - 1], b'.'][..],
  36. ))
  37. .chain(once(
  38. &[state[l - 3], state[l - 2], state[l - 1], b'.', b'.'][..],
  39. ))
  40. .chain(once(&[state[l - 2], state[l - 1], b'.', b'.', b'.'][..]))
  41. .chain(once(&[state[l - 1], b'.', b'.', b'.', b'.'][..]))
  42. .map(|x| rules[x])
  43. .skip_while(|&x| {
  44. if x == b'.' {
  45. idx_min += 1;
  46. true
  47. } else {
  48. false
  49. }
  50. })
  51. //.inspect(|x| println!("{:?}", x))
  52. .collect();
  53.  
  54. while let Some(b'.') = state.last() {
  55. state.pop();
  56. idx_max -= 1;
  57. }
  58.  
  59. assert_eq!(idx_max - idx_min, state.len() as i64);
  60.  
  61. if visited.contains_key(&state) {
  62. let (a, b, c) = visited[&state];
  63. println!("steady state detected {} : {} ... {} --> {} : {} ... {}", a, b, c, i, idx_min, idx_max);
  64. break
  65. } else {
  66. visited.insert(state.clone(), (i, idx_min, idx_max));
  67. }
  68.  
  69. //println!("{:3} | {} | {}", idx_min, str::from_utf8(&state[..]).unwrap(), idx_max);
  70.  
  71. if i == 20 {
  72. println!("solution A: {}", state.iter().zip(idx_min..idx_max).filter_map(|(&x, i)| if x == b'#' {Some(i)} else {None}).sum::<i64>())
  73. }
  74. }
  75.  
  76. idx_min += 50000000000 - i;
  77. idx_max += 50000000000 - i;
  78.  
  79. println!("solution B: {}", state.iter().zip(idx_min..idx_max).filter_map(|(&x, i)| if x == b'#' {Some(i)} else {None}).sum::<i64>())
  80. }
  81.  
  82. const INITIAL_STATE: &str = "#...#..##.......####.#..###..#.##..########.#.#...#.#...###.#..###.###.#.#..#...#.#..##..#######.##";
  83.  
  84. const RULES: &str = "#..#. => #
  85. #.#.. => #
  86. ###.. => #
  87. ##..# => .
  88. .#.## => #
  89. ..... => .
  90. ...#. => #
  91. ##.#. => #
  92. #.#.# => .
  93. ###.# => #
  94. ....# => .
  95. ####. => #
  96. .##.. => #
  97. #.##. => #
  98. #..## => #
  99. ##... => #
  100. #...# => .
  101. ##.## => #
  102. .#... => .
  103. .#..# => #
  104. ..#.# => #
  105. ##### => .
  106. .#### => #
  107. ..#.. => #
  108. #.### => .
  109. ..##. => .
  110. .##.# => #
  111. .#.#. => .
  112. ..### => .
  113. .###. => .
  114. ...## => .
  115. #.... => .";
Add Comment
Please, Sign In to add comment