part1 (2304B)
1--- Day 24: Planet of Discord --- 2 3You land on Eris, your last stop before reaching Santa. As soon as you do, your sensors start 4picking up strange life forms moving around: Eris is infested with bugs! With an over 24-hour 5roundtrip for messages between you and Earth, you'll have to deal with this problem on your own. 6 7Eris isn't a very large place; a scan of the entire area fits into a 5x5 grid (your puzzle input). 8The scan shows [1m[97mbugs[0m (#) and [1m[97mempty spaces[0m (.). 9 10Each [1m[97mminute[0m, The bugs live and die based on the number of bugs in the [1m[97mfour adjacent tiles[0m: 11 12 13 - A bug [1m[97mdies[0m (becoming an empty space) unless there is [1m[97mexactly one[0m bug adjacent to it. 14 15 - An empty space [1m[97mbecomes infested[0m with a bug if [1m[97mexactly one or two[0m bugs are adjacent to it. 16 17 18Otherwise, a bug or empty space remains the same. (Tiles on the edges of the grid have fewer than 19four adjacent tiles; the missing tiles count as empty space.) This process happens in every location 20[1m[97msimultaneously[0m; that is, within the same minute, the number of adjacent bugs is counted for every 21tile first, and then the tiles are updated. 22 23Here are the first few minutes of an example scenario: 24 25Initial state: 26....# 27#..#. 28#..## 29..#.. 30#.... 31 32After 1 minute: 33#..#. 34####. 35###.# 36##.## 37.##.. 38 39After 2 minutes: 40##### 41....# 42....# 43...#. 44#.### 45 46After 3 minutes: 47#.... 48####. 49...## 50#.##. 51.##.# 52 53After 4 minutes: 54####. 55....# 56##..# 57..... 58##... 59 60To understand the nature of the bugs, watch for the first time a layout of bugs and empty spaces 61[1m[97mmatches any previous layout[0m. In the example above, the first layout to appear twice is: 62 63..... 64..... 65..... 66#.... 67.#... 68 69To calculate the [1m[97mbiodiversity rating[0m for this layout, consider each tile left-to-right in the top 70row, then left-to-right in the second row, and so on. Each of these tiles is worth biodiversity 71points equal to [1m[97mincreasing powers of two[0m: 1, 2, 4, 8, 16, 32, and so on. Add up the biodiversity 72points for tiles with bugs; in this example, the 16th tile (32768 points) and 22nd tile (2097152 73points) have bugs, a total biodiversity rating of [1m[97m2129920[0m. 74 75[1m[97mWhat is the biodiversity rating for the first layout that appears twice?[0m 76 77