Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- p0 = '..#..####.##.####...#....#######..#.#..#..#.#.#####.######..#.#.#.#..##.###.#....####.#.#....#.#####'=='#';
- N = 300; % Padding
- p = [false(1,N),p0,false(1,N)];
- mostLeftPlantIdx = N+1;
- mostRightPlantIdx = N+length(p0);
- x = importdata('input.txt');
- L = length(x);
- r = false(L,6);
- for i=1:L
- r(i,1:5) = x{i}(1:5)=='#';
- r(i,6) = x{i}(10)=='#';
- end
- allSum = zeros(200,1);
- for generation=1:200
- pNew = p;
- for plantIdx = mostLeftPlantIdx-5:mostRightPlantIdx+5
- ruleFound = false;
- for ruleIdx = 1:size(r,1)
- if isequal( r(ruleIdx,1:5) , p(plantIdx-2:plantIdx+2)) % matching rule found
- pNew(plantIdx) = r(ruleIdx,6);
- ruleFound = true;
- end
- end
- if ~ruleFound
- pNew(plantIdx) = false;
- end
- end
- if generation == 20
- disp(sum(find(p)-(N+1))) % Part 1
- end
- p = pNew;
- mostLeftPlantIdx = find(p,1,'first');
- mostRightPlantIdx = find(p,1,'last');
- allSum(generation,1) = sum(find(p)-(N+1));
- end
- disp(allSum(end) + (allSum(end)-allSum(end-1)) * (50000000000-length(allSum))) % Part 2
Advertisement
Add Comment
Please, Sign In to add comment