C++ Lanternfish | Advent of Code Year 2021 Day 6

--- Day 6: Lanternfish ---

The sea floor is getting steeper. Maybe the sleigh keys got carried this way?

A massive school of glowing lanternfish swims past. They must spawn quickly to reach such large numbers - maybe exponentially quickly? You should model their growth rate to be sure.

Although you know nothing about this specific species of lanternfish, you make some guesses about their attributes. Surely, each lanternfish creates a new lanternfish once every 7 days.

However, this process isn't necessarily synchronized between every lanternfish - one lanternfish might have 2 days left until it creates another lanternfish, while another might have 4. So, you can model each fish as a single number that represents the number of days until it creates a new lanternfish.

Furthermore, you reason, a new lanternfish would surely need slightly longer before it's capable of producing more lanternfish: two more days for its first cycle.

So, suppose you have a lanternfish with an internal timer value of 3:

  • After one day, its internal timer would become 2.
  • After another day, its internal timer would become 1.
  • After another day, its internal timer would become 0.
  • After another day, its internal timer would reset to 6, and it would create a new lanternfish with an internal timer of 8.
  • After another day, the first lanternfish would have an internal timer of 5, and the second lanternfish would have an internal timer of 7.

A lanternfish that creates a new fish resets its timer to 6not 7 (because 0 is included as a valid timer value). The new lanternfish starts with an internal timer of 8 and does not start counting down until the next day.

Realizing what you're trying to do, the submarine automatically produces a list of the ages of several hundred nearby lanternfish (your puzzle input). For example, suppose you were given the following list:

3,4,3,1,2

This list means that the first fish has an internal timer of 3, the second fish has an internal timer of 4, and so on until the fifth fish, which has an internal timer of 2. Simulating these fish over several days would proceed as follows:

Initial state: 3,4,3,1,2
After  1 day:  2,3,2,0,1
After  2 days: 1,2,1,6,0,8
After  3 days: 0,1,0,5,6,7,8
After  4 days: 6,0,6,4,5,6,7,8,8
After  5 days: 5,6,5,3,4,5,6,7,7,8
After  6 days: 4,5,4,2,3,4,5,6,6,7
After  7 days: 3,4,3,1,2,3,4,5,5,6
After  8 days: 2,3,2,0,1,2,3,4,4,5
After  9 days: 1,2,1,6,0,1,2,3,3,4,8
After 10 days: 0,1,0,5,6,0,1,2,2,3,7,8
After 11 days: 6,0,6,4,5,6,0,1,1,2,6,7,8,8,8
After 12 days: 5,6,5,3,4,5,6,0,0,1,5,6,7,7,7,8,8
After 13 days: 4,5,4,2,3,4,5,6,6,0,4,5,6,6,6,7,7,8,8
After 14 days: 3,4,3,1,2,3,4,5,5,6,3,4,5,5,5,6,6,7,7,8
After 15 days: 2,3,2,0,1,2,3,4,4,5,2,3,4,4,4,5,5,6,6,7
After 16 days: 1,2,1,6,0,1,2,3,3,4,1,2,3,3,3,4,4,5,5,6,8
After 17 days: 0,1,0,5,6,0,1,2,2,3,0,1,2,2,2,3,3,4,4,5,7,8
After 18 days: 6,0,6,4,5,6,0,1,1,2,6,0,1,1,1,2,2,3,3,4,6,7,8,8,8,8

Each day, a 0 becomes a 6 and adds a new 8 to the end of the list, while each other number decreases by 1 if it was present at the start of the day.

In this example, after 18 days, there are a total of 26 fish. After 80 days, there would be a total of 5934.

Find a way to simulate lanternfish. How many lanternfish would there be after 80 days?

--- Part Two ---

Suppose the lanternfish live forever and have unlimited food and space. Would they take over the entire ocean?

After 256 days in the example above, there would be a total of 26984457539 lanternfish!

How many lanternfish would there be after 256 days?

Input:

1,2,1,3,2,1,1,5,1,4,1,2,1,4,3,3,5,1,1,3,5,3,4,5,5,4,3,1,1,4,3,1,5,2,5,2,4,1,1,1,1,1,1,1,4,1,4,4,4,1,4,4,1,4,2,1,1,1,1,3,5,4,3,3,5,4,1,3,1,1,2,1,1,1,4,1,2,5,2,3,1,1,1,2,1,5,1,1,1,4,4,4,1,5,1,2,3,2,2,2,1,1,4,3,1,4,4,2,1,1,5,1,1,1,3,1,2,1,1,1,1,4,5,5,2,3,4,2,1,1,1,2,1,1,5,5,3,5,4,3,1,3,1,1,5,1,1,4,2,1,3,1,1,4,3,1,5,1,1,3,4,2,2,1,1,2,1,1,2,1,3,2,3,1,4,5,1,1,4,3,3,1,1,2,2,1,5,2,1,3,4,5,4,5,5,4,3,1,5,1,1,1,4,4,3,2,5,2,1,4,3,5,1,3,5,1,3,3,1,1,1,2,5,3,1,1,3,1,1,1,2,1,5,1,5,1,3,1,1,5,4,3,3,2,2,1,1,3,4,1,1,1,1,4,1,3,1,5,1,1,3,1,1,1,1,2,2,4,4,4,1,2,5,5,2,2,4,1,1,4,2,1,1,5,1,5,3,5,4,5,3,1,1,1,2,3,1,2,1,1

Solution for part one:

366057

#include<bits/stdc++.h>
using namespace std;
long long n,d,a[100000000],f;
char s;
void displaytext(string t,double delay,bool end){
	int i=0;
	while (t[i]!='\0'){
		cout<<t[i];
		usleep(1000000*delay);
		i++;
	}
	if(end)cout<<endl;
}
int main(){
	displaytext("Enter amount of Lanternfish.",0.1,true);
	cin>>n;
	displaytext("Enter Lanternfish data.",0.1,true);
	for(int i=1;i<=n;i++){
		cin>>s;
		if(s==',')cin>>s;
		a[i]=s-'0';
	}
	displaytext("Enter amount of days for simulation.",0.1,true);
	cin>>d;
	displaytext("Processing...",0.05,false);
	sleep(1);
	for(int i=1;i<=d;i++){
		f=0;
		for(int j=1;j<=n;j++){	
			if(a[j]==0)a[j]=7,f++;
			a[j]--;
		}
		for(int j=1;j<=f;j++)n++,a[n]=8;
	}
	displaytext("\nAmount of Lanternfish after "+to_string(d)+" days:",0.05,false);
	sleep(1);
	displaytext(to_string(n),0.05,false);
	return 0;
}

Solution for part two (best solution,faster):

1653559299811

#include<bits/stdc++.h>
using namespace std;
unsigned long long n,d,a[100000000],b[10],sa;
char s;
void displaytext(string t,double delay,bool end){
	int i=0;
	while (t[i]!='\0'){
		cout<<t[i];
		usleep(1000000*delay);
		i++;
	}
	if(end)cout<<endl;
}
int main(){
	displaytext("Enter amount of Lanternfish.",0.05,true);
	cin>>n;
	displaytext("Enter Lanternfish data.",0.05,true);
	for(int i=1;i<=n;i++){
		cin>>s;
		if(s==',')cin>>s;
		a[i]=s-'0';
		b[a[i]]++;
	}
	displaytext("Enter amount of days for simulation.",0.05,true);
	cin>>d;
	displaytext("Processing...",0.05,false);
	sleep(1);
	for(int i=1;i<=d;i++){
		for(int j=1;j<=8;j++)b[j-1]=b[j];
		b[8]=sa;
		b[6]+=sa;
		sa=b[0];
	}
	n=0;
	for(int i=0;i<=8;i++)n+=b[i];
	displaytext("\nAmount of Lanternfish after "+to_string(d)+" days:",0.05,false);
	sleep(1);
	displaytext(to_string(n),0.05,false);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值