-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday6.py
178 lines (131 loc) · 5.52 KB
/
day6.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import copy
class Day6:
def __init__(self, input_file_name):
self.init_state = []
self.init_state2 = []
self.input_file_name = input_file_name
self.read_input_data()
def read_input_data(self):
with open(self.input_file_name) as f:
data = f.read().splitlines()
self.init_state = list(map(int, data[0].split(',')))
for remain_turn in self.init_state:
self.init_state2.append({'current_day': 0, 'remain_turn': remain_turn})
def get_total_lanternfish_count_after_days(self, days):
current_day = 1
zero_count = 0
for i in range(days):
prev_zero_count = zero_count
zero_count = 0
for index, remain_turn in enumerate(self.init_state):
self.init_state[index] -= 1
if self.init_state[index] == 0:
zero_count += 1
self.init_state[index] = 7
for j in range(prev_zero_count):
self.init_state.append(8)
# print(f'days: {current_day} state: {self.init_state}')
# print(f'days: {current_day}, count: {len(self.init_state)}')
current_day += 1
return len(self.init_state)
def get_total_lanternfish_count_after_days2(self, days):
total_count = len(self.init_state2)
BORN_CYCLE_DAYS = 7
for state in self.init_state2:
born_fish_count = int((days-(state['remain_turn']-BORN_CYCLE_DAYS)-(state['current_day']+1))/BORN_CYCLE_DAYS)
total_count += born_fish_count
# print(born_fish_count)
# if state['remain_turn'] < BORN_CYCLE_DAYS:
# day = BORN_CYCLE_DAYS - state['remain_turn']
# else:
# day = state['current_day'] + 9
if state['remain_turn'] == 8:
day = state['current_day'] + 9
else:
day = state['remain_turn']+1
for i in range(born_fish_count):
self.init_state2.append({'current_day': day, 'remain_turn': 8})
day += BORN_CYCLE_DAYS
# print(total_count)
return total_count
def get_total_lanternfish_count_after_days3(self, days):
fish_dic = {}
total_count = 0
num = 5
init_num = num
tommorow_fishes = []
tommorow_fishes.append(init_num)
for day in range(days+10):
key = f'num({num}):day{day+1}'
today_fishes = copy.deepcopy(tommorow_fishes)
for index, fish in enumerate(today_fishes):
if fish == 0:
tommorow_fishes[index] = 6
tommorow_fishes.append(8)
else:
tommorow_fishes[index] -= 1
print(day, len(tommorow_fishes))
fish_dic[key] = len(tommorow_fishes)
print(key, len(tommorow_fishes))
for input_num in self.init_state:
key = f'num(5):day{days+(5-input_num)}'
print(fish_dic[key])
total_count += fish_dic[key]
return total_count
def get_total_lanternfish_count_after_days4(self, days):
fish_dic = {}
total_count = 0
init_num = 10
tommorow_fishes = []
tommorow_fishes.append(init_num)
for day in range(days+10):
day_x = day+1
key = f'num({init_num}):day{day_x}'
today_fishes = copy.deepcopy(tommorow_fishes)
for index, fish in enumerate(today_fishes):
if fish == 0:
tommorow_fishes[index] = 6
tommorow_fishes.append(8)
else:
tommorow_fishes[index] -= 1
print(day_x, len(tommorow_fishes))
fish_dic[key] = { 'num': len(tommorow_fishes), 'fishes': copy.deepcopy(tommorow_fishes) }
# print(key, len(tommorow_fishes))
for input_num in self.init_state:
key = f'num({init_num}):day{days+(init_num-input_num)}'
print(input_num, key, fish_dic[key])
total_count += fish_dic[key]['num']
return total_count
def get_total_lanternfish_count_after_days5(self, days):
fishes_dict = {}
current_state = copy.deepcopy(self.init_state)
for num in current_state:
if num in fishes_dict:
fishes_dict[num] += 1
else:
fishes_dict[num] = 1
today_fishes_dict = fishes_dict
for day in range(days):
tommorow_fishes_dict = {}
for number, count in today_fishes_dict.items():
if number == 0:
if 6 in tommorow_fishes_dict:
tommorow_fishes_dict[6] += count
else:
tommorow_fishes_dict[6] = count
if 8 in tommorow_fishes_dict:
tommorow_fishes_dict[8] += count
else:
tommorow_fishes_dict[8] = count
else:
tommorow_number = number - 1
if tommorow_number in tommorow_fishes_dict:
tommorow_fishes_dict[tommorow_number] += count
else:
tommorow_fishes_dict[tommorow_number] = count
print(day+1, tommorow_fishes_dict)
today_fishes_dict = copy.deepcopy(tommorow_fishes_dict)
total_count = 0
for number, count in today_fishes_dict.items():
total_count += count
return total_count